-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigurer.go
97 lines (78 loc) · 2.44 KB
/
configurer.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
package configurer
import (
"fmt"
"os"
"strings"
"path/filepath"
"github.com/spf13/viper"
)
// Config provides a method set for extracting data out of a configuration file.
type Config interface {
GetInt(string) int
GetBool(string) bool
GetInt64(string) int64
GetString(string) string
GetStringMapString(string) map[string]string
SetConfigName(string)
SetConfigType(string)
SetEnvPrefix(string)
SetEnvKeyReplacer(*strings.Replacer)
AutomaticEnv()
ReadInConfig() error
AddConfigPath(string)
UnmarshalKey(string, interface{}, ...viper.DecoderConfigOption) error
}
// ConfigReader defines the interface for read-only config file access
type ConfigReader interface {
GetInt(string) int
GetBool(string) bool
GetInt64(string) int64
GetString(string) string
UnmarshalKey(string, interface{}, ...viper.DecoderConfigOption) error
}
// DomainsReader defines the interface for read-only map[string]string access
type DomainsReader interface {
GetStringMapString(string) map[string]string
}
// BuildConfig provides the logger and the info endpoint access to hard-coded build configuration
type BuildConfig struct {
Build string
Component string
Version string
EMFVersion string
EchoVersion string
ReleaseTimestamp string
AdditionalInformation []DataPoint
}
// DataPoint represents additional information component wants to display
type DataPoint struct {
Label string `json:"label"`
Value string `json:"value"`
}
// LoadConfig loads a configuration file specified by the function argument or a path provided the command line.
func LoadConfig(configFile string, prefix string) Config {
var err error
var configPath string
var configName string
var rootConfig Config
if configFile == "" {
configName = "config.yaml"
configPath = os.Getenv("GOPATH") + "/src/github.com/cambridge-blockchain/emf"
} else {
configPath, configName = filepath.Split(configFile)
}
rootConfig = viper.New()
configName = strings.TrimSuffix(configName, filepath.Ext(configName))
rootConfig.SetConfigType("yaml")
rootConfig.SetConfigName(configName)
rootConfig.AddConfigPath(os.ExpandEnv(configPath))
rootConfig.SetEnvPrefix(prefix)
rootConfig.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
rootConfig.AutomaticEnv()
if err = rootConfig.ReadInConfig(); err != nil {
fmt.Printf("[error] configuration file could not be read: %v", err)
const exitCode = 1
os.Exit(exitCode)
}
return rootConfig
}