Skip to content

Commit

Permalink
added test to serverLs, edded config to tabwriter
Browse files Browse the repository at this point in the history
  • Loading branch information
enrichman committed Feb 12, 2024
1 parent 52441bf commit 8349461
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 16 deletions.
14 changes: 10 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ be displayed and one can be selected.
Name: "ls",
Usage: "List all servers",
Action: func(ctx *cli.Context) error {
return serverLs(ctx, cfg)
format := ctx.String("format")
return serverLs(ctx.App.Writer, cfg, format)
},
},
{
Expand Down Expand Up @@ -118,12 +119,17 @@ func serverDelete(cfg *config.Config, serverName string) error {
}

// serverLs command to list rancher servers from the local config
func serverLs(ctx *cli.Context, cfg *config.Config) error {
writer := NewTableWriter([][]string{
func serverLs(out io.Writer, cfg *config.Config, format string) error {
writerConfig := &TableWriterConfig{
Writer: out,
Format: format,
}

writer := NewTableWriterWithConfig([][]string{
{"CURRENT", "Current"},
{"NAME", "Name"},
{"URL", "URL"},
}, ctx)
}, writerConfig)

defer writer.Close()

Expand Down
87 changes: 87 additions & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,93 @@ func TestServerSwitch(t *testing.T) {
}
}

func TestServerLs(t *testing.T) {
tt := []struct {
name string
config *config.Config
format string
expectedOutput string
expectedErr bool
}{
{
name: "list servers",
expectedOutput: `CURRENT NAME URL
* server1 https://myserver-1.com
server2 https://myserver-2.com
server3 https://myserver-3.com
`,
},
{
name: "list empty config",
config: &config.Config{},
format: "",
expectedOutput: "CURRENT NAME URL\n",
},
{
name: "list servers with json format",
format: "json",
expectedOutput: `{"Index":1,"Current":"*","Name":"server1","URL":"https://myserver-1.com"}
{"Index":2,"Current":"","Name":"server2","URL":"https://myserver-2.com"}
{"Index":3,"Current":"","Name":"server3","URL":"https://myserver-3.com"}
`,
},
{
name: "list servers with yaml format",
format: "yaml",
expectedOutput: `Current: '*'
Index: 1
Name: server1
URL: https://myserver-1.com
Current: ""
Index: 2
Name: server2
URL: https://myserver-2.com
Current: ""
Index: 3
Name: server3
URL: https://myserver-3.com
`,
},
{
name: "list servers with custom format",
format: "{{.URL}}",
expectedOutput: `https://myserver-1.com
https://myserver-2.com
https://myserver-3.com
`,
},
{
name: "list servers with custom format",
format: "{{.err}}",
expectedErr: true,
},
}
for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
out := &bytes.Buffer{}

if tc.config == nil {
tc.config = newTestConfig()
}

// do test and check resulting config
err := serverLs(out, tc.config, tc.format)
if tc.expectedErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

assert.Equal(t, tc.expectedOutput, out.String())
})
}
}

func newTestConfig() *config.Config {
return &config.Config{
CurrentServer: "server1",
Expand Down
50 changes: 38 additions & 12 deletions cmd/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"encoding/json"
"io"
"os"
"text/tabwriter"

Expand All @@ -18,27 +19,52 @@ type TableWriter struct {
Writer *tabwriter.Writer
}

type TableWriterConfig struct {
Quiet bool
Format string
Writer io.Writer
}

func NewTableWriter(values [][]string, ctx *cli.Context) *TableWriter {
cfg := &TableWriterConfig{
Writer: os.Stdout,
Quiet: ctx.Bool("quiet"),
Format: ctx.String("format"),
}

return NewTableWriterWithConfig(values, cfg)
}

func NewTableWriterWithConfig(values [][]string, config *TableWriterConfig) *TableWriter {
writer := config.Writer
if writer == nil {
writer = os.Stdout
}

t := &TableWriter{
Writer: tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0),
Writer: tabwriter.NewWriter(writer, 10, 1, 3, ' ', 0),
}
t.HeaderFormat, t.ValueFormat = SimpleFormat(values)

if ctx.Bool("quiet") {
// remove headers if quiet or with a different format
if config.Quiet || config.Format != "" {
t.HeaderFormat = ""
}

// when quiet show only the ID
if config.Quiet {
t.ValueFormat = "{{.ID}}\n"
}

customFormat := ctx.String("format")
if customFormat == "json" {
t.HeaderFormat = ""
t.ValueFormat = "json"
} else if customFormat == "yaml" {
t.HeaderFormat = ""
t.ValueFormat = "yaml"
} else if customFormat != "" {
t.ValueFormat = customFormat + "\n"
t.HeaderFormat = ""
// check for custom formatting
if config.Format != "" {
customFormat := config.Format

// add a newline for other custom formats
if customFormat != "json" && customFormat != "yaml" {
customFormat += "\n"
}
t.ValueFormat = customFormat
}

return t
Expand Down

0 comments on commit 8349461

Please sign in to comment.