-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
49 lines (42 loc) · 1.04 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
// 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 main
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func LoadConfig(file string) {
// Config from file
viper.SetConfigType("yaml")
if file != "" {
viper.SetConfigFile(file)
}
viper.AddConfigPath("/.config/")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
fmt.Println("No config file. Read config from env.")
viper.AllowEmptyEnv(false)
}
// Config from env if possible
viper.AutomaticEnv()
viper.SetEnvPrefix("autonomy")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// allow log.level to be adjusted
switch strings.ToUpper(viper.GetString("log.level")) {
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)
}
}