-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
76 lines (68 loc) · 1.62 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
package main
import (
"flag"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/joho/godotenv"
arc_config "github.com/sapcc/arc/config"
)
func defaultConfigDir() string {
if runtime.GOOS == "windows" {
return "C:/monsoon/arc"
}
return "/opt/arc"
}
var (
appName = "arc"
envPrefix = "ARC_"
defaultConfigFile = path.Join(defaultConfigDir(), appName+".cfg")
config = arc_config.New()
)
//returns the path to the config file we want to load
//returns the file the user explicitly specified by flag or env var
//alternativly it returns the default config file if it exists
func configFile() string {
env := os.Getenv(envPrefix + "CONFIGFILE")
var filename string
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.SetOutput(ioutil.Discard)
fs.StringVar(&filename, "config-file", env, "")
fs.StringVar(&filename, "c", env, "")
err := fs.Parse(os.Args[1:])
if err != nil {
return defaultConfigFile
}
//No file specified by the user
if filename == "" {
if _, err := os.Stat(defaultConfigFile); err == nil {
return defaultConfigFile
}
}
return filename
}
func loadConfigFile(file string) error {
vars, err := godotenv.Read(file)
if err != nil {
return err
}
log.Debug("Loaded config file: ", file)
for name, value := range vars {
name = strings.Replace(name, "-", "_", -1)
name = strings.Replace(name, " ", "_", -1)
name = strings.ToUpper(name)
if !strings.HasPrefix(name, envPrefix) {
name = envPrefix + name
}
if os.Getenv(name) == "" {
err = os.Setenv(name, value)
if err != nil {
return err
}
}
}
return nil
}