From 10e75b55808f89c83e1123cdd76a6a9dce169b77 Mon Sep 17 00:00:00 2001 From: Pallav Gupta Date: Wed, 14 Apr 2021 16:45:28 -0700 Subject: [PATCH] Fix plan list command The aim of this PS to fix current issue we have when we have new plan created with description width more than 40 char. Added condition if width is more than specified in command so the command execution will not break and just crop exceeding description length. Eg: https://github.com/kubernetes-sigs/cli-utils/blob/e351b2bc43cec2107ba1d874c3dec54fd0956c59/pkg/print/table/columndefs.go#L71 Change-Id: Icc353ec688b2fbb563add39f8d38004008093895 --- pkg/phase/command.go | 11 ++++++++--- pkg/phase/command_test.go | 15 +++++++++++---- pkg/phase/printers.go | 23 +++++++++++++++++++---- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/pkg/phase/command.go b/pkg/phase/command.go index be2c981df..9efae238d 100644 --- a/pkg/phase/command.go +++ b/pkg/phase/command.go @@ -166,7 +166,7 @@ type PlanListCommand struct { Writer io.Writer } -// RunE runs a phase plan command +// RunE runs a plan list command func (c *PlanListCommand) RunE() error { cfg, err := c.Factory() if err != nil { @@ -192,7 +192,7 @@ func (c *PlanListCommand) RunE() error { descriptionCol := table.ColumnDef{ ColumnName: "description", ColumnHeader: "DESCRIPTION", - ColumnWidth: 40, + ColumnWidth: 200, PrintResourceFunc: func(w io.Writer, width int, r table.Resource) (int, error) { rs := r.ResourceStatus() if rs == nil { @@ -203,7 +203,12 @@ func (c *PlanListCommand) RunE() error { if err != nil { return 0, err } - return fmt.Fprint(w, plan.Description) + txt := plan.Description + if len(txt) > width { + txt = txt[:width] + } + _, err = fmt.Fprint(w, txt) + return len(txt), err }, } printer.Columns = append(printer.Columns, descriptionCol) diff --git a/pkg/phase/command_test.go b/pkg/phase/command_test.go index 10335b984..352936f47 100644 --- a/pkg/phase/command_test.go +++ b/pkg/phase/command_test.go @@ -111,9 +111,11 @@ func TestRunCommand(t *testing.T) { func TestListCommand(t *testing.T) { outputString1 := "NAMESPACE RESOURCE CLUSTER " + - "NAME EXECUTOR DOC ENTRYPOINT " + "NAME EXECUTOR DOC ENTRYPOINT " + + " " outputString2 := " Phase/phase ephemeral" + - "-cluster KubernetesApply ephemeral/phase " + "-cluster KubernetesApply ephemeral/phase " + + " " yamlOutput := `--- - apiVersion: airshipit.org/v1alpha1 config: @@ -348,8 +350,13 @@ func TestPlanListCommand(t *testing.T) { return conf, nil }, expectedOut: [][]byte{ - []byte("NAMESPACE RESOURCE DESCRIPTION "), - []byte(" PhasePlan/phasePlan Default phase plan "), + []byte("NAMESPACE RESOURCE DESCRIPTION " + + " " + + " "), + []byte(" PhasePlan/phasePlan Default phase plan" + + " " + + " " + + " "), {}, }, }, diff --git a/pkg/phase/printers.go b/pkg/phase/printers.go index 1a3a5712d..3e5352b2a 100644 --- a/pkg/phase/printers.go +++ b/pkg/phase/printers.go @@ -44,7 +44,12 @@ func PrintPhaseListTable(w io.Writer, phases []*v1alpha1.Phase) error { if err != nil { return 0, nil } - return fmt.Fprintf(w, phase.ClusterName) + txt := phase.ClusterName + if len(txt) > width { + txt = txt[:width] + } + _, err = fmt.Fprintf(w, txt) + return len(txt), err }, } executorrefkindCol := table.ColumnDef{ @@ -57,20 +62,30 @@ func PrintPhaseListTable(w io.Writer, phases []*v1alpha1.Phase) error { if err != nil { return 0, nil } - return fmt.Fprintf(w, phase.Config.ExecutorRef.Kind) + txt := phase.Config.ExecutorRef.Kind + if len(txt) > width { + txt = txt[:width] + } + _, err = fmt.Fprintf(w, txt) + return len(txt), err }, } docentrypointCol := table.ColumnDef{ ColumnName: "docentrypoint", ColumnHeader: "DOC ENTRYPOINT", - ColumnWidth: 40, + ColumnWidth: 100, PrintResourceFunc: func(w io.Writer, width int, r table.Resource) (int, error) { phase, err := phaseFromResource(r) if err != nil { return 0, nil } - return fmt.Fprintf(w, phase.Config.DocumentEntryPoint) + txt := phase.Config.DocumentEntryPoint + if len(txt) > width { + txt = txt[:width] + } + _, err = fmt.Fprintf(w, txt) + return len(txt), err }, } printer.Columns = append(printer.Columns, clusternameCol, executorrefkindCol, docentrypointCol)