Skip to content

Commit 72eb96f

Browse files
feat(pkg): add --omit-group flag to package list command
Agent-Logs-Url: https://github.com/microsoft/azure-linux-dev-tools/sessions/1b729d85-f047-4447-8a61-0344df08ffc5 Co-authored-by: christopherco <35273088+christopherco@users.noreply.github.com>
1 parent a377109 commit 72eb96f

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

docs/user/how-to/inspect-package-config.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ azldev package list -a -q -O json
9090
]
9191
```
9292

93+
To exclude the `group` field from the JSON output (for example, when consumers only
94+
need package name and channel), add `--omit-group`:
95+
96+
```bash
97+
azldev package list -a -q -O json --omit-group
98+
```
99+
100+
```json
101+
[
102+
{
103+
"packageName": "libcurl",
104+
"component": "",
105+
"channel": "rpm-base"
106+
},
107+
...
108+
]
109+
```
110+
93111
## Alias
94112

95113
`pkg` is an alias for the `package` subcommand:

docs/user/reference/cli/azldev_package_list.md

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/azldev/cmds/pkg/list.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type ListPackageOptions struct {
2121
// PackageNames contains specific binary package names to look up.
2222
// If a package is not in any explicit config it is still resolved using project defaults.
2323
PackageNames []string
24+
25+
// OmitGroup omits the 'group' field from the output when true.
26+
OmitGroup bool
2427
}
2528

2629
func listOnAppInit(_ *azldev.App, parent *cobra.Command) {
@@ -56,7 +59,10 @@ Resolution order (lowest to highest priority):
5659
azldev package list -p curl -p wget
5760
5861
# Output as JSON for scripting
59-
azldev package list -a -q -O json`,
62+
azldev package list -a -q -O json
63+
64+
# Output as JSON without the group field
65+
azldev package list -a -q -O json --omit-group`,
6066
RunE: azldev.RunFuncWithExtraArgs(func(env *azldev.Env, args []string) (interface{}, error) {
6167
options.PackageNames = append(args, options.PackageNames...)
6268

@@ -66,6 +72,7 @@ Resolution order (lowest to highest priority):
6672

6773
cmd.Flags().BoolVarP(&options.All, "all-packages", "a", false, "List all explicitly-configured binary packages")
6874
cmd.Flags().StringArrayVarP(&options.PackageNames, "package", "p", []string{}, "Package name to look up (repeatable)")
75+
cmd.Flags().BoolVar(&options.OmitGroup, "omit-group", false, "Omit the 'group' field from the output")
6976

7077
azldev.ExportAsMCPTool(cmd)
7178

@@ -78,7 +85,8 @@ type PackageListResult struct {
7885
PackageName string `json:"packageName" table:"Package"`
7986

8087
// Group is the package-group this package belongs to, or empty if it is not in any group.
81-
Group string `json:"group" table:"Group"`
88+
// When [ListPackageOptions.OmitGroup] is true, this field is always empty and omitted from JSON.
89+
Group string `json:"group,omitempty" table:"Group"`
8290

8391
// Component is the component that has an explicit per-package override for this package,
8492
// or empty if the package is only configured via a group or project default.
@@ -178,15 +186,19 @@ func ListPackages(env *azldev.Env, options *ListPackageOptions) ([]PackageListRe
178186
return nil, fmt.Errorf("failed to resolve config for package %#q:\n%w", pkgName, err)
179187
}
180188

189+
group := groupOf[pkgName]
190+
if options.OmitGroup {
191+
group = ""
192+
}
193+
181194
results = append(results, PackageListResult{
182195
PackageName: pkgName,
183-
Group: groupOf[pkgName],
196+
Group: group,
184197
Component: compName,
185198
Channel: pkgConfig.Publish.Channel,
186199
})
187200
}
188201

189-
// Sort by package name for deterministic, readable output.
190202
sort.Slice(results, func(i, j int) bool {
191203
return results[i].PackageName < results[j].PackageName
192204
})

internal/app/azldev/cmds/pkg/list_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestNewPackageListCommand(t *testing.T) {
1818
require.NotNil(t, cmd)
1919
assert.Equal(t, "list [package-name...]", cmd.Use)
2020
assert.NotNil(t, cmd.RunE)
21+
assert.NotNil(t, cmd.Flags().Lookup("omit-group"), "expected --omit-group flag to be registered")
2122
}
2223

2324
func TestListPackages_NoCriteria(t *testing.T) {
@@ -213,3 +214,26 @@ func TestListPackages_DuplicatePackageAcrossComponents_ReturnsError(t *testing.T
213214
assert.Contains(t, err.Error(), "curl")
214215
assert.Contains(t, err.Error(), "other")
215216
}
217+
218+
func TestListPackages_OmitGroup_ClearsGroupField(t *testing.T) {
219+
testEnv := testutils.NewTestEnv(t)
220+
testEnv.Config.PackageGroups = map[string]projectconfig.PackageGroupConfig{
221+
"devel-packages": {
222+
Packages: []string{"curl-devel"},
223+
DefaultPackageConfig: projectconfig.PackageConfig{
224+
Publish: projectconfig.PackagePublishConfig{Channel: "devel"},
225+
},
226+
},
227+
}
228+
229+
results, err := pkgcmds.ListPackages(testEnv.Env, &pkgcmds.ListPackageOptions{
230+
All: true,
231+
OmitGroup: true,
232+
})
233+
234+
require.NoError(t, err)
235+
require.Len(t, results, 1)
236+
assert.Equal(t, "curl-devel", results[0].PackageName)
237+
assert.Empty(t, results[0].Group, "group should be empty when OmitGroup is true")
238+
assert.Equal(t, "devel", results[0].Channel)
239+
}

0 commit comments

Comments
 (0)