summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsh McKenzie <amckenzie@gitlab.com>2019-09-11 23:00:48 +1000
committerIgor Drozdov <idrozdov@gitlab.com>2019-10-23 11:07:22 +0300
commit412ed17cc66d876c46ee8df3767066ca0e676f28 (patch)
tree1cfc7c78ada4933ce2c458a8d7404cc41c39b846
parent629e3bf9c31687f7b824cf29ba07ad2ce402e280 (diff)
downloadgitlab-shell-412ed17cc66d876c46ee8df3767066ca0e676f28.tar.gz
New console package for writing to the console
-rw-r--r--internal/console/console.go73
-rw-r--r--internal/console/console_test.go201
2 files changed, 274 insertions, 0 deletions
diff --git a/internal/console/console.go b/internal/console/console.go
new file mode 100644
index 0000000..72f80cc
--- /dev/null
+++ b/internal/console/console.go
@@ -0,0 +1,73 @@
+package console
+
+import (
+ "fmt"
+ "io"
+ "strings"
+)
+
+func DisplayWarningMessage(message string, out io.Writer) {
+ DisplayWarningMessages([]string{message}, out)
+}
+
+func DisplayInfoMessage(message string, out io.Writer) {
+ DisplayInfoMessages([]string{message}, out)
+}
+
+func DisplayWarningMessages(messages []string, out io.Writer) {
+ DisplayMessages(messages, out, true)
+}
+
+func DisplayInfoMessages(messages []string, out io.Writer) {
+ DisplayMessages(messages, out, false)
+}
+
+func DisplayMessages(messages []string, out io.Writer, displayDivider bool) {
+ if noMessages(messages) {
+ return
+ }
+
+ displayBlankLineOrDivider(out, displayDivider)
+
+ for _, msg := range messages {
+ fmt.Fprintf(out, formatLine(msg))
+ }
+
+ displayBlankLineOrDivider(out, displayDivider)
+}
+
+func noMessages(messages []string) bool {
+ if len(messages) == 0 {
+ return true
+ }
+
+ for _, msg := range messages {
+ if len(strings.TrimSpace(msg)) > 0 {
+ return false
+ }
+ }
+
+ return true
+}
+
+func formatLine(message string) string {
+ return fmt.Sprintf("remote: %v\n", message)
+}
+
+func displayBlankLineOrDivider(out io.Writer, displayDivider bool) {
+ if displayDivider {
+ fmt.Fprintf(out, divider())
+ } else {
+ fmt.Fprintf(out, blankLine())
+ }
+}
+
+func blankLine() string {
+ return formatLine("")
+}
+
+func divider() string {
+ ruler := strings.Repeat("=", 72)
+
+ return fmt.Sprintf("%v%v%v", blankLine(), formatLine(ruler), blankLine())
+}
diff --git a/internal/console/console_test.go b/internal/console/console_test.go
new file mode 100644
index 0000000..bcd8444
--- /dev/null
+++ b/internal/console/console_test.go
@@ -0,0 +1,201 @@
+package console
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestDisplayWarningMessage(t *testing.T) {
+ tests := []struct {
+ name string
+ message string
+ wantOut string
+ }{
+ {
+ name: "empty",
+ message: "",
+ wantOut: "",
+ },
+ {
+ name: "basically empty",
+ message: " ",
+ wantOut: "",
+ },
+ {
+ name: "something",
+ message: "something",
+ wantOut: `remote:
+remote: ========================================================================
+remote:
+remote: something
+remote:
+remote: ========================================================================
+remote:
+`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ out := &bytes.Buffer{}
+ DisplayWarningMessage(tt.message, out)
+
+ require.Equal(t, tt.wantOut, out.String())
+ })
+ }
+}
+
+func TestDisplayWarningMessages(t *testing.T) {
+ tests := []struct {
+ name string
+ messages []string
+ wantOut string
+ }{
+ {
+ name: "empty",
+ messages: []string{""},
+ wantOut: "",
+ },
+ {
+ name: "basically empty",
+ messages: []string{" "},
+ wantOut: "",
+ },
+ {
+ name: "something",
+ messages: []string{"something", "here"},
+ wantOut: `remote:
+remote: ========================================================================
+remote:
+remote: something
+remote: here
+remote:
+remote: ========================================================================
+remote:
+`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ out := &bytes.Buffer{}
+ DisplayWarningMessages(tt.messages, out)
+
+ require.Equal(t, tt.wantOut, out.String())
+ })
+ }
+}
+
+func TestDisplayInfoMessage(t *testing.T) {
+ tests := []struct {
+ name string
+ message string
+ wantOut string
+ }{
+ {
+ name: "empty",
+ message: "",
+ wantOut: "",
+ },
+ {
+ name: "basically empty",
+ message: " ",
+ wantOut: "",
+ },
+ {
+ name: "something",
+ message: "something",
+ wantOut: `remote:
+remote: something
+remote:
+`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ out := &bytes.Buffer{}
+ DisplayInfoMessage(tt.message, out)
+
+ require.Equal(t, tt.wantOut, out.String())
+ })
+ }
+}
+
+func TestDisplayInfoMessages(t *testing.T) {
+ tests := []struct {
+ name string
+ messages []string
+ wantOut string
+ }{
+ {
+ name: "empty",
+ messages: []string{""},
+ wantOut: "",
+ },
+ {
+ name: "basically empty",
+ messages: []string{" "},
+ wantOut: "",
+ },
+ {
+ name: "something",
+ messages: []string{"something", "here"},
+ wantOut: "remote: \nremote: something\nremote: here\nremote: \n",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ out := &bytes.Buffer{}
+ DisplayInfoMessages(tt.messages, out)
+
+ require.Equal(t, tt.wantOut, out.String())
+ })
+ }
+}
+
+func Test_noMessages(t *testing.T) {
+ tests := []struct {
+ name string
+ messages []string
+ want bool
+ }{
+ {
+ name: "empty",
+ messages: []string{""},
+ want: true,
+ },
+ {
+ name: "basically empty",
+ messages: []string{" "},
+ want: true,
+ },
+ {
+ name: "something",
+ messages: []string{"something", "here"},
+ want: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ require.Equal(t, tt.want, noMessages(tt.messages))
+ })
+ }
+}
+
+func Test_formatLine(t *testing.T) {
+ require.Equal(t, "remote: something\n", formatLine("something"))
+}
+
+func Test_divider(t *testing.T) {
+ want := `remote:
+remote: ========================================================================
+remote:
+`
+
+ require.Equal(t, want, divider())
+}