-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
44 lines (36 loc) · 1.07 KB
/
init.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
package config
import (
"fmt"
"os"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
// InitConfig reads in config file and ENV variables if set.
func InitConfig(cfgFile string) {
setDefaults()
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name "starcloud" (without extension).
viper.AddConfigPath(".")
viper.AddConfigPath(home)
viper.SetConfigName("starcloud")
}
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "__"))
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}