From 20e27ea9f9a103c5d3f104ede2239284cfa12334 Mon Sep 17 00:00:00 2001 From: Igor Anic Date: Thu, 25 Nov 2021 20:26:34 +0100 Subject: [PATCH] move ui table definition to the single place --- cli/cmd/root.go | 14 ++++---------- cli/controller/aws.go | 28 +++++----------------------- cli/controller/controller.go | 13 +++++++++++++ cli/controller/stage.go | 12 +++--------- 4 files changed, 25 insertions(+), 42 deletions(-) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index ba781225..9761c5a6 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -3,7 +3,6 @@ package cmd import ( "errors" "fmt" - "os" "strings" "github.com/mantil-io/mantil/cli/controller" @@ -11,7 +10,6 @@ import ( "github.com/mantil-io/mantil/cli/ui" "github.com/mantil-io/mantil/domain" "github.com/mantil-io/mantil/texts" - "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" ) @@ -219,16 +217,12 @@ Please check the following rules when naming projects, stages and functions: ui.Info("You can create new project with 'mantil new'.") return } - ui.Info("") - ui.Info("Current projects with active stages:") - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"name", "path"}) - table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) - table.SetCenterSeparator("|") + ui.Info("\nCurrent projects with active stages:") + var data [][]string for _, p := range store.Workspace().Projects { - table.Append([]string{p.Name, p.Path}) + data = append(data, []string{p.Name, p.Path}) } - table.Render() + controller.ShowTable([]string{"name", "path"}, data) return } diff --git a/cli/controller/aws.go b/cli/controller/aws.go index cb1f292f..3a5fbc47 100644 --- a/cli/controller/aws.go +++ b/cli/controller/aws.go @@ -1,13 +1,11 @@ package controller import ( - "os" "sort" "github.com/mantil-io/mantil/cli/log" "github.com/mantil-io/mantil/cli/ui" "github.com/mantil-io/mantil/domain" - "github.com/olekukonko/tablewriter" ) func Nodes() error { @@ -15,19 +13,14 @@ func Nodes() error { if err != nil { return log.Wrap(err) } - if len(fs.Workspace().Nodes) == 0 { return log.Wrap(&domain.WorkspaceNoNodesError{}) } - - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"name", "AWS Account", "AWS Region", "ID"}) - table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) - table.SetCenterSeparator("|") + var data [][]string for _, n := range fs.Workspace().Nodes { - table.Append([]string{n.Name, n.AccountID, n.Region, n.ID}) + data = append(data, []string{n.Name, n.AccountID, n.Region, n.ID}) } - table.Render() + ShowTable([]string{"name", "AWS Account", "AWS Region", "ID"}, data) return nil } @@ -110,7 +103,7 @@ func (a *AwsResources) showResourcesTable(rs []domain.AwsResource) { for _, rs := range rs { data = append(data, []string{rs.Name, rs.Type, rs.AWSName, rs.LogGroup()}) } - showTable([]string{"name", "type", "AWS resource name", "cloudwatch log group"}, data) + ShowTable([]string{"name", "type", "AWS resource name", "cloudwatch log group"}, data) } func (a *AwsResources) showTagsTable(tgs map[string]string) { @@ -121,7 +114,7 @@ func (a *AwsResources) showTagsTable(tgs map[string]string) { sort.Slice(tags, func(i, j int) bool { return tags[i][0] < tags[j][0] }) - showTable([]string{"key", "value"}, tags) + ShowTable([]string{"key", "value"}, tags) } func (a *AwsResources) node(n *domain.Node) { @@ -131,14 +124,3 @@ func (a *AwsResources) node(n *domain.Node) { ui.Info("Tags:") a.showTagsTable(n.ResourceTags()) } - -func showTable(header []string, data [][]string) { - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader(header) - table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) - table.SetCenterSeparator("|") - for _, row := range data { - table.Append(row) - } - table.Render() -} diff --git a/cli/controller/controller.go b/cli/controller/controller.go index 0e90e159..16ac6ca5 100644 --- a/cli/controller/controller.go +++ b/cli/controller/controller.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "net/url" + "os" "strings" "text/template" "time" @@ -17,6 +18,7 @@ import ( "github.com/mantil-io/mantil/cli/ui" "github.com/mantil-io/mantil/domain" "github.com/mantil-io/mantil/node/dto" + "github.com/olekukonko/tablewriter" ) // package defers @@ -178,3 +180,14 @@ func timerFn() func() int { return dur } } + +func ShowTable(header []string, data [][]string) { + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader(header) + table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) + table.SetCenterSeparator("|") + for _, row := range data { + table.Append(row) + } + table.Render() +} diff --git a/cli/controller/stage.go b/cli/controller/stage.go index da01f2c5..080c089f 100644 --- a/cli/controller/stage.go +++ b/cli/controller/stage.go @@ -3,7 +3,6 @@ package controller import ( "errors" "fmt" - "os" "strings" "github.com/manifoldco/promptui" @@ -11,7 +10,6 @@ import ( "github.com/mantil-io/mantil/cli/ui" "github.com/mantil-io/mantil/domain" "github.com/mantil-io/mantil/node/dto" - "github.com/olekukonko/tablewriter" ) const DestroyHTTPMethod = "destroy" @@ -262,20 +260,16 @@ func (s *Stage) destroyRequest(stage *domain.Stage) error { func (s *Stage) List() error { if len(s.project.Stages) == 0 { return log.Wrap(&domain.ProjectNoStagesError{}) - return nil } - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"default", "name", "node", "endpoint"}) - table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) - table.SetCenterSeparator("|") + var data [][]string for _, ps := range s.project.Stages { def := " " if ps.Default { def = "*" } - table.Append([]string{def, ps.Name, ps.NodeName, ps.Endpoints.Rest}) + data = append(data, []string{def, ps.Name, ps.NodeName, ps.Endpoints.Rest}) } - table.Render() + ShowTable([]string{"default", "name", "node", "endpoint"}, data) return nil }