-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
94 lines (72 loc) · 2.17 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
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/ExchangeUnion/xud-simnet-bot/build"
"github.com/ExchangeUnion/xud-simnet-bot/channels"
"github.com/ExchangeUnion/xud-simnet-bot/database"
"github.com/ExchangeUnion/xud-simnet-bot/discord"
"github.com/ExchangeUnion/xud-simnet-bot/faucet"
"github.com/ExchangeUnion/xud-simnet-bot/xudrpc"
"github.com/jessevdk/go-flags"
"os"
)
type helpOptions struct {
ShowHelp bool `short:"h" long:"help" description:"Display this help message"`
ShowVersion bool `short:"v" long:"version" description:"Display version and exit"`
}
type config struct {
ConfigFile string `short:"c" long:"configfile" description:"Path to configuration file"`
LogFile string `short:"l" long:"logfile" description:"Path to the log file"`
Xud *xudrpc.Xud `group:"XUD Options"`
Discord *discord.Discord `group:"Discord Options"`
Database *database.Database `group:"Database options"`
ChannelManager *channels.ChannelManager `group:"Channel Manager Options"`
Faucet *faucet.Faucet `group:"Faucet"`
Ethereum *faucet.Ethereum `group:"Ethereum"`
// This option is only parsed in the TOML config file
Channels []*channels.Channel
Help *helpOptions `group:"Help Options"`
}
func loadConfig() *config {
cfg := config{
LogFile: "./xud-simnet-bot.log",
ConfigFile: "./xud-simnet-bot.toml",
Database: &database.Database{
FileName: "./xud-simnet-bot.json",
},
Faucet: &faucet.Faucet{
Port: 9000,
},
Ethereum: &faucet.Ethereum{
RPCHost: "http://130.211.223.61:8545",
},
}
parser := flags.NewParser(&cfg, flags.IgnoreUnknown)
_, err := parser.Parse()
if cfg.Help.ShowHelp {
parser.WriteHelp(os.Stdout)
os.Exit(0)
}
if cfg.Help.ShowVersion {
fmt.Println(build.GetVersion())
os.Exit(0)
}
if err != nil {
printCouldNotParseCli(err)
}
if cfg.ConfigFile != "" {
_, err := toml.DecodeFile(cfg.ConfigFile, &cfg)
if err != nil {
fmt.Println("Could not read config file: " + err.Error())
}
}
_, err = flags.Parse(&cfg)
if err != nil {
printCouldNotParseCli(err)
}
return &cfg
}
func printCouldNotParseCli(err error) {
printFatal("Could not parse CLI arguments: %s", err)
}