-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
55 lines (47 loc) · 1.43 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
// SPDX-License-Identifier: ISC
// Copyright (c) 2019-2021 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package config
import (
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// LoadConfig first reads `config.yaml` from a list of configuration path and merges
// the configurations with environment variables if there is any.
func LoadConfig(envPrefix string, extraConfigPaths ...string) {
// Config from file
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("/.config/")
if err := viper.ReadInConfig(); err != nil {
log.WithError(err).Warn("No config file. Read config from env.")
}
// merge config with extra configurations
for _, p := range extraConfigPaths {
viper.SetConfigFile(p)
if err := viper.MergeInConfig(); err != nil {
log.WithError(err).WithField("path", p).Warn("Fail to read config from path.")
}
}
// Config from env if possible
viper.AutomaticEnv()
viper.SetEnvPrefix(envPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// allow log.level to be adjusted
switch strings.ToUpper(viper.GetString("log.level")) {
case "TRACE":
log.SetLevel(log.TraceLevel)
case "DEBUG":
log.SetLevel(log.DebugLevel)
case "INFO":
log.SetLevel(log.InfoLevel)
case "WARN":
log.SetLevel(log.WarnLevel)
case "ERROR":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.ErrorLevel)
}
}