-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (113 loc) · 3.02 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cmdconfig
import (
"flag"
"os"
"strings"
"github.com/rs/zerolog/log"
)
// ArgMap for parsing flags with multiple keys
type ArgMap []string
func (a *ArgMap) String() string {
return strings.Join(*a, ", ")
}
func (a *ArgMap) Set(value string) error {
*a = append(*a, value)
return nil
}
const (
FlagAll = "all"
FlagBase64 = "base64"
FlagCompare = "compare"
FlagCSV = "csv"
FlagDel = "del"
FlagDryRun = "dry-run"
FlagEnv = "env"
FlagExtend = "extend"
FlagGenerate = "generate"
FlagGet = "get"
FlagKey = "key"
FlagMerge = "merge"
FlagPrefix = "prefix"
FlagSep = "sep"
FlagValue = "value"
FlagVersion = "version"
FlagOS = "os"
FlagFormat = "format"
)
// ParseFlags before calling Cmd
func ParseFlags(version string) *CmdIn {
in := NewCmdIn(CmdInParams{Version: version})
// Flags
flag.BoolVar(&in.PrintVersion,
FlagVersion, false, "Print build version")
flag.StringVar(&in.Prefix,
FlagPrefix, "APP_", "Config key prefix")
flag.StringVar(&in.Env,
FlagEnv, "dev",
"Config file to use, also supports wildcards \"*\" and \"sample.*\"")
flag.BoolVar(&in.All,
FlagAll, false, "Apply to all config files and samples")
flag.BoolVar(&in.Del,
FlagDel, false, "Delete the specified keys")
// Default must be empty
flag.StringVar(&in.Compare,
FlagCompare, "", "Compare config file keys")
in.Keys = ArgMap{}
flag.Var(&in.Keys,
FlagKey, "Set key and print config JSON")
in.Values = ArgMap{}
flag.Var(&in.Values,
FlagValue, "Value for last key specified")
// Default must be empty
flag.StringVar(&in.PrintValue,
FlagGet, "", "Print value for given key")
// Default must be empty
flag.StringVar(&in.Generate,
FlagGenerate, "", "Generate config helper at path")
flag.BoolVar(&in.CSV,
FlagCSV, false, "Print env as a list of key=value")
flag.StringVar(&in.Sep,
FlagSep, ",", "Separator for use with csv flag")
flag.BoolVar(&in.DryRun,
FlagDryRun, false, "Don't write files, just print result")
flag.BoolVar(&in.Base64,
FlagBase64, false, "Encode config file as base64 string")
flag.StringVar(&in.OS,
FlagOS, "other",
"Override compiled x-platform config")
flag.StringVar(&in.Format,
FlagFormat, "", "Override config file format")
in.Extend = ArgMap{}
flag.Var(&in.Extend,
FlagExtend, "Extend config")
flag.BoolVar(&in.Merge,
FlagMerge, false, "Merge with parent config")
flag.Parse()
return in
}
// Main function for cmd/configu.
// The configu command can be customized by copying the code below.
// Try not to change the default behaviour, e.g.
// custom flags must only add functionality
func Main(version string) {
// Parse and validate flags
in := ParseFlags(version)
err := in.Valid()
if err != nil {
log.Error().Stack().Err(err).Msg("")
os.Exit(1)
}
// Insert your custom code here...
// Run cmd
out, err := Cmd(in)
if err != nil {
log.Error().Stack().Err(err).Msg("")
os.Exit(1)
}
// Process cmd results
exitCode, err := in.Process(out)
if err != nil {
log.Error().Stack().Err(err).Msg("")
}
os.Exit(exitCode)
}