-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.go
50 lines (43 loc) · 1.14 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
package main
import (
"encoding/json"
"io/ioutil"
binance "github.com/adshao/go-binance/v2"
"github.com/juju/errors"
"gopkg.in/urfave/cli.v1"
)
var config = new(Config)
// Config define cli config
type Config struct {
Accounts []AccountConfig `json:"accounts"`
}
// AccountBalances return account balance map
func (c *Config) AccountBalances() map[string]map[string]binance.Balance {
accountMap := make(map[string]map[string]binance.Balance)
for _, info := range c.Accounts {
accountMap[info.Name] = make(map[string]binance.Balance)
for _, balance := range info.Balances {
accountMap[info.Name][balance.Asset] = balance
}
}
return accountMap
}
// AccountConfig define account config
type AccountConfig struct {
Name string `json:"name"`
Balances []binance.Balance `json:"balances"`
}
func loadConfig(c *cli.Context) (*Config, error) {
if !c.GlobalIsSet("configfile") {
return config, nil
}
keyBytes, err := ioutil.ReadFile(c.GlobalString("configfile"))
if err != nil {
return nil, errors.Trace(err)
}
err = json.Unmarshal(keyBytes, config)
if err != nil {
return nil, errors.Trace(err)
}
return config, nil
}