diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-13 18:23:48 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-13 18:23:48 -0500 |
| commit | 00aa327eb204d504f1e03dbe5a7cdec4af4ec913 (patch) | |
| tree | 571a5bc5ce52a11e0039f9763c89b9316e7bf9d4 /internal/output | |
| parent | Tweak readme (diff) | |
| download | cli-00aa327eb204d504f1e03dbe5a7cdec4af4ec913.zip | |
Rework docs, add more output, and tweak tests
Diffstat (limited to 'internal/output')
| -rw-r--r-- | internal/output/console.go | 60 | ||||
| -rw-r--r-- | internal/output/console_test.go | 76 | ||||
| -rw-r--r-- | internal/output/output.go | 7 |
3 files changed, 143 insertions, 0 deletions
diff --git a/internal/output/console.go b/internal/output/console.go new file mode 100644 index 0000000..d2946ed --- /dev/null +++ b/internal/output/console.go @@ -0,0 +1,60 @@ +package output + +import ( + "fmt" + "io" + "os" + "text/tabwriter" +) + +type ConsoleOutputter struct { + stdout io.Writer + stderr io.Writer +} + +func NewConsoleOutputter() ConsoleOutputter { + return ConsoleOutputter{stdout: os.Stdout, stderr: os.Stderr} +} + +func (c ConsoleOutputter) Output(message string) error { + _, err := fmt.Fprintln(c.stdout, message) + return err +} + +func (c ConsoleOutputter) Error(message string) error { + _, err := fmt.Fprintln(c.stderr, message) + return err +} + +func (c ConsoleOutputter) Table(headers []string, rows [][]string) error { + w := tabwriter.NewWriter(c.stdout, 4, 4, 4, ' ', 0) + + for _, header := range headers { + _, err := fmt.Fprintf(w, "%s\t", header) + if err != nil { + return err + } + } + if len(headers) > 0 { + _, err := fmt.Fprint(w, "\n") + if err != nil { + return err + } + } + + for _, row := range rows { + for _, cell := range row { + _, err := fmt.Fprintf(w, "%s\t", cell) + if err != nil { + return err + } + } + _, err := fmt.Fprint(w, "\n") + if err != nil { + return err + } + } + + w.Flush() + return nil +} diff --git a/internal/output/console_test.go b/internal/output/console_test.go new file mode 100644 index 0000000..be369ab --- /dev/null +++ b/internal/output/console_test.go @@ -0,0 +1,76 @@ +package output + +import ( + "bytes" + "io" + "testing" + + "gotest.tools/v3/assert" +) + +func NewConsoleOutputterWithWriters(stdout io.Writer, stderr io.Writer) ConsoleOutputter { + return ConsoleOutputter{stdout: stdout, stderr: stderr} +} + +func TestConsoleOutputter_Output(t *testing.T) { + var stdout bytes.Buffer + var stderr bytes.Buffer + consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr) + + consoleOutputter.Output("test string") + + assert.Equal(t, stdout.String(), "test string\n") + assert.Equal(t, stderr.String(), "") +} + +func TestConsoleOutputter_Error(t *testing.T) { + var stdout bytes.Buffer + var stderr bytes.Buffer + consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr) + + consoleOutputter.Error("test string") + + assert.Equal(t, stdout.String(), "") + assert.Equal(t, stderr.String(), "test string\n") +} + +func TestConsoleOutputter_Table(t *testing.T) { + type test struct { + headers []string + rows [][]string + expectedStdout string + } + + tests := []test{ + { + headers: []string{}, + rows: make([][]string, 0), + expectedStdout: "", + }, + { + headers: []string{"Version"}, + rows: [][]string{{"test1"}, {"test2"}}, + expectedStdout: "Version \n" + + "test1 \n" + + "test2 \n", + }, + { + headers: []string{"Version", "Message"}, + rows: [][]string{{"v1", "m1"}, {"v1", "m2"}}, + expectedStdout: "Version Message \n" + + "v1 m1 \n" + + "v1 m2 \n", + }, + } + + for _, tc := range tests { + var stdout bytes.Buffer + var stderr bytes.Buffer + consoleOutputter := NewConsoleOutputterWithWriters(&stdout, &stderr) + + consoleOutputter.Table(tc.headers, tc.rows) + + assert.Equal(t, stdout.String(), tc.expectedStdout) + assert.Equal(t, stderr.String(), "") + } +} diff --git a/internal/output/output.go b/internal/output/output.go new file mode 100644 index 0000000..c51553b --- /dev/null +++ b/internal/output/output.go @@ -0,0 +1,7 @@ +package output + +type Outputter interface { + Output(message string) error + Error(message string) error + Table(header []string, rows [][]string) error +} |