diff --git a/main.go b/main.go index d0276c1..2e20e96 100644 --- a/main.go +++ b/main.go @@ -9,10 +9,8 @@ import ( "log" "os" "os/exec" - "os/signal" "strings" "sync" - "syscall" ) type database struct { @@ -187,46 +185,3 @@ func runSQL(quitContext context.Context, db database, sql string, key string, pr return result } - -func println(w io.Writer, s string) { - printLock.Lock() - defer printLock.Unlock() - fmt.Fprintln(w, s) -} - -func readInput(r io.Reader) string { - ls := []string{} - var err error - rd := bufio.NewReader(r) - - for { - var s string - s, err = rd.ReadString('\n') - - if err == io.EOF { - return strings.Join(ls, " ") - } - s = strings.TrimSpace(s) - if len(s) == 0 { - continue - } - ls = append(ls, strings.TrimSpace(s)) - } -} - -func trimEmpty(s []string) []string { - var r []string - for _, str := range s { - if str != "" { - r = append(r, str) - } - } - return r -} - -func awaitSignal(cancel context.CancelFunc) { - signals := make(chan os.Signal) - signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) - <-signals - cancel() -} diff --git a/test-docker-compose.yml b/test-docker-compose.yml index ff4e5c2..5e28466 100644 --- a/test-docker-compose.yml +++ b/test-docker-compose.yml @@ -1,6 +1,6 @@ version: '3' services: - blah-test: + test: image: golang:1.11 volumes: - "$PWD:/go/src/sql" diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..79cadf9 --- /dev/null +++ b/utils.go @@ -0,0 +1,55 @@ +package main + +import ( + "bufio" + "context" + "fmt" + "io" + "os" + "os/signal" + "strings" + "syscall" +) + +func println(w io.Writer, s string) { + printLock.Lock() + defer printLock.Unlock() + fmt.Fprintln(w, s) +} + +func readInput(r io.Reader) string { + ls := []string{} + var err error + rd := bufio.NewReader(r) + + for { + var s string + s, err = rd.ReadString('\n') + + if err == io.EOF { + return strings.Join(ls, " ") + } + s = strings.TrimSpace(s) + if len(s) == 0 { + continue + } + ls = append(ls, strings.TrimSpace(s)) + } +} + +func trimEmpty(s []string) []string { + var r = make([]string, 0) + for _, str := range s { + if str != "" { + r = append(r, str) + } + } + return r +} + +func awaitSignal(cancel context.CancelFunc) { + signals := make(chan os.Signal) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + <-signals + cancel() +} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..1b7a464 --- /dev/null +++ b/utils_test.go @@ -0,0 +1,38 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestTrimEmpty(t *testing.T) { + var ts = []struct { + name string + input []string + expected []string + }{ + { + name: "Empty case", + input: []string{}, + expected: []string{}, + }, + { + name: "Removes multiple empty from beginning, middle and end", + input: []string{"", "a", "", "b", ""}, + expected: []string{"a", "b"}, + }, + { + name: "Doesn't remove spaces", + input: []string{" ", "a", "", "b", ""}, + expected: []string{" ", "a", "b"}, + }, + } + for _, tc := range ts { + t.Run(tc.name, func(t *testing.T) { + var actual = trimEmpty(tc.input) + if !reflect.DeepEqual(tc.expected, actual) { + t.Errorf("Expected %v but got %v", tc.expected, actual) + } + }) + } +}