-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
181 lines (165 loc) · 4.59 KB
/
config.go
File metadata and controls
181 lines (165 loc) · 4.59 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package config
import (
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/Unknwon/com"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
// ConfigInterface ...
type ConfigInterface interface {
ConfigName() string
SetDefaults()
Read()
Wait()
String() string
Debug()
}
func setViperConfig(opts *Options) error {
defer viper.AutomaticEnv() // read in environment variables that match
defer viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if opts.ConfigString != nil {
return nil
}
if IsValidRemotePrefix(opts.ConfigRemotePath) {
var provider string
entry := opts.ConfigRemotePath
for _, p := range validRemotePrefixes {
if strings.HasPrefix(entry, p) {
provider = strings.TrimSuffix(p, "://")
entry = strings.TrimPrefix(entry, p)
break
}
}
if !strings.HasPrefix(entry, "http://") &&
!strings.HasPrefix(entry, "https://") {
entry = "http://" + entry
}
urlParsed, err := url.Parse(entry)
if err != nil {
return errors.Errorf("unable to parse remote url %s", entry)
}
path := strings.TrimPrefix(urlParsed.Path, "/")
urlParsed.Scheme = ""
urlParsed.Path = ""
urlString := strings.TrimPrefix(urlParsed.String(), "//")
viper.AddRemoteProvider(provider, urlString, path)
log.WithField("provider", provider).
WithField("url", urlString).
WithField("path", path).
Info("configuring remote config file provider")
if strings.HasSuffix(path, ".json") {
viper.SetConfigType("json")
} else {
viper.SetConfigType("yaml")
}
return nil
}
defer func() {
for _, pth := range opts.ConfigSearchPaths {
pth, err := homedir.Expand(pth)
if err != nil {
return
}
viper.AddConfigPath(pth)
}
viper.SetConfigType(opts.ConfigFileType)
}()
if com.IsFile(opts.ConfigFileAbsolutePath) {
log.Debug("Found ", opts.ConfigFileAbsolutePath, " already set. Using ", opts.ConfigFileAbsolutePath, " as the config file.")
viper.SetConfigFile(opts.ConfigFileAbsolutePath)
return nil
}
if val, ok := os.LookupEnv(opts.ConfigEnvironName); ok {
pth, _ := homedir.Expand(val)
log.Debug("Found ", opts.ConfigEnvironName, " in env. Using ", val, " as config file name")
if com.IsFile(pth) {
viper.SetConfigFile(pth)
return nil
}
dir, file := path.Split(pth)
ext := path.Ext(file)
file = strings.TrimSuffix(file, ext)
viper.SetConfigName(file)
viper.AddConfigPath(dir)
return nil
}
if pth, err := filepath.Abs("." + opts.AppName + "_config.yml"); err == nil && com.IsFile(pth) {
log.Debug("Using ." + opts.AppName + "_config.yml as config file.")
viper.SetConfigFile(pth)
return nil
}
if pth, err := homedir.Expand("~/." + opts.AppName + "_config.yml"); err == nil && com.IsFile(pth) {
log.Debug("Using ~/." + opts.AppName + "_config.yml as config file.")
viper.SetConfigFile(pth)
return nil
}
if pth, err := filepath.Abs("../." + opts.AppName + "_config.yml"); err == nil && com.IsFile(pth) {
log.Debug("Using ../." + opts.AppName + "_config.yml as config file.")
viper.SetConfigFile(pth)
return nil
}
log.Info("No fixed configuration file found, searching for a config file with name=", opts.ConfigFileBaseName)
viper.SetConfigName(opts.ConfigFileBaseName)
return nil
}
func load(opts *Options) {
initEnv(opts)
err := setViperConfig(opts)
if err != nil {
log.WithError(err).Error("failed to set viper config")
}
if opts.ConfigString != nil {
configFileName := DefaultAppName + "_config.yml"
viper.SetConfigFile(configFileName)
viper.AddConfigPath(".")
memoryFileSystem := afero.NewMemMapFs()
file, err := memoryFileSystem.Create(configFileName)
if err != nil {
log.WithError(err).Error("Cannot create a memory fs")
}
_, err = file.Write([]byte(*opts.ConfigString))
if err != nil {
log.WithError(err).Error("cannot write config memory fs")
}
defer file.Close()
viper.SetFs(memoryFileSystem)
}
// read configuration
if IsValidRemotePrefix(opts.ConfigRemotePath) {
log.Info("reading remote configuration file")
err = viper.ReadRemoteConfig()
} else {
err = viper.ReadInConfig()
}
if err != nil {
log.WithError(err).
WithField("config_file", viper.ConfigFileUsed()).
Error("Cannot read in configuration file ")
}
// Everything depens on app, so we load it first
App.SetDefaults()
App.Read()
// Load the rest of the configurations
for _, r := range registry {
r.SetDefaults()
}
for _, r := range registry {
r.Read()
}
// if IsDebug {
// println("read config " + viper.ConfigFileUsed())
// }
}
// Debug ...
func Debug() {
log.Debug("Config = ")
for _, r := range registry {
r.Debug()
}
}