2018-03-29 19:50:09 +00:00
/ *
Copyright 2018 The Kubernetes Authors .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2018-04-19 21:57:09 +00:00
package get
2018-03-29 19:50:09 +00:00
import (
"github.com/spf13/cobra"
2018-08-21 10:46:39 +00:00
"k8s.io/cli-runtime/pkg/genericclioptions"
2018-03-29 19:50:09 +00:00
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/kubectl/scheme"
2018-04-19 21:57:09 +00:00
"k8s.io/kubernetes/pkg/printers"
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
2018-03-29 19:50:09 +00:00
)
// 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 {
ShowKind * bool
ShowLabels * bool
SortBy * string
ColumnLabels * [ ] string
// get.go-specific values
NoHeaders bool
Kind schema . GroupKind
AbsoluteTimestamps bool
WithNamespace bool
}
2018-05-20 05:26:53 +00:00
// SetKind sets the Kind option
func ( f * HumanPrintFlags ) SetKind ( kind schema . GroupKind ) {
2018-04-19 21:57:09 +00:00
f . Kind = kind
2018-05-20 05:26:53 +00:00
}
// EnsureWithKind sets the "Showkind" humanreadable option to true.
func ( f * HumanPrintFlags ) EnsureWithKind ( ) error {
showKind := true
2018-04-19 21:57:09 +00:00
f . ShowKind = & showKind
return nil
}
// EnsureWithNamespace sets the "WithNamespace" humanreadable option to true.
func ( f * HumanPrintFlags ) EnsureWithNamespace ( ) error {
f . WithNamespace = true
return nil
}
2018-10-30 10:35:24 +00:00
// AllowedFormats returns more customized formating options
fix kubectl -o
Fix kubectl -o error message:
Before this change:
```shell
kubectl get pods -o foo
error: unable to match a printer suitable for the output format "" and the options specified: &get.PrintFlags{JSONYamlPrintFlags:(*genericclioptions.JSONYamlPrintFlags)(0x23aa610), NamePrintFlags:(*genericclioptions.NamePrintFlags)(0xc42058b4e0), TemplateFlags:(*printers.KubeTemplatePrintFlags)(0xc4206765e0), CustomColumnsFlags:(*printers.CustomColumnsPrintFlags)(0xc420676620), HumanReadableFlags:(*get.HumanPrintFlags)(0xc4204eb180), NoHeaders:(*bool)(0xc4206fefbc), OutputFormat:(*string)(0xc42058b4d0)}
```
After this change:
```shell
Kubectl get pods -o foo
error: unable to match a printer suitable for the output format "aaa", allowed formats are: custom-columns,custom-columns-file,go-template,go-template-file,json,jsonpath,jsonpath-file,name,template,templatefile,wide,yaml
```
2018-05-27 10:54:44 +00:00
func ( f * HumanPrintFlags ) AllowedFormats ( ) [ ] string {
return [ ] string { "wide" }
}
2018-03-29 19:50:09 +00:00
// ToPrinter receives an outputFormat and returns a printer capable of
// handling human-readable output.
2018-04-19 21:57:09 +00:00
func ( f * HumanPrintFlags ) ToPrinter ( outputFormat string ) ( printers . ResourcePrinter , error ) {
2018-03-29 19:50:09 +00:00
if len ( outputFormat ) > 0 && outputFormat != "wide" {
fix kubectl -o
Fix kubectl -o error message:
Before this change:
```shell
kubectl get pods -o foo
error: unable to match a printer suitable for the output format "" and the options specified: &get.PrintFlags{JSONYamlPrintFlags:(*genericclioptions.JSONYamlPrintFlags)(0x23aa610), NamePrintFlags:(*genericclioptions.NamePrintFlags)(0xc42058b4e0), TemplateFlags:(*printers.KubeTemplatePrintFlags)(0xc4206765e0), CustomColumnsFlags:(*printers.CustomColumnsPrintFlags)(0xc420676620), HumanReadableFlags:(*get.HumanPrintFlags)(0xc4204eb180), NoHeaders:(*bool)(0xc4206fefbc), OutputFormat:(*string)(0xc42058b4d0)}
```
After this change:
```shell
Kubectl get pods -o foo
error: unable to match a printer suitable for the output format "aaa", allowed formats are: custom-columns,custom-columns-file,go-template,go-template-file,json,jsonpath,jsonpath-file,name,template,templatefile,wide,yaml
```
2018-05-27 10:54:44 +00:00
return nil , genericclioptions . NoCompatiblePrinterError { Options : f , AllowedFormats : f . AllowedFormats ( ) }
2018-03-29 19:50:09 +00:00
}
decoder := scheme . Codecs . UniversalDecoder ( )
showKind := false
if f . ShowKind != nil {
showKind = * f . ShowKind
}
showLabels := false
if f . ShowLabels != nil {
showLabels = * f . ShowLabels
}
columnLabels := [ ] string { }
if f . ColumnLabels != nil {
columnLabels = * f . ColumnLabels
}
2018-04-30 18:55:15 +00:00
p := printers . NewHumanReadablePrinter ( decoder , printers . PrintOptions {
2018-03-29 19:50:09 +00:00
Kind : f . Kind ,
WithKind : showKind ,
NoHeaders : f . NoHeaders ,
Wide : outputFormat == "wide" ,
WithNamespace : f . WithNamespace ,
ColumnLabels : columnLabels ,
ShowLabels : showLabels ,
} )
2018-04-19 21:57:09 +00:00
printersinternal . AddHandlers ( p )
2018-03-29 19:50:09 +00:00
// TODO(juanvallejo): handle sorting here
2018-04-19 21:57:09 +00:00
return p , nil
2018-03-29 19:50:09 +00:00
}
// 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 . 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 . SortBy != nil {
c . Flags ( ) . StringVar ( f . SortBy , "sort-by" , * f . SortBy , "If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string." )
}
if f . ColumnLabels != nil {
c . Flags ( ) . StringSliceVarP ( f . ColumnLabels , "label-columns" , "L" , * f . ColumnLabels , "Accepts a comma separated list of labels that are going to be presented as columns. Names are case-sensitive. You can also use multiple flag options like -L label1 -L label2..." )
}
if f . ShowKind != nil {
c . Flags ( ) . BoolVar ( f . ShowKind , "show-kind" , * f . ShowKind , "If present, list the resource type for the requested object(s)." )
}
}
// NewHumanPrintFlags returns flags associated with
// human-readable printing, with default values set.
2018-04-19 21:57:09 +00:00
func NewHumanPrintFlags ( ) * HumanPrintFlags {
2018-03-29 19:50:09 +00:00
showLabels := false
sortBy := ""
showKind := false
columnLabels := [ ] string { }
return & HumanPrintFlags {
2018-04-19 21:57:09 +00:00
NoHeaders : false ,
WithNamespace : false ,
AbsoluteTimestamps : false ,
2018-03-29 19:50:09 +00:00
ColumnLabels : & columnLabels ,
2018-04-19 21:57:09 +00:00
Kind : schema . GroupKind { } ,
2018-03-29 19:50:09 +00:00
ShowLabels : & showLabels ,
SortBy : & sortBy ,
ShowKind : & showKind ,
}
}