/
client.go
81 lines (71 loc) · 1.63 KB
/
client.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
package client
import (
"github.com/bivas/rivi/pkg/util/log"
"github.com/spf13/viper"
)
type ClientConfig interface {
GetOAuthToken() string
GetSecret() string
GetApplicationID() int
GetPrivateKeyFile() string
}
type clientConfig struct {
*viper.Viper
}
func (c *clientConfig) GetPrivateKeyFile() string {
return c.GetString("private_key_file")
}
func (c *clientConfig) GetApplicationID() int {
return c.GetInt("appid")
}
func (c *clientConfig) GetOAuthToken() string {
return c.GetString("token")
}
func (c *clientConfig) GetSecret() string {
return c.GetString("secret")
}
func NewDefaultClientConfig() ClientConfig {
return NewClientConfig(viper.New())
}
func NewClientConfig(v *viper.Viper) ClientConfig {
v.SetEnvPrefix("rivi_config")
v.BindEnv("token")
v.BindEnv("secret")
v.BindEnv("appid")
v.BindEnv("private_key_file")
return &clientConfig{v}
}
func NewClientConfigFromFile(file string) ClientConfig {
logger := log.Get("config.client")
v := viper.New()
logger.DebugWith(
log.MetaFields{
log.F("config", file),
}, "Loading client config from file",
)
v.SetConfigFile(file)
if err := v.ReadInConfig(); err != nil {
logger.WarningWith(
log.MetaFields{
log.E(err),
log.F("config", file),
}, "Error loading config from file",
)
return NewDefaultClientConfig()
}
configViper := v.Sub("config")
if configViper != nil {
logger.DebugWith(
log.MetaFields{
log.F("config", file),
}, "Loading client config from sub config",
)
return NewClientConfig(configViper)
}
logger.DebugWith(
log.MetaFields{
log.F("config", file),
}, "Loading client config",
)
return NewClientConfig(v)
}