-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CLI flags to configure table print options
Signed-off-by: Justin Toh <tohjustin@hotmail.com>
- Loading branch information
Showing
4 changed files
with
235 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package lineage | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
) | ||
|
||
// HumanPrintFlags provides default flags necessary for printing. Given the | ||
// following flag values, a printer can be requested that knows how to handle | ||
// printing based on these values. | ||
type HumanPrintFlags struct { | ||
NoHeaders *bool | ||
ShowGroup *bool | ||
ShowLabels *bool | ||
WithNamespace bool | ||
} | ||
|
||
// EnsureWithGroup sets the "ShowGroup" human-readable option to true. | ||
func (f *HumanPrintFlags) EnsureWithGroup() error { | ||
showGroup := true | ||
f.ShowGroup = &showGroup | ||
return nil | ||
} | ||
|
||
// EnsureWithNamespace sets the "WithNamespace" human-readable option to true. | ||
func (f *HumanPrintFlags) EnsureWithNamespace() error { | ||
f.WithNamespace = true | ||
return nil | ||
} | ||
|
||
// AllowedFormats returns more customized formating options | ||
func (f *HumanPrintFlags) AllowedFormats() []string { | ||
return []string{"wide"} | ||
} | ||
|
||
// ToPrinter receives an outputFormat and returns a printer capable of handling | ||
// human-readable output. | ||
func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) { | ||
if len(outputFormat) > 0 && outputFormat != "wide" { | ||
return nil, genericclioptions.NoCompatiblePrinterError{Options: f, AllowedFormats: f.AllowedFormats()} | ||
} | ||
noHeaders := false | ||
if f.NoHeaders != nil { | ||
noHeaders = *f.NoHeaders | ||
} | ||
showLabels := false | ||
if f.ShowLabels != nil { | ||
showLabels = *f.ShowLabels | ||
} | ||
p := printers.NewTablePrinter(printers.PrintOptions{ | ||
NoHeaders: noHeaders, | ||
ShowLabels: showLabels, | ||
Wide: outputFormat == "wide", | ||
WithNamespace: f.WithNamespace, | ||
}) | ||
return p, nil | ||
} | ||
|
||
// AddFlags receives a *cobra.Command reference and binds flags related to | ||
// human-readable printing to it | ||
func (f *HumanPrintFlags) AddFlags(c *cobra.Command) { | ||
if f.NoHeaders != nil { | ||
c.Flags().BoolVar(f.NoHeaders, "no-headers", *f.NoHeaders, "When using the default output format, don't print headers (default print headers).") | ||
} | ||
if f.ShowLabels != nil { | ||
c.Flags().BoolVar(f.ShowLabels, "show-labels", *f.ShowLabels, "When printing, show all labels as the last column (default hide labels column)") | ||
} | ||
if f.ShowGroup != nil { | ||
c.Flags().BoolVar(f.ShowGroup, "show-group", *f.ShowGroup, "If present, include the resource group for the requested object(s).") | ||
} | ||
} | ||
|
||
// NewHumanPrintFlags returns flags associated with human-readable printing, | ||
// with default values set. | ||
func NewHumanPrintFlags() *HumanPrintFlags { | ||
noHeaders := false | ||
showGroup := false | ||
showLabels := false | ||
|
||
return &HumanPrintFlags{ | ||
NoHeaders: &noHeaders, | ||
ShowGroup: &showGroup, | ||
ShowLabels: &showLabels, | ||
WithNamespace: false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package lineage | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
) | ||
|
||
// PrintFlags composes common printer flag structs used in the lineage command. | ||
type PrintFlags struct { | ||
HumanReadableFlags *HumanPrintFlags | ||
OutputFormat *string | ||
} | ||
|
||
// AllowedFormats is the list of formats in which data can be displayed | ||
func (f *PrintFlags) AllowedFormats() []string { | ||
formats := []string{} | ||
formats = append(formats, f.HumanReadableFlags.AllowedFormats()...) | ||
return formats | ||
} | ||
|
||
// Copy returns a copy of PrintFlags for mutation | ||
func (f *PrintFlags) Copy() PrintFlags { | ||
printFlags := *f | ||
return printFlags | ||
} | ||
|
||
// EnsureWithGroup ensures that human-readable flags return a printer capable of | ||
// including resource kinds. | ||
func (f *PrintFlags) EnsureWithGroup() error { | ||
return f.HumanReadableFlags.EnsureWithGroup() | ||
} | ||
|
||
// EnsureWithNamespace ensures that human-readable flags return a printer capable | ||
// of printing with a "namespace" column. | ||
func (f *PrintFlags) EnsureWithNamespace() error { | ||
return f.HumanReadableFlags.EnsureWithNamespace() | ||
} | ||
|
||
// ToPrinter attempts to find a composed set of PrintFlags suitable for | ||
// returning a printer based on current flag values. | ||
func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) { | ||
outputFormat := "" | ||
if f.OutputFormat != nil { | ||
outputFormat = *f.OutputFormat | ||
} | ||
|
||
p, err := f.HumanReadableFlags.ToPrinter(outputFormat) | ||
if !genericclioptions.IsNoCompatiblePrinterError(err) { | ||
return p, err | ||
} | ||
|
||
return nil, genericclioptions.NoCompatiblePrinterError{ | ||
AllowedFormats: f.AllowedFormats(), | ||
OutputFormat: &outputFormat, | ||
} | ||
} | ||
|
||
// AddFlags receives a *cobra.Command reference and binds flags related to | ||
// human-readable printing to it | ||
func (f *PrintFlags) AddFlags(c *cobra.Command) { | ||
f.HumanReadableFlags.AddFlags(c) | ||
|
||
if f.OutputFormat != nil { | ||
c.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(f.AllowedFormats(), "|"))) | ||
} | ||
} | ||
|
||
// NewLineagePrintFlags returns flags associated with human-readable printing, | ||
// with default values set. | ||
func NewLineagePrintFlags() *PrintFlags { | ||
outputFormat := "" | ||
|
||
return &PrintFlags{ | ||
OutputFormat: &outputFormat, | ||
HumanReadableFlags: NewHumanPrintFlags(), | ||
} | ||
} |
Oops, something went wrong.