-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.go
More file actions
66 lines (54 loc) · 1.48 KB
/
cli.go
File metadata and controls
66 lines (54 loc) · 1.48 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package zindexer
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"os"
"os/signal"
"syscall"
)
type CleanUpHandler func()
type DefaultConfigHandler func()
var defaultConfigHandler DefaultConfigHandler
func SetupCloseHandler(handler CleanUpHandler) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
zap.S().Warn("\r- Ctrl+C pressed in Terminal")
if handler != nil {
handler()
}
_ = zap.S().Sync() // Sync logger
os.Exit(0)
}()
}
func SetupDefaultConfiguration(handler DefaultConfigHandler) {
defaultConfigHandler = handler
}
func SetupConfiguration(c *cobra.Command, args []string) {
var configFileFlag string
c.PersistentFlags().StringVarP(&configFileFlag, "config", "c", "./config.yaml", "The path to the config file to use.")
err := viper.BindPFlag("config", c.PersistentFlags().Lookup("config"))
if err != nil {
zap.S().Fatalf("unable to bind config flag: %+v", err)
}
viper.SetConfigFile(configFileFlag)
viper.SetConfigName("config") // config file name without extension
viper.AddConfigPath(".") // search path
if defaultConfigHandler != nil {
defaultConfigHandler()
}
viper.AutomaticEnv() // read value ENV variables
err = viper.ReadInConfig()
if err != nil {
zap.S().Fatalf("unable to read in config due to error: %+v", err)
}
}
func RunCLI(rootCommand *cobra.Command) {
if err := rootCommand.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}