-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.go
50 lines (43 loc) · 903 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)
type threadSafePrintliner struct {
l sync.Mutex
w io.Writer
}
func newThreadSafePrintliner(w io.Writer) *threadSafePrintliner {
return &threadSafePrintliner{w: w}
}
func (p *threadSafePrintliner) println(s string) {
p.l.Lock()
fmt.Fprintln(p.w, s)
p.l.Unlock()
}
func readQuery(r io.Reader) string {
s, _ := ioutil.ReadAll(r) // N.B. not interested in this error; might as well return an empty string
return strings.TrimSpace(strings.Replace(string(s), "\n", " ", -1))
}
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()
}