@@ -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