mirror of https://github.com/k3s-io/k3s
special-case template printing in get.go
parent
e467e9abb7
commit
508145e529
|
@ -1620,6 +1620,10 @@ run_kubectl_get_tests() {
|
|||
## check --allow-missing-template-keys defaults to true for go templates
|
||||
kubectl get "${kube_flags[@]}" pod valid-pod -o go-template='{{.missing}}'
|
||||
|
||||
## check --template flag causes go-template to be printed, even when no --output value is provided
|
||||
output_message=$(kubectl get "${kube_flags[@]}" pod valid-pod --template="{{$id_field}}:")
|
||||
kube::test::if_has_string "${output_message}" 'valid-pod:'
|
||||
|
||||
## check --allow-missing-template-keys=false results in an error for a missing key with jsonpath
|
||||
output_message=$(! kubectl get pod valid-pod --allow-missing-template-keys=false -o jsonpath='{.missing}' "${kube_flags[@]}")
|
||||
kube::test::if_has_string "${output_message}" 'missing is not found'
|
||||
|
|
|
@ -212,8 +212,13 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
|
|||
o.ServerPrint = false
|
||||
}
|
||||
|
||||
templateArg := ""
|
||||
if o.PrintFlags.TemplateFlags != nil && o.PrintFlags.TemplateFlags.TemplateArgument != nil {
|
||||
templateArg = *o.PrintFlags.TemplateFlags.TemplateArgument
|
||||
}
|
||||
|
||||
// human readable printers have special conversion rules, so we determine if we're using one.
|
||||
if len(*o.PrintFlags.OutputFormat) == 0 || *o.PrintFlags.OutputFormat == "wide" {
|
||||
if (len(*o.PrintFlags.OutputFormat) == 0 && len(templateArg) == 0) || *o.PrintFlags.OutputFormat == "wide" {
|
||||
o.IsHumanReadablePrinter = true
|
||||
}
|
||||
|
||||
|
|
|
@ -33,9 +33,9 @@ import (
|
|||
type PrintFlags struct {
|
||||
JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags
|
||||
NamePrintFlags *genericclioptions.NamePrintFlags
|
||||
TemplateFlags *printers.KubeTemplatePrintFlags
|
||||
CustomColumnsFlags *printers.CustomColumnsPrintFlags
|
||||
HumanReadableFlags *HumanPrintFlags
|
||||
TemplateFlags *printers.KubeTemplatePrintFlags
|
||||
|
||||
NoHeaders *bool
|
||||
OutputFormat *string
|
||||
|
@ -117,6 +117,15 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
|
|||
f.HumanReadableFlags.NoHeaders = noHeaders
|
||||
f.CustomColumnsFlags.NoHeaders = noHeaders
|
||||
|
||||
// for "get.go" we want to support a --template argument given, even when no --output format is provided
|
||||
if f.TemplateFlags.TemplateArgument != nil && len(*f.TemplateFlags.TemplateArgument) > 0 && len(outputFormat) == 0 {
|
||||
outputFormat = "go-template"
|
||||
}
|
||||
|
||||
if p, err := f.TemplateFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return p, err
|
||||
}
|
||||
|
||||
if f.TemplateFlags.TemplateArgument != nil {
|
||||
f.CustomColumnsFlags.TemplateArgument = *f.TemplateFlags.TemplateArgument
|
||||
}
|
||||
|
@ -129,10 +138,6 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
|
|||
return p, err
|
||||
}
|
||||
|
||||
if p, err := f.TemplateFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return p, err
|
||||
}
|
||||
|
||||
if p, err := f.CustomColumnsFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
|
||||
return p, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue