-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
104 lines (86 loc) · 2.2 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"fmt"
"github.com/covermymeds/azure-key-vault-agent/configwatcher"
"github.com/luci/luci-go/common/flag/flagenum"
log "github.com/sirupsen/logrus"
"os"
"runtime/debug"
)
type outputType uint
var (
version = "dev"
commit = "none"
date = "unknown"
)
var outputTypeEnum = flagenum.Enum{
"json": outputType(10),
"text": outputType(20),
}
func (val *outputType) Set(v string) error {
return outputTypeEnum.FlagSet(val, v)
}
func (val *outputType) String() string {
return outputTypeEnum.FlagString(*val)
}
func (val outputType) MarshalJSON() ([]byte, error) {
return outputTypeEnum.JSONMarshal(val)
}
var configFile string
var output outputType
var help bool
var debugMode bool
var ver bool
var runOnce bool
func init() {
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fs.SetOutput(os.Stdout)
fs.BoolVar(&help, "help", false, "Show this help text")
fs.BoolVar(&debugMode, "debug", false, "Enable debugging")
fs.StringVar(&configFile, "config", "", "Read config from this `file`")
fs.StringVar(&configFile, "c", "", "Read config from this `file` (shorthand)")
fs.Var(&output, "output", fmt.Sprintf("Output type (default json). Options are: %v (default json)", outputTypeEnum.Choices()))
fs.BoolVar(&ver, "version", false, "Show the version of akva-key-vault-agent")
fs.BoolVar(&runOnce, "once", false, "Run once and quit")
fs.Parse(os.Args[1:])
if help {
fs.PrintDefaults()
os.Exit(0)
}
if ver {
fmt.Printf("Version: %s\nCommit: %s\nBuilt on %s\n", version, commit, date)
os.Exit(0)
}
if configFile == "" {
log.Fatalf("Missing --config/-c")
}
if output != outputTypeEnum["text"] {
// JSON Format customized to use _timestamp so it marshals first alphabetically
log.SetFormatter(&log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyTime: "_timestamp",
},
})
} else {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
}
fs.Parse(os.Args[1:])
}
func main() {
defer func() {
if r := recover(); r != nil {
if debugMode {
debug.PrintStack()
}
log.Fatalf("Caught panic in main: %v", r)
}
}()
if runOnce {
configwatcher.ParseAndRunWorkersOnce(configFile)
} else {
configwatcher.Watcher(configFile)
}
}