-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
106 lines (94 loc) · 2.8 KB
/
keys.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
package verdeter
import (
"fmt"
"github.com/ditrit/verdeter/models"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// LKey defines a local flag for cobra bound to env and config file
func (verdeterCmd *VerdeterCommand) LKey(name string, valType models.ConfigType, short string, usage string) error {
verdeterCmd.localConfigKeys[name] = &ConfigKey{
Name: name,
}
return Key(verdeterCmd.cmd, name, valType, short, usage, false)
}
// GKey defines a global flag for cobra bound to env and config file
func (verdeterCmd *VerdeterCommand) GKey(name string, valType models.ConfigType, short string, usage string) error {
verdeterCmd.globalConfigKeys[name] = &ConfigKey{
Name: name,
}
return Key(verdeterCmd.cmd, name, valType, short, usage, true)
}
// Key defines a flag in cobra bound to env and files
//
// for booleans, the default value will be set to false
// for strings, the default value will be set to ""
// for floats, the default value will be set to 0
func Key(cmd *cobra.Command, name string, valueType models.ConfigType, short string, usage string, global bool) error {
var flagSet = cmd.PersistentFlags()
if !global {
flagSet = cmd.Flags()
}
switch valueType {
case IsStr:
keyString(flagSet, name, short, usage)
case IsInt:
keyInt(flagSet, name, short, usage)
case IsBool:
keyBool(flagSet, name, short, usage)
case IsUint:
keyUint(flagSet, name, short, usage)
}
var flag *pflag.Flag
if global {
flag = cmd.PersistentFlags().Lookup(name)
} else {
flag = cmd.Flags().Lookup(name)
}
return bindFlagToViper(name, flag)
}
// create a string flag
func keyString(flagSet *pflag.FlagSet, name string, short string, usage string) {
if short == "" {
flagSet.String(name, "", usage)
} else {
flagSet.StringP(name, short, "", usage)
}
}
// create a boolean flag
func keyBool(flagSet *pflag.FlagSet, name string, short string, usage string) {
if short == "" {
flagSet.Bool(name, false, usage)
} else {
flagSet.BoolP(name, short, false, usage)
}
}
// create an integer flag
func keyInt(flagSet *pflag.FlagSet, name string, short string, usage string) {
if short == "" {
flagSet.Int(name, 0, usage)
} else {
flagSet.IntP(name, short, 0, usage)
}
}
// create an unsigned integer flag
func keyUint(flagSet *pflag.FlagSet, name string, short string, usage string) {
if short == "" {
flagSet.Uint(name, 0, usage)
} else {
flagSet.UintP(name, short, 0, usage)
}
}
// bind a cobra flag to a viper key and an environment variable
func bindFlagToViper(name string, flag *pflag.Flag) error {
err := viper.BindPFlag(name, flag)
if err != nil {
return fmt.Errorf("could not bind flag %q to viper key (error=%w)", name, err)
}
err = viper.BindEnv(name)
if err != nil {
return fmt.Errorf("could not bind flag %q to env key (error=%w)", name, err)
}
return nil
}