move template printers to genericclioptions

This commit is contained in:
David Eads 2018-07-02 10:00:16 -04:00
parent 09784ccf83
commit 52d45cfd4f
13 changed files with 42 additions and 46 deletions

View File

@ -122,12 +122,12 @@ func TestIllegalPackageSourceCheckerThroughPrintFlags(t *testing.T) {
} }
func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) { func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) {
jsonPathPrinter, err := printers.NewJSONPathPrinter("{ .metadata.name }") jsonPathPrinter, err := genericprinters.NewJSONPathPrinter("{ .metadata.name }")
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
goTemplatePrinter, err := printers.NewGoTemplatePrinter([]byte("{{ .metadata.name }}")) goTemplatePrinter, err := genericprinters.NewGoTemplatePrinter([]byte("{{ .metadata.name }}"))
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@ -140,7 +140,7 @@ func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
expectInternalObjErr bool expectInternalObjErr bool
printer printers.ResourcePrinter printer genericprinters.ResourcePrinter
obj runtime.Object obj runtime.Object
expectedOutput string expectedOutput string
}{ }{

View File

@ -31,7 +31,7 @@ import (
type kubectlConfigPrintFlags struct { type kubectlConfigPrintFlags struct {
JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags
NamePrintFlags *genericclioptions.NamePrintFlags NamePrintFlags *genericclioptions.NamePrintFlags
TemplateFlags *printers.KubeTemplatePrintFlags TemplateFlags *genericclioptions.KubeTemplatePrintFlags
TypeSetter *genericprinters.TypeSetterPrinter TypeSetter *genericprinters.TypeSetterPrinter
@ -94,7 +94,7 @@ func newKubeConfigPrintFlags(scheme runtime.ObjectTyper) *kubectlConfigPrintFlag
JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
NamePrintFlags: genericclioptions.NewNamePrintFlags(""), NamePrintFlags: genericclioptions.NewNamePrintFlags(""),
TemplateFlags: printers.NewKubeTemplatePrintFlags(), TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(),
TypeSetter: genericprinters.NewTypeSetter(scheme), TypeSetter: genericprinters.NewTypeSetter(scheme),
} }

View File

@ -31,7 +31,7 @@ import (
type PrintFlags struct { type PrintFlags struct {
JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags
NamePrintFlags *genericclioptions.NamePrintFlags NamePrintFlags *genericclioptions.NamePrintFlags
TemplateFlags *printers.KubeTemplatePrintFlags TemplateFlags *genericclioptions.KubeTemplatePrintFlags
TypeSetter *genericprinters.TypeSetterPrinter TypeSetter *genericprinters.TypeSetterPrinter
@ -86,7 +86,7 @@ func NewPrintFlags(operation string, scheme runtime.ObjectTyper) *PrintFlags {
JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
NamePrintFlags: genericclioptions.NewNamePrintFlags(operation), NamePrintFlags: genericclioptions.NewNamePrintFlags(operation),
TemplateFlags: printers.NewKubeTemplatePrintFlags(), TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(),
TypeSetter: genericprinters.NewTypeSetter(scheme), TypeSetter: genericprinters.NewTypeSetter(scheme),
} }

View File

@ -35,7 +35,7 @@ type PrintFlags struct {
NamePrintFlags *genericclioptions.NamePrintFlags NamePrintFlags *genericclioptions.NamePrintFlags
CustomColumnsFlags *printers.CustomColumnsPrintFlags CustomColumnsFlags *printers.CustomColumnsPrintFlags
HumanReadableFlags *HumanPrintFlags HumanReadableFlags *HumanPrintFlags
TemplateFlags *printers.KubeTemplatePrintFlags TemplateFlags *genericclioptions.KubeTemplatePrintFlags
NoHeaders *bool NoHeaders *bool
OutputFormat *string OutputFormat *string
@ -182,7 +182,7 @@ func NewGetPrintFlags() *PrintFlags {
JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(), JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
NamePrintFlags: genericclioptions.NewNamePrintFlags(""), NamePrintFlags: genericclioptions.NewNamePrintFlags(""),
TemplateFlags: printers.NewKubeTemplatePrintFlags(), TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(),
HumanReadableFlags: NewHumanPrintFlags(), HumanReadableFlags: NewHumanPrintFlags(),
CustomColumnsFlags: printers.NewCustomColumnsPrintFlags(), CustomColumnsFlags: printers.NewCustomColumnsPrintFlags(),

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package printers package genericclioptions
import ( import (
"fmt" "fmt"
@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions" "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
) )
// templates are logically optional for specifying a format. // templates are logically optional for specifying a format.
@ -55,9 +55,9 @@ func (f *JSONPathPrintFlags) AllowedFormats() []string {
// ToPrinter receives an templateFormat and returns a printer capable of // ToPrinter receives an templateFormat and returns a printer capable of
// handling --template format printing. // handling --template format printing.
// Returns false if the specified templateFormat does not match a template format. // Returns false if the specified templateFormat does not match a template format.
func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) { func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error) {
if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 { if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 {
return nil, genericclioptions.NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
} }
templateValue := "" templateValue := ""
@ -76,7 +76,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
} }
if _, supportedFormat := jsonFormats[templateFormat]; !supportedFormat { if _, supportedFormat := jsonFormats[templateFormat]; !supportedFormat {
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()} return nil, NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
} }
if len(templateValue) == 0 { if len(templateValue) == 0 {
@ -92,7 +92,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter,
templateValue = string(data) templateValue = string(data)
} }
p, err := NewJSONPathPrinter(templateValue) p, err := printers.NewJSONPathPrinter(templateValue)
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing jsonpath %s, %v\n", templateValue, err) return nil, fmt.Errorf("error parsing jsonpath %s, %v\n", templateValue, err)
} }

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package printers package genericclioptions
import ( import (
"bytes" "bytes"
@ -26,7 +26,6 @@ import (
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
) )
func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) { func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) {
@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) {
p, err := printFlags.ToPrinter(tc.outputFormat) p, err := printFlags.ToPrinter(tc.outputFormat)
if tc.expectNoMatch { if tc.expectNoMatch {
if !genericclioptions.IsNoCompatiblePrinterError(err) { if !IsNoCompatiblePrinterError(err) {
t.Fatalf("expected no printer matches for output format %q", tc.outputFormat) t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
} }
return return
} }
if genericclioptions.IsNoCompatiblePrinterError(err) { if IsNoCompatiblePrinterError(err) {
t.Fatalf("expected to match template printer for output format %q", tc.outputFormat) t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
} }
@ -184,7 +183,7 @@ func TestJSONPathPrinterDefaultsAllowMissingKeysToTrue(t *testing.T) {
outputFormat := "jsonpath" outputFormat := "jsonpath"
p, err := printFlags.ToPrinter(outputFormat) p, err := printFlags.ToPrinter(outputFormat)
if genericclioptions.IsNoCompatiblePrinterError(err) { if IsNoCompatiblePrinterError(err) {
t.Fatalf("expected to match template printer for output format %q", outputFormat) t.Fatalf("expected to match template printer for output format %q", outputFormat)
} }
if err != nil { if err != nil {

View File

@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package printers package genericclioptions
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions" "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
) )
// KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer. // KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer.
@ -37,8 +37,8 @@ func (f *KubeTemplatePrintFlags) AllowedFormats() []string {
return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...) return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...)
} }
func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (ResourcePrinter, error) { func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) { if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
return p, err return p, err
} }
return f.GoTemplatePrintFlags.ToPrinter(outputFormat) return f.GoTemplatePrintFlags.ToPrinter(outputFormat)

