Skip to content

Commit

Permalink
move ui table definition to the single place
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Nov 25, 2021
1 parent a8c5807 commit 20e27ea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
14 changes: 4 additions & 10 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package cmd
import (
"errors"
"fmt"
"os"
"strings"

"github.com/mantil-io/mantil/cli/controller"
"github.com/mantil-io/mantil/cli/log"
"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"
)

Expand Down Expand Up @@ -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
}

Expand Down
28 changes: 5 additions & 23 deletions cli/controller/aws.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
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 {
fs, err := newStore()
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
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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()
}
13 changes: 13 additions & 0 deletions cli/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"text/template"
"time"
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
12 changes: 3 additions & 9 deletions cli/controller/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package controller
import (
"errors"
"fmt"
"os"
"strings"

"github.com/manifoldco/promptui"
"github.com/mantil-io/mantil/cli/log"
"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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 20e27ea

Please sign in to comment.