-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
70 lines (59 loc) · 1.73 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
package pkg
import (
"log"
"os"
"os/user"
"github.com/meinto/glow/cmd"
"github.com/meinto/glow/git"
l "github.com/meinto/glow/logging"
"github.com/spf13/viper"
)
var configInitialized = false
func InitGlobalConfig() {
if !configInitialized {
configInitialized = true
exec := cmd.NewCmdExecutor("/bin/bash")
g := git.NewNativeService(git.Options{
CmdExecutor: exec,
})
rootRepoPath, _, _, err := g.GitRepoPath()
if err != nil {
rootRepoPath = "."
}
viper.SetDefault("versionFile", "VERSION")
viper.SetDefault("logLevel", "info")
viper.SetDefault("versionFileType", "raw")
viper.SetConfigName("glow.config")
viper.AddConfigPath(rootRepoPath)
err = viper.ReadInConfig()
l.Configure(l.Options{
l.GetLevel(viper.GetString("logLevel")),
})
l.Log().
Info(viper.AllSettings()).
ErrorFields(err, l.Fields{"msg": "there is no glow config"})
viper.SetConfigName("glow.private")
viper.AddConfigPath(rootRepoPath)
err = viper.MergeInConfig()
l.Log().ErrorFields(err, l.Fields{"msg": "there is no private glow config"})
// load global config
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
globalConfigPath := usr.HomeDir + "/.glow"
viper.SetConfigName("glow.config")
viper.AddConfigPath(globalConfigPath)
err = viper.MergeInConfig()
l.Log().ErrorFields(err, l.Fields{"msg": "there is no global glow config"})
viper.SetConfigName("glow.private")
viper.AddConfigPath(globalConfigPath)
err = viper.MergeInConfig()
l.Log().ErrorFields(err, l.Fields{"msg": "there is no global private glow config"})
viper.SetEnvPrefix("glow")
err = viper.BindEnv("token")
l.Log().
WarnIf(l.Fields{"msg": "env GLOW_TOKEN is missing"}, os.Getenv("GLOW_TOKEN") == "").
Error(err)
}
}