-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
83 lines (64 loc) · 1.88 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
71
72
73
74
75
76
77
78
79
80
81
82
83
package telepathy
import (
"fmt"
"os"
"github.com/spf13/viper"
)
// Constants for Environment variables
const (
Name = "telepathy"
Env = "env"
Environment = "environment"
EnvironmentVar = "TELEPATHY__ENVIRONMENT"
PidFull = "pidfull"
PidFile = "pidfile"
PidFileVar = "TELEPATHY__PIDFILE"
PidPath = "pidpath"
PidPathVar = "TELEPATHY__PIDPATH"
Local = "local"
Production = "production"
)
// ConfigReader represents configuration reader
type ConfigReader interface {
Get(string) interface{}
GetString(string) string
GetInt(string) int
GetBool(string) bool
GetStringMap(string) map[string]interface{}
GetStringMapString(string) map[string]string
GetStringSlice(string) []string
SetDefault(string, interface{})
}
// DefaultSettings is the function for configuring defaults
type DefaultSettings func(config ConfigReader)
// ConfigDefaults - returns the defauls of the config passed
func ConfigDefaults(config ConfigReader) {
Defaults(config)
}
// Defaults is the default settings functor
func Defaults(config ConfigReader) {
config.SetDefault(Environment, GetEnv(EnvironmentVar, Local))
config.SetDefault(PidFile, GetEnv(PidFileVar, fmt.Sprintf("%s.pid", Name)))
config.SetDefault(PidPath, GetEnv(PidPathVar, fmt.Sprintf("/var/run/%s", Name)))
}
// GetEnv - pull values or set defaults.
func GetEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}
// LoadConfig - returns configuration for a particular app
func LoadConfig(defaultSetup DefaultSettings) (ConfigReader, error) {
config := viper.New()
Defaults(config)
// var e error
//
// if config.GetString(CoinbaseSecret) == "" || config.GetString(CoinbaseKey) == "" || config.GetString(CoinbasePhrase) == "" {
// e = errors.New("missing or invalid CoinbaseSecret/CoinbaseKey/CoinbasePhrase")
// } else {
// e = nil
// }
return config, nil
}