/
viper.go
94 lines (81 loc) · 2.39 KB
/
viper.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
package config
import (
"errors"
"fmt"
"os"
"path"
"github.com/spf13/viper"
)
const DefaultClientConfig = "default"
const CludodExecutable = "cludod"
const CludoExecutable = "cludo"
// EnvPrefix defines a prefix required on all environment variables bound to Config.
// For example the path "number" is bound to CLUDO_NUMBER.
const EnvPrefix = "CLUDO"
var ErrConfigNotFound = errors.New("Failed to load configuration file: File not found")
func ErrConfigLoadFailed(cause error) error {
return fmt.Errorf("Failed to load configuration file: %v", cause)
}
func ErrHomeDirFailed(cause error) error {
return fmt.Errorf("Failed to get user home directory: %v", cause)
}
func ErrWorkingDirFailed(cause error) error {
return fmt.Errorf("Failed to get working directory: %v", cause)
}
func ConfigureViper(executable string, configFile string) error {
cwd, err := os.Getwd()
if err != nil {
return ErrWorkingDirFailed(err)
}
homedir, err := os.UserHomeDir()
if err != nil {
return ErrHomeDirFailed(err)
}
// When we bind flags to environment variables expect that the
// environment variables are prefixed, e.g. a flag like --profile
// binds to an environment variable CLUDO_PROFILE. This helps
// avoid conflicts.
viper.SetEnvPrefix(EnvPrefix)
viper.AutomaticEnv()
// Read configuration
if configFile != "" {
viper.SetConfigFile(configFile)
} else {
viper.SetConfigName(executable)
viper.SetConfigType("yaml")
viper.AddConfigPath(path.Join("/etc", executable))
viper.AddConfigPath(path.Join(homedir, fmt.Sprintf(".%s", executable)))
viper.AddConfigPath(path.Join(homedir, ".config", executable))
viper.AddConfigPath(cwd)
}
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
return ErrConfigNotFound
} else {
return ErrConfigLoadFailed(err)
}
}
// local repo cludo.yaml file
// Only check for this when we are using the client.
if executable == "cludo" {
viper.SetConfigName("cludo")
viper.AddConfigPath(".")
if err := viper.MergeInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return ErrConfigNotFound
} else {
return ErrConfigLoadFailed(err)
}
}
}
return nil
}
func NewConfigFromViper() (*Config, error) {
config := Config{}
err := viper.Unmarshal(&config)
if err != nil {
return nil, err
}
return &config, nil
}