Move tabwriter to package util

This offloads the "magic numbers" that are used when creating a
tabwriter
This commit is contained in:
Ian Howell 2019-05-23 15:24:56 -05:00
parent 33fecc360b
commit be2a61047c
2 changed files with 21 additions and 2 deletions

View File

@ -3,11 +3,11 @@ package workflow
import ( import (
"fmt" "fmt"
"io" "io"
"text/tabwriter"
"github.com/spf13/cobra" "github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/ian-howell/airshipctl/pkg/util"
"github.com/ian-howell/airshipctl/pkg/environment" "github.com/ian-howell/airshipctl/pkg/environment"
wfenv "github.com/ian-howell/airshipctl/pkg/workflow/environment" wfenv "github.com/ian-howell/airshipctl/pkg/workflow/environment"
) )
@ -29,7 +29,7 @@ func NewWorkflowListCommand(out io.Writer, rootSettings *environment.AirshipCTLS
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
} }
w := tabwriter.NewWriter(out, 0, 0, 5, ' ', 0) w := util.NewTabWriter(out)
defer w.Flush() defer w.Flush()
fmt.Fprintf(w, "%s\t%s\n", "NAME", "PHASE") fmt.Fprintf(w, "%s\t%s\n", "NAME", "PHASE")
for _, wf := range wflist.Items { for _, wf := range wflist.Items {

19
pkg/util/tabwriter.go Normal file
View File

@ -0,0 +1,19 @@
package util
import (
"io"
"text/tabwriter"
)
const (
tabwriterMinWidth = 6
tabwriterWidth = 4
tabwriterPadding = 3
tabwriterPadChar = ' '
tabwriterFlags = 0
)
// NewTabWriter returns a tabwriter that translates tabbed columns in input into properly aligned text.
func NewTabWriter(out io.Writer) *tabwriter.Writer {
return tabwriter.NewWriter(out, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags)
}