-
Notifications
You must be signed in to change notification settings - Fork 156
/
command.go
52 lines (46 loc) · 1.16 KB
/
command.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
51
52
package cmd
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"syscall"
)
// ReadConfigFile takes a file path as an argument and attempts to
// unmarshal the content of the file into a struct containing a
// configuration of a Pebble component.
//
// Lifted from
//
// https://raw.githubusercontent.com/letsencrypt/boulder/master/cmd/shell.go
func ReadConfigFile(filename string, out interface{}) error {
configData, err := os.ReadFile(filename)
if err != nil {
return err
}
return json.Unmarshal(configData, out)
}
// FailOnError exits and prints an error message if we encountered a problem
//
// Lifted from
//
// https://raw.githubusercontent.com/letsencrypt/boulder/master/cmd/shell.go
func FailOnError(err error, msg string) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", msg, err)
os.Exit(1)
}
}
// CatchSignals catches SIGTERM, SIGINT, SIGHUP and executes a callback
// method before exiting
func CatchSignals(callback func()) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM)
signal.Notify(sigChan, syscall.SIGINT)
signal.Notify(sigChan, syscall.SIGHUP)
<-sigChan
if callback != nil {
callback()
}
os.Exit(0)
}