-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
100 lines (87 loc) · 2.89 KB
/
config.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
package daemon
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ubuntu/authd/internal/consts"
"github.com/ubuntu/authd/internal/log"
"github.com/ubuntu/decorate"
)
// initViperConfig sets verbosity level and add config env variables and file support based on name prefix.
func initViperConfig(name string, cmd *cobra.Command, vip *viper.Viper) (err error) {
defer decorate.OnError(&err, "can't load configuration")
// Force a visit of the local flags so persistent flags for all parents are merged.
//cmd.LocalFlags() // TODO: still necessary?
// Get cmdline flag for verbosity to configure logger until we have everything parsed.
v, err := cmd.Flags().GetCount("verbosity")
if err != nil {
return fmt.Errorf("internal error: no persistent verbosity flag installed on cmd: %w", err)
}
setVerboseMode(v)
// Handle configuration.
if v, err := cmd.Flags().GetString("config"); err == nil && v != "" {
vip.SetConfigFile(v)
} else {
vip.SetConfigName(name)
vip.AddConfigPath("./")
vip.AddConfigPath("$HOME/")
vip.AddConfigPath("/etc/authd/")
// Add the executable path to the config search path.
if binPath, err := os.Executable(); err != nil {
log.Warningf(context.Background(), "Failed to get current executable path, not adding it as a config dir: %v", err)
} else {
vip.AddConfigPath(filepath.Dir(binPath))
}
}
if err := vip.ReadInConfig(); err != nil {
var e viper.ConfigFileNotFoundError
if errors.As(err, &e) {
log.Infof(context.Background(), "No configuration file: %v.\nWe will only use the defaults, env variables or flags.", e)
} else {
return fmt.Errorf("invalid configuration file: %w", err)
}
} else {
log.Infof(context.Background(), "Using configuration file: %v", vip.ConfigFileUsed())
}
// Handle environment.
vip.SetEnvPrefix(name)
vip.AutomaticEnv()
// Visit manually env to bind every possibly related environment variable to be able to unmarshall
// those into a struct.
// More context on https://github.com/spf13/viper/pull/1429.
prefix := strings.ToUpper(name) + "_"
for _, e := range os.Environ() {
if !strings.HasPrefix(e, prefix) {
continue
}
s := strings.Split(e, "=")
k := strings.ReplaceAll(strings.TrimPrefix(s[0], prefix), "_", ".")
vip.BindEnv(k, s[0])
}
return nil
}
// installConfigFlag installs a --config option.
func installConfigFlag(cmd *cobra.Command) *string {
return cmd.PersistentFlags().StringP("config", "c", "" /*i18n.G(*/, "use a specific configuration file") /*)*/
}
// SetVerboseMode change ErrorFormat and logs between very, middly and non verbose.
func setVerboseMode(level int) {
var reportCaller bool
switch level {
case 0:
log.SetLevel(consts.DefaultLogLevel)
case 1:
log.SetLevel(log.InfoLevel)
case 3:
reportCaller = true
fallthrough
default:
log.SetLevel(log.DebugLevel)
}
log.SetReportCaller(reportCaller)
}