View File

@ -24,7 +24,6 @@ import (
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/jsonpath" "k8s.io/client-go/util/jsonpath"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
) )
// exists returns true if it would be possible to call the index function // exists returns true if it would be possible to call the index function
@ -118,8 +117,8 @@ func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
// we use reflect.Indirect here in order to obtain the actual value from a pointer. // we use reflect.Indirect here in order to obtain the actual value from a pointer.
// we need an actual value in order to retrieve the package path for an object. // we need an actual value in order to retrieve the package path for an object.
// using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers. // using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers.
if printers.InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) {
return fmt.Errorf(printers.InternalObjectPrinterErr) return fmt.Errorf(InternalObjectPrinterErr)
} }
var queryObj interface{} = obj var queryObj interface{} = obj

View File

@ -25,7 +25,6 @@ import (
"text/template" "text/template"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
) )
// GoTemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template. // GoTemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
@ -61,8 +60,8 @@ func (p *GoTemplatePrinter) AllowMissingKeys(allow bool) {
// PrintObj formats the obj with the Go Template. // PrintObj formats the obj with the Go Template.
func (p *GoTemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error { func (p *GoTemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
if printers.InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) {
return fmt.Errorf(printers.InternalObjectPrinterErr) return fmt.Errorf(InternalObjectPrinterErr)
} }
var data []byte var data []byte

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package printers package genericclioptions
import ( import (
"fmt" "fmt"
@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions" "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
) )
// templates are logically optional for specifying a format. // templates are logically optional for specifying a format.
@ -57,9 +57,9 @@ func (f *GoTemplatePrintFlags) AllowedFormats() []string {
// ToPrinter receives an templateFormat and returns a printer capable of // ToPrinter receives an templateFormat and returns a printer capable of
// handling --template format printing. // handling --template format printing.
// Returns false if the specified templateFormat does not match a template format. // Returns false if the specified templateFormat does not match a template format.
func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) { func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error) {
if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 { if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 {
return nil, genericclioptions.NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat} return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat}
} }
templateValue := "" templateValue := ""
@ -78,7 +78,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
} }
if _, supportedFormat := templateFormats[templateFormat]; !supportedFormat { if _, supportedFormat := templateFormats[templateFormat]; !supportedFormat {
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()} return nil, NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
} }
if len(templateValue) == 0 { if len(templateValue) == 0 {
@ -94,7 +94,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter
templateValue = string(data) templateValue = string(data)
} }
p, err := NewGoTemplatePrinter([]byte(templateValue)) p, err := printers.NewGoTemplatePrinter([]byte(templateValue))
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing template %s, %v\n", templateValue, err) return nil, fmt.Errorf("error parsing template %s, %v\n", templateValue, err)
} }

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package printers package genericclioptions
import ( import (
"bytes" "bytes"
@ -26,7 +26,6 @@ import (
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
) )
func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) { func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) {
@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) {
p, err := printFlags.ToPrinter(tc.outputFormat) p, err := printFlags.ToPrinter(tc.outputFormat)
if tc.expectNoMatch { if tc.expectNoMatch {
if !genericclioptions.IsNoCompatiblePrinterError(err) { if !IsNoCompatiblePrinterError(err) {
t.Fatalf("expected no printer matches for output format %q", tc.outputFormat) t.Fatalf("expected no printer matches for output format %q", tc.outputFormat)
} }
return return
} }
if genericclioptions.IsNoCompatiblePrinterError(err) { if IsNoCompatiblePrinterError(err) {
t.Fatalf("expected to match template printer for output format %q", tc.outputFormat) t.Fatalf("expected to match template printer for output format %q", tc.outputFormat)
} }
@ -178,7 +177,7 @@ func TestTemplatePrinterDefaultsAllowMissingKeysToTrue(t *testing.T) {
outputFormat := "template" outputFormat := "template"
p, err := printFlags.ToPrinter(outputFormat) p, err := printFlags.ToPrinter(outputFormat)
if genericclioptions.IsNoCompatiblePrinterError(err) { if IsNoCompatiblePrinterError(err) {
t.Fatalf("expected to match template printer for output format %q", outputFormat) t.Fatalf("expected to match template printer for output format %q", outputFormat)
} }
if err != nil { if err != nil {

View File

@ -338,7 +338,7 @@ func TestUnknownTypePrinting(t *testing.T) {
func TestTemplatePanic(t *testing.T) { func TestTemplatePanic(t *testing.T) {
tmpl := `{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}` tmpl := `{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
printer, err := printers.NewGoTemplatePrinter([]byte(tmpl)) printer, err := genericprinters.NewGoTemplatePrinter([]byte(tmpl))
if err != nil { if err != nil {
t.Fatalf("tmpl fail: %v", err) t.Fatalf("tmpl fail: %v", err)
} }
@ -503,7 +503,7 @@ func TestTemplateStrings(t *testing.T) {
} }
// The point of this test is to verify that the below template works. // The point of this test is to verify that the below template works.
tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}` tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}`
printer, err := printers.NewGoTemplatePrinter([]byte(tmpl)) printer, err := genericprinters.NewGoTemplatePrinter([]byte(tmpl))
if err != nil { if err != nil {
t.Fatalf("tmpl fail: %v", err) t.Fatalf("tmpl fail: %v", err)
} }
@ -535,17 +535,17 @@ func TestPrinters(t *testing.T) {
jsonpathPrinter printers.ResourcePrinter jsonpathPrinter printers.ResourcePrinter
) )
templatePrinter, err = printers.NewGoTemplatePrinter([]byte("{{.name}}")) templatePrinter, err = genericprinters.NewGoTemplatePrinter([]byte("{{.name}}"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
templatePrinter2, err = printers.NewGoTemplatePrinter([]byte("{{len .items}}")) templatePrinter2, err = genericprinters.NewGoTemplatePrinter([]byte("{{len .items}}"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
jsonpathPrinter, err = printers.NewJSONPathPrinter("{.metadata.name}") jsonpathPrinter, err = genericprinters.NewJSONPathPrinter("{.metadata.name}")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }