From b9c8b76b6b2355001dc1a925263043b84b3c4586 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Tue, 13 Oct 2015 18:48:06 -0700 Subject: [PATCH] address comments --- pkg/kubectl/cmd/get.go | 3 +-- pkg/kubectl/resource_printer.go | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index 536aff97809..88f18fedc14 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -19,7 +19,6 @@ package cmd import ( "fmt" "io" - "text/tabwriter" "github.com/spf13/cobra" "k8s.io/kubernetes/pkg/api/meta" @@ -224,7 +223,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string // use the default printer for each object printer = nil var lastMapping *meta.RESTMapping - w := tabwriter.NewWriter(out, kubectl.TabwriterMinWidth, kubectl.TabwriterWidth, kubectl.TabwriterPadding, kubectl.TabwriterPadChar, kubectl.TabwriterFlags) + w := kubectl.GetNewTabWriter(out) defer w.Flush() return b.Flatten().Do().Visit(func(r *resource.Info, err error) error { if err != nil { diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 75d2b1716e1..3937cd9cf7f 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -46,11 +46,11 @@ import ( ) const ( - TabwriterMinWidth = 10 - TabwriterWidth = 4 - TabwriterPadding = 3 - TabwriterPadChar = ' ' - TabwriterFlags = 0 + tabwriterMinWidth = 10 + tabwriterWidth = 4 + tabwriterPadding = 3 + tabwriterPadChar = ' ' + tabwriterFlags = 0 ) // GetPrinter takes a format type, an optional format argument. It will return true @@ -1475,11 +1475,17 @@ func formatWideHeaders(wide bool, t reflect.Type) []string { return nil } +// GetNewTabWriter returns a tabwriter that translates tabbed columns in input into properly aligned text. +func GetNewTabWriter(output io.Writer) *tabwriter.Writer { + return tabwriter.NewWriter(output, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags) +} + // PrintObj prints the obj in a human-friendly format according to the type of the obj. func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) error { + // if output is a tabwriter (when it's called by kubectl get), we use it; create a new tabwriter otherwise w, found := output.(*tabwriter.Writer) if !found { - w = tabwriter.NewWriter(output, TabwriterMinWidth, TabwriterWidth, TabwriterPadding, TabwriterPadChar, TabwriterFlags) + w = GetNewTabWriter(output) defer w.Flush() } t := reflect.TypeOf(obj)