/
config.go
93 lines (71 loc) · 2.19 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
package main
import (
"fmt"
"github.com/BoltzExchange/channel-bot/cleaner"
"github.com/BoltzExchange/channel-bot/utils"
"os"
"github.com/BoltzExchange/channel-bot/build"
"github.com/BoltzExchange/channel-bot/discord"
"github.com/BoltzExchange/channel-bot/lnd"
"github.com/BoltzExchange/channel-bot/notifications"
"github.com/BurntSushi/toml"
"github.com/google/logger"
"github.com/jessevdk/go-flags"
)
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"`
Notifications *notifications.ChannelManager `group:"Notification Options"`
ChannelCleaner *cleaner.ChannelCleaner `group:"Channel Cleaner Options"`
Lnd *lnd.LND `group:"LND Options"`
Discord *discord.Discord `group:"Discord Options"`
Help *helpOptions `group:"Help Options"`
// This option is only parsed in the TOML config file
SignificantChannels []*notifications.SignificantChannel
}
func loadConfig() *config {
cfg := config{
LogFile: "./channel-bot.log",
ConfigFile: "./channel-bot.toml",
Notifications: ¬ifications.ChannelManager{},
ChannelCleaner: &cleaner.ChannelCleaner{
Interval: 24,
MaxInactive: 30,
MaxInactivePrivate: 60,
},
}
parser := flags.NewParser(&cfg, flags.IgnoreUnknown)
_, err := parser.Parse()
if cfg.Help.ShowVersion {
fmt.Println(build.GetVersion())
os.Exit(0)
}
if cfg.Help.ShowHelp {
parser.WriteHelp(os.Stdout)
os.Exit(0)
}
if err != nil {
printCouldNotParseCli(err)
}
if cfg.ConfigFile != "" {
_, err := toml.DecodeFile(cfg.ConfigFile, &cfg)
if err != nil {
fmt.Printf("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)
}
func logConfig(cfg *config) {
logger.Info("Loaded config: " + utils.Stringify(cfg))
}