-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
78 lines (64 loc) · 2.03 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
package cli
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type BaseConfig struct {
LogLevel string `mapstructure:"log-level"`
LogFormat string `mapstructure:"log-format"`
C1zPath string `mapstructure:"file"`
ClientID string `mapstructure:"client-id"`
ClientSecret string `mapstructure:"client-secret"`
GrantEntitlementID string `mapstructure:"grant-entitlement"`
GrantPrincipalID string `mapstructure:"grant-principal"`
GrantPrincipalType string `mapstructure:"grant-principal-type"`
RevokeGrantID string `mapstructure:"revoke-grant"`
C1zTempDir string `mapstructure:"c1z-temp-dir"`
}
func getConfigPath(customPath string) (string, string, error) {
if customPath != "" {
cfgDir, cfgFile := filepath.Split(filepath.Clean(customPath))
if cfgDir == "" {
cfgDir = "."
}
ext := filepath.Ext(cfgFile)
if ext == "" || (ext != ".yaml" && ext != ".yml") {
return "", "", errors.New("expected config file to have .yaml or .yml extension")
}
return strings.TrimSuffix(cfgDir, string(filepath.Separator)), strings.TrimSuffix(cfgFile, ext), nil
}
return ".", ".baton", nil
}
// loadConfig sets viper up to parse the config into the provided configuration object.
func loadConfig[T any, PtrT *T](cmd *cobra.Command, cfg PtrT) (*viper.Viper, error) {
v := viper.New()
v.SetConfigType("yaml")
cfgPath, cfgName, err := getConfigPath(os.Getenv("BATON_CONFIG_PATH"))
if err != nil {
return nil, err
}
v.SetConfigName(cfgName)
v.AddConfigPath(cfgPath)
if err := v.ReadInConfig(); err != nil {
if ok := !errors.Is(err, viper.ConfigFileNotFoundError{}); !ok {
return nil, err
}
}
v.SetEnvPrefix(envPrefix)
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.AutomaticEnv()
if err := v.BindPFlags(cmd.PersistentFlags()); err != nil {
return nil, err
}
if err := v.BindPFlags(cmd.Flags()); err != nil {
return nil, err
}
if err := v.Unmarshal(cfg); err != nil {
return nil, err
}
return v, nil
}