-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
62 lines (49 loc) · 1.28 KB
/
init.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
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"os"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
flag "github.com/spf13/pflag"
"go.uber.org/zap"
)
func initLogger() *zap.Logger {
logger, err := zap.NewProduction()
if err != nil {
fmt.Print("Failed to initialise logger: ", err)
os.Exit(1)
}
defer logger.Sync()
return logger
}
func initConfig() (*koanf.Koanf, error) {
var k = koanf.New("_")
f := flag.NewFlagSet("vmtctl", flag.ContinueOnError)
// Print CLI flags when -h is used
f.Usage = func() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}
// Define CLI flags
f.String("config", "sample_config.toml", "Path to the .toml config file")
f.Bool("gen-endpoints", false, "Generate only endpoints")
f.Bool("csv", false, "Create a CSV file with endpoints")
// Parse the defined flags
err := f.Parse(os.Args[1:])
if err != nil {
fmt.Print("Failed to parse args")
os.Exit(1)
}
// Load the config file provided in the command line
c, _ := f.GetString("config")
if err := k.Load(file.Provider(c), toml.Parser()); err != nil {
return nil, err
}
// Overwrite from command line
if err := k.Load(posflag.Provider(f, "_", k), nil); err != nil {
return nil, err
}
return k, err
}