-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
75 lines (61 loc) 路 1.65 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
package config
import (
"fmt"
"github.com/spf13/viper"
)
// Config holds the merged configuration from the config file, environment variables, and flags
type Config struct {
// General configuration
PreviewOrder bool `yaml:"preview_order"`
// Account configuration
Brokerage string `yaml:"brokerage"`
Tradier *TradierConfig `yaml:"tradier,omitempty"`
}
// TradierConfig holds the configuration for Tradier brokerage
type TradierConfig struct {
AccountID string `yaml:"account,omitempty"`
AccessToken string `yaml:"access_token,omitempty"`
}
// New creates a new config from Viper object
func New(v *viper.Viper) *Config {
brokerage := v.GetString("brokerage")
config := &Config{
Brokerage: brokerage,
PreviewOrder: v.GetBool("preview_order"),
}
switch brokerage {
case "tradier":
config.Tradier = &TradierConfig{
AccountID: v.GetString("tradier.account"),
AccessToken: v.GetString("tradier.access_token"),
}
}
// Handle tradier config if it exists
// if len(v.GetStringMap("tradier")) != 0 {
// tc := &TradierConfig{
// AccountID: v.GetString("tradier.account_id"),
// AccessToken: v.GetString("tradier.access_token"),
// }
//
// config.Tradier = tc
// }
return config
}
// CheckConfig checks for the validity of the cofiguration
func (c *Config) CheckConfig() error {
if len(c.Brokerage) == 0 {
return fmt.Errorf("config error: brokerage not provided")
}
if len(c.Tradier.AccessToken) == 0 {
return fmt.Errorf("config error: access token not provided")
}
return nil
}
// Copy returns a copy of the configuration
func (c *Config) Copy() *Config {
if c == nil {
return nil
}
cp := *c
return &cp
}