-
Notifications
You must be signed in to change notification settings - Fork 27
/
config_helper.go
56 lines (48 loc) · 1.14 KB
/
config_helper.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
package zcnbridge
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/0chain/gosdk/zcnbridge/log"
"github.com/spf13/viper"
)
func initBridgeConfig(sdkConfig *BridgeSDKConfig) *viper.Viper {
cfg := readConfig(sdkConfig, func() string {
return *sdkConfig.ConfigBridgeFile
})
log.Logger.Info(fmt.Sprintf("Bridge config has been initialized from %s", cfg.ConfigFileUsed()))
return cfg
}
func readConfig(sdkConfig *BridgeSDKConfig, getConfigName func() string) *viper.Viper {
cfg := viper.New()
cfg.AddConfigPath(*sdkConfig.ConfigDir)
cfg.SetConfigName(getConfigName())
cfg.SetConfigType("yaml")
err := cfg.ReadInConfig()
if err != nil {
_, file, line, ok := runtime.Caller(2)
if !ok {
file = "???"
line = 0
}
f := filepath.Base(file)
header := fmt.Sprintf("[ERROR] %s:%d: ", f, line)
ExitWithError(header+"Can't read config: ", err)
}
return cfg
}
func GetConfigDir() string {
var configDir string
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configDir = home + "/.zcn"
return configDir
}
func ExitWithError(v ...interface{}) {
_, _ = fmt.Fprintln(os.Stderr, v...)
os.Exit(1)
}