forked from cmaster11/overseer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
50 lines (42 loc) · 904 Bytes
/
util.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 (
"os"
"os/signal"
"strings"
"sync"
"syscall"
)
func waitForSignalInterrupt() {
endWaiter := sync.WaitGroup{}
endWaiter.Add(1)
onSignalInterrupt(func() {
endWaiter.Done()
})
endWaiter.Wait()
}
func onSignalInterrupt(fn func()) {
// We listen for SIGTERM, SIGINT, to please k8s and keyboard users.
onSignals(fn, syscall.SIGINT, syscall.SIGTERM)
}
func onSignals(fn func(), sig ...os.Signal) {
go func() {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, sig...)
<-signalCh
fn()
}()
}
func indent(text, indent string) string {
if text[len(text)-1:] == "\n" {
result := ""
for _, j := range strings.Split(text[:len(text)-1], "\n") {
result += indent + j + "\n"
}
return result
}
result := ""
for _, j := range strings.Split(strings.TrimRight(text, "\n"), "\n") {
result += indent + j + "\n"
}
return result[:len(result)-1]
}