-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
114 lines (98 loc) · 3.44 KB
/
engine.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package goConf
import (
"fmt"
"path/filepath"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
func addConfigPath(v *viper.Viper, p string) {
if v != nil {
v.AddConfigPath(p)
} else {
viper.AddConfigPath(p)
}
}
func setConfigName(v *viper.Viper, configName string) {
// Now set the configuration file.
if v != nil {
v.SetConfigName(configName)
} else {
viper.SetConfigName(configName)
}
}
//----------------------------------------------------------------------------------
// TranslatePath()
//----------------------------------------------------------------------------------
// Translates a relative path into a fully qualified path relative to the config
// file that specified it. Absolute paths are passed unscathed.
//----------------------------------------------------------------------------------
func TranslatePath(base, p string) string {
if filepath.IsAbs(p) {
return p
}
return filepath.Join(base, p)
}
//----------------------------------------------------------------------------------
// TranslatePathInPlace()
//----------------------------------------------------------------------------------
// Translates a relative path into a fully qualified path in-place (updating the
// pointer) relative to the config file that specified it. Absolute paths are
// passed unscathed.
//----------------------------------------------------------------------------------
func TranslatePathInPlace(base string, p *string) {
*p = TranslatePath(base, *p)
}
//----------------------------------------------------------------------------------
// GetPath()
//----------------------------------------------------------------------------------
// GetPath allows configuration strings that specify a (config-file) relative path
//
// For example: Assume our config is located in /etc/hyperledger/fabric/core.yaml with
// a key "msp.configPath" = "msp/config.yaml".
//
// This function will return:
// GetPath("msp.configPath") -> /etc/hyperledger/fabric/msp/config.yaml
//
//----------------------------------------------------------------------------------
func GetPath(key string) string {
p := viper.GetString(key)
if p == "" {
return ""
}
return TranslatePath(filepath.Dir(viper.ConfigFileUsed()), p)
}
//----------------------------------------------------------------------------------
// InitViper()
//----------------------------------------------------------------------------------
// Performs basic initialization of our viper-based configuration layer.
// Primary thrust is to establish the paths that should be consulted to find
// the configuration we need. If v == nil, we will initialize the global
// Viper instance
//----------------------------------------------------------------------------------
func InitViper(v *viper.Viper, configName string) error {
paths := GetSearchPath()
for _, dir := range paths {
addConfigPath(v, dir)
}
v.SetConfigName(configName)
return nil
}
// InitViperConfig initializes viper config
func InitViperConfig(v *viper.Viper, configName string, configFile string) error {
if configFile == "" {
configFile = v.GetString("c")
}
if configFile != "" {
viper.SetConfigFile(configFile)
} else {
err := InitViper(v, configName)
if err != nil {
return err
}
}
err := v.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return errors.WithMessage(err, fmt.Sprintf("error when reading %s config file", configName))
}
return nil
}