Skip to content

Commit c24385d

Browse files
committed
Allow multiple --yq options for limactl ls
This is mostly useful for selection, so e.g. a plugin can select all running instances, and an option to the plugin can narrow down the selection further, or select fields etc. This change also make the `--quiet` option compatible with `--yq`. Before `--quiet` took precedence and `--yq` was ignored instead of generated an incompatibility warning. Now `--quiet` works with `--yq` by passing the final result through ` | .name`, which implements the same functionality it normally has. Signed-off-by: Jan Dubois <jan.dubois@suse.com>
1 parent ab8908b commit c24385d

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

cmd/limactl/list.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The following legacy flags continue to function:
7171
listCommand.Flags().Bool("json", false, "JSONify output")
7272
listCommand.Flags().BoolP("quiet", "q", false, "Only show names")
7373
listCommand.Flags().Bool("all-fields", false, "Show all fields")
74-
listCommand.Flags().String("yq", "", "Apply yq expression to each instance")
74+
listCommand.Flags().StringArray("yq", nil, "Apply yq expression to each instance")
7575

7676
return listCommand
7777
}
@@ -117,7 +117,7 @@ func listAction(cmd *cobra.Command, args []string) error {
117117
if err != nil {
118118
return err
119119
}
120-
yq, err := cmd.Flags().GetString("yq")
120+
yq, err := cmd.Flags().GetStringArray("yq")
121121
if err != nil {
122122
return err
123123
}
@@ -133,7 +133,7 @@ func listAction(cmd *cobra.Command, args []string) error {
133133
if listFields && cmd.Flags().Changed("format") {
134134
return errors.New("option --list-fields conflicts with option --format")
135135
}
136-
if yq != "" {
136+
if len(yq) != 0 {
137137
if cmd.Flags().Changed("format") && format != "json" && format != "yaml" {
138138
return errors.New("option --yq only works with --format json or yaml")
139139
}
@@ -182,7 +182,7 @@ func listAction(cmd *cobra.Command, args []string) error {
182182
instanceNames = allInstances
183183
}
184184

185-
if quiet {
185+
if quiet && len(yq) == 0 {
186186
for _, instName := range instanceNames {
187187
fmt.Fprintln(cmd.OutOrStdout(), instName)
188188
}
@@ -221,22 +221,27 @@ func listAction(cmd *cobra.Command, args []string) error {
221221
}
222222
}
223223
// --yq implies --format json unless --format yaml has been explicitly specified
224-
if yq != "" && !cmd.Flags().Changed("format") {
224+
if len(yq) != 0 && !cmd.Flags().Changed("format") {
225225
format = "json"
226226
}
227227
// Always pipe JSON and YAML through yq to colorize it if isTTY
228-
if yq == "" && (format == "json" || format == "yaml") {
229-
yq = "."
228+
if len(yq) == 0 && (format == "json" || format == "yaml") {
229+
yq = append(yq, ".")
230230
}
231231

232-
if yq == "" {
232+
if len(yq) == 0 {
233233
err = store.PrintInstances(cmd.OutOrStdout(), instances, format, &options)
234234
if err == nil && unmatchedInstances {
235235
return unmatchedInstancesError{}
236236
}
237237
return err
238238
}
239239

240+
if quiet {
241+
yq = append(yq, ".name")
242+
}
243+
yqExpr := strings.Join(yq, " | ")
244+
240245
buf := new(bytes.Buffer)
241246
err = store.PrintInstances(buf, instances, format, &options)
242247
if err != nil {
@@ -260,7 +265,7 @@ func listAction(cmd *cobra.Command, args []string) error {
260265
scanner := bufio.NewScanner(buf)
261266
for scanner.Scan() {
262267
var str string
263-
if str, err = yqutil.EvaluateExpressionWithEncoder(yq, scanner.Text(), encoder); err != nil {
268+
if str, err = yqutil.EvaluateExpressionWithEncoder(yqExpr, scanner.Text(), encoder); err != nil {
264269
return err
265270
}
266271
if _, err = fmt.Fprint(cmd.OutOrStdout(), str); err != nil {
@@ -277,12 +282,12 @@ func listAction(cmd *cobra.Command, args []string) error {
277282
var str string
278283
if isTTY {
279284
// This branch is trading the better formatting from yamlfmt for colorizing from yqlib.
280-
if str, err = yqutil.EvaluateExpressionPlain(yq, buf.String(), true); err != nil {
285+
if str, err = yqutil.EvaluateExpressionPlain(yqExpr, buf.String(), true); err != nil {
281286
return err
282287
}
283288
} else {
284289
var res []byte
285-
if res, err = yqutil.EvaluateExpression(yq, buf.Bytes()); err != nil {
290+
if res, err = yqutil.EvaluateExpression(yqExpr, buf.Bytes()); err != nil {
286291
return err
287292
}
288293
str = string(res)

0 commit comments

Comments
 (0)