Skip to content

Commit 8e5a3bc

Browse files
-o json non-breaking, -o api-json is list of calls
1 parent a072fdb commit 8e5a3bc

2 files changed

Lines changed: 75 additions & 15 deletions

File tree

internal/core/flag-option.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,39 @@ func WithCompletionComplex(
6565
return completionFunc(passedCmd, args, toComplete)
6666
}
6767

68-
// Determine the location to use
69-
location, _ := passedCmd.Flags().GetString(constants.FlagLocation)
70-
if location == "" && len(locations) > 0 {
71-
location = locations[0]
68+
// If --location explicitly set, complete from that location only
69+
if passedCmd.Flags().Changed(constants.FlagLocation) {
70+
location, _ := passedCmd.Flags().GetString(constants.FlagLocation)
71+
normalizedLoc := strings.ReplaceAll(location, "/", "-")
72+
if strings.Contains(baseURL, "%s") {
73+
viper.Set(constants.ArgServerUrl, fmt.Sprintf(baseURL, normalizedLoc))
74+
} else {
75+
viper.Set(constants.ArgServerUrl, baseURL)
76+
}
77+
return completionFunc(passedCmd, args, toComplete)
7278
}
7379

74-
// Normalize the location and set the server URL
75-
normalizedLoc := strings.ReplaceAll(location, "/", "-")
76-
if strings.Contains(baseURL, "%s") {
77-
viper.Set(constants.ArgServerUrl, fmt.Sprintf(baseURL, normalizedLoc))
78-
} else {
80+
// No explicit --location: query all locations, merge with location hints
81+
if !strings.Contains(baseURL, "%s") || len(locations) == 0 {
7982
viper.Set(constants.ArgServerUrl, baseURL)
83+
return completionFunc(passedCmd, args, toComplete)
8084
}
8185

82-
return completionFunc(passedCmd, args, toComplete)
86+
var allResults []string
87+
for _, loc := range locations {
88+
normalizedLoc := strings.ReplaceAll(loc, "/", "-")
89+
viper.Set(constants.ArgServerUrl, fmt.Sprintf(baseURL, normalizedLoc))
90+
91+
results, _ := completionFunc(passedCmd, args, toComplete)
92+
for _, r := range results {
93+
if strings.Contains(r, "\t") {
94+
allResults = append(allResults, r+" ("+loc+")")
95+
} else {
96+
allResults = append(allResults, r+"\t("+loc+")")
97+
}
98+
}
99+
}
100+
return allResults, cobra.ShellCompDirectiveNoFileComp
83101
},
84102
)
85103
}

internal/core/regional_list.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,58 @@ func (c *CommandConfig) ListAllLocations(
119119

120120
// Determine output format
121121
format := viper.GetString(constants.ArgOutput)
122-
if format == "json" || format == "api-json" {
123-
return c.regionalJSON(results)
122+
switch format {
123+
case "json":
124+
return c.regionalLegacyJSON(results)
125+
case "api-json":
126+
return c.regionalAPIJSON(results)
124127
}
125128

126129
return c.regionalText(results, columns)
127130
}
128131

129-
// regionalJSON outputs an array of per-location API responses.
130-
// Both -o json and -o api-json produce the same format: an array of raw responses.
131-
func (c *CommandConfig) regionalJSON(results []locResult) error {
132+
// regionalLegacyJSON merges items from all locations into {"items": [...]}.
133+
// This matches the single-location -o json format (non-breaking).
134+
func (c *CommandConfig) regionalLegacyJSON(results []locResult) error {
135+
if viper.GetBool(constants.ArgQuiet) {
136+
return nil
137+
}
138+
139+
allItems := make([]any, 0)
140+
for _, r := range results {
141+
if r.err != nil {
142+
continue
143+
}
144+
// Marshal/unmarshal to get a generic map, then extract items
145+
b, err := json.Marshal(r.data)
146+
if err != nil {
147+
continue
148+
}
149+
var m map[string]any
150+
if err := json.Unmarshal(b, &m); err != nil {
151+
continue
152+
}
153+
if items, ok := m["items"].([]any); ok {
154+
allItems = append(allItems, items...)
155+
}
156+
}
157+
158+
var data any = map[string]any{"items": allItems}
159+
data, err := table.ApplyQueryFilter(data)
160+
if err != nil {
161+
return err
162+
}
163+
164+
out, err := json.MarshalIndent(data, "", " ")
165+
if err != nil {
166+
return err
167+
}
168+
return c.Out(string(out)+"\n", nil)
169+
}
170+
171+
// regionalAPIJSON outputs an array of per-location API responses.
172+
// Each element is the raw, untouched API response for that location.
173+
func (c *CommandConfig) regionalAPIJSON(results []locResult) error {
132174
if viper.GetBool(constants.ArgQuiet) {
133175
return nil
134176
}

0 commit comments

Comments
 (0)