Merge pull request #13898 from baude/machinelistquiet

Add --quiet to machine ls
This commit is contained in:
OpenShift Merge Robot
2022-04-18 10:27:16 -04:00
committed by GitHub
2 changed files with 17 additions and 4 deletions

View File

@ -41,6 +41,7 @@ var (
type listFlagType struct { type listFlagType struct {
format string format string
noHeading bool noHeading bool
quiet bool
} }
type machineReporter struct { type machineReporter struct {
@ -70,6 +71,7 @@ func init() {
flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using JSON or a Go template") flags.StringVar(&listFlag.format, formatFlagName, "{{.Name}}\t{{.VMType}}\t{{.Created}}\t{{.LastUp}}\t{{.CPUs}}\t{{.Memory}}\t{{.DiskSize}}\n", "Format volume output using JSON or a Go template")
_ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(machineReporter{})) _ = lsCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(machineReporter{}))
flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers") flags.BoolVar(&listFlag.noHeading, "noheading", false, "Do not print headers")
flags.BoolVarP(&listFlag.quiet, "quiet", "q", false, "Show only machine names")
} }
func list(cmd *cobra.Command, args []string) error { func list(cmd *cobra.Command, args []string) error {
@ -79,6 +81,10 @@ func list(cmd *cobra.Command, args []string) error {
err error err error
) )
if listFlag.quiet {
listFlag.format = "{{.Name}}\n"
}
provider := getSystemDefaultProvider() provider := getSystemDefaultProvider()
listResponse, err = provider.List(opts) listResponse, err = provider.List(opts)
if err != nil { if err != nil {
@ -124,7 +130,10 @@ func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error {
"Memory": "MEMORY", "Memory": "MEMORY",
"DiskSize": "DISK SIZE", "DiskSize": "DISK SIZE",
}) })
printHeader := !listFlag.noHeading
if listFlag.quiet {
printHeader = false
}
var row string var row string
switch { switch {
case cmd.Flags().Changed("format"): case cmd.Flags().Changed("format"):
@ -146,8 +155,7 @@ func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error {
return err return err
} }
defer w.Flush() defer w.Flush()
if printHeader {
if !listFlag.noHeading {
if err := tmpl.Execute(w, headers); err != nil { if err := tmpl.Execute(w, headers); err != nil {
return errors.Wrapf(err, "failed to write report column headers") return errors.Wrapf(err, "failed to write report column headers")
} }

View File

@ -47,7 +47,12 @@ Print usage statement.
#### **--noheading** #### **--noheading**
Omit the table headings from the listing of pods. Omit the table headings from the listing of machines
#### **--quiet**, **-q**
Only print the name of the machine. This also implies no table heading
is printed.
## EXAMPLES ## EXAMPLES