forked from BoltzExchange/boltz-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
154 lines (116 loc) · 3.63 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package boltz_lnd
import (
"fmt"
"github.com/BoltzExchange/boltz-lnd/boltz"
"github.com/BoltzExchange/boltz-lnd/build"
"github.com/BoltzExchange/boltz-lnd/database"
"github.com/BoltzExchange/boltz-lnd/lnd"
"github.com/BoltzExchange/boltz-lnd/rpcserver"
"github.com/BoltzExchange/boltz-lnd/utils"
"github.com/BurntSushi/toml"
"github.com/jessevdk/go-flags"
"os"
"path"
"runtime"
)
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 {
DataDir string `short:"d" long:"datadir" description:"Data directory of boltz-lnd"`
ConfigFile string `short:"c" long:"configfile" description:"Path to configuration file"`
LogFile string `short:"l" long:"logfile" description:"Path to the log file"`
LogPrefix string `long:"logprefix" description:"Prefix of all log messages"`
Boltz *boltz.Boltz `group:"Boltz Options"`
LND *lnd.LND `group:"LND Options"`
RPC *rpcserver.RpcServer `group:"RPC options"`
Database *database.Database `group:"Database options"`
Help *helpOptions `group:"Help Options"`
}
func LoadConfig() *config {
defaultDataDir, err := utils.GetDefaultDataDir()
if err != nil {
fmt.Println("Could not get home directory: " + err.Error())
os.Exit(1)
}
cfg := config{
DataDir: defaultDataDir,
ConfigFile: "",
LogFile: "",
LogPrefix: "",
Boltz: &boltz.Boltz{
URL: "",
},
LND: &lnd.LND{
Host: "127.0.0.1",
Port: 10009,
Macaroon: "",
Certificate: "",
},
RPC: &rpcserver.RpcServer{
Host: "127.0.0.1",
Port: 9002,
RestHost: "127.0.0.1",
RestPort: 9003,
RestDisabled: false,
TlsCertPath: "",
TlsKeyPath: "",
NoMacaroons: false,
AdminMacaroonPath: "",
ReadonlyMacaroonPath: "",
},
Database: &database.Database{
Path: "",
},
}
parser := flags.NewParser(&cfg, flags.IgnoreUnknown)
_, err = parser.Parse()
if cfg.Help.ShowVersion {
fmt.Println(build.GetVersion())
fmt.Println("Built with: " + runtime.Version())
os.Exit(0)
}
if cfg.Help.ShowHelp {
parser.WriteHelp(os.Stdout)
os.Exit(0)
}
if err != nil {
printCouldNotParse(err)
}
cfg.ConfigFile = utils.ExpandDefaultPath(cfg.DataDir, cfg.ConfigFile, "boltz.toml")
if cfg.ConfigFile != "" {
_, err := toml.DecodeFile(cfg.ConfigFile, &cfg)
if err != nil {
fmt.Printf("Could not read config file: " + err.Error() + "\n")
}
}
_, err = flags.Parse(&cfg)
if err != nil {
printCouldNotParse(err)
}
fmt.Println("Using data dir: " + cfg.DataDir)
cfg.LogFile = utils.ExpandDefaultPath(cfg.DataDir, cfg.LogFile, "boltz.log")
cfg.Database.Path = utils.ExpandDefaultPath(cfg.DataDir, cfg.Database.Path, "boltz.db")
cfg.RPC.TlsKeyPath = utils.ExpandDefaultPath(cfg.DataDir, cfg.RPC.TlsKeyPath, "tls.key")
cfg.RPC.TlsCertPath = utils.ExpandDefaultPath(cfg.DataDir, cfg.RPC.TlsCertPath, "tls.cert")
macaroonDir := path.Join(cfg.DataDir, "macaroons")
cfg.RPC.AdminMacaroonPath = utils.ExpandDefaultPath(macaroonDir, cfg.RPC.AdminMacaroonPath, "admin.macaroon")
cfg.RPC.ReadonlyMacaroonPath = utils.ExpandDefaultPath(macaroonDir, cfg.RPC.ReadonlyMacaroonPath, "readonly.macaroon")
createDirIfNotExists(cfg.DataDir)
createDirIfNotExists(macaroonDir)
return &cfg
}
func createDirIfNotExists(dir string) {
if !utils.FileExists(dir) {
err := os.Mkdir(dir, 0700)
if err != nil {
fmt.Println("Could not create directory: " + err.Error())
os.Exit(1)
}
}
}
func printCouldNotParse(err error) {
fmt.Println("Could not parse arguments: " + err.Error())
os.Exit(1)
}