-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
168 lines (131 loc) · 4.04 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/Qitmeer/qitmeer-cli/commands"
"github.com/Qitmeer/qitmeer-cli/rpc/client"
)
// Config file
type Config struct {
configFile string
Debug bool //print log
Jq bool //output by json format
client.Config
}
var preCfg *Config
func init() {
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: true,
})
cobra.EnableCommandSorting = false
bindFlags()
}
func main() {
commands.RootCmd.PersistentPreRunE = LoadConfig
if err := commands.RootCmd.Execute(); err != nil {
os.Exit(1)
}
// /fmt.Println(commands.MakeTpl())
return
}
// InitFlags init flags
func bindFlags() {
preCfg = &Config{}
gFlags := commands.RootCmd.PersistentFlags()
gFlags.StringVarP(&preCfg.configFile, "config", "c", "cli.toml", "config file")
gFlags.StringVarP(&preCfg.RPCServer, "rpcserver", "s", "127.0.0.1:18131", "RPC server to connect to")
gFlags.StringVarP(&preCfg.RPCUser, "rpcuser", "u", "", "RPC username")
gFlags.StringVarP(&preCfg.RPCPassword, "rpcpassword", "P", "", "RPC password")
gFlags.StringVar(&preCfg.RPCCert, "rpccert", "", "RPC server certificate file path")
gFlags.BoolVar(&preCfg.NoTLS, "notls", true, "Do not verify tls certificates (not recommended!)")
gFlags.BoolVar(&preCfg.TLSSkipVerify, "tlsskipverify", true, "Do not verify tls certificates (not recommended!)")
gFlags.StringVar(&preCfg.Proxy, "proxy", "", "Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)")
gFlags.StringVar(&preCfg.ProxyUser, "proxyuser", "", "Username for proxy server")
gFlags.StringVar(&preCfg.ProxyPass, "proxypass", "", "Password for proxy server")
gFlags.StringVar(&preCfg.Timeout, "timeout", "30s", "rpc timeout,s:second h:hour m:minute")
gFlags.BoolVar(&preCfg.Debug, "debug", false, "debug print log")
gFlags.BoolVar(&preCfg.Jq, "jq", false, "print json format")
}
// LoadConfig config file and flags
func LoadConfig(cmd *cobra.Command, args []string) (err error) {
// debug
if cmd.Flag("debug").Changed && preCfg.Debug {
log.SetLevel(log.TraceLevel)
}
// load configfile ane merge command ,but don't udpate configfile
fileCfg := &Config{}
_, err = toml.DecodeFile(preCfg.configFile, fileCfg)
if err != nil {
//if not set config file and default cli.toml decode err, use default set only.
if !cmd.Flag("config").Changed {
if fExit, _ := FileExists(preCfg.configFile); fExit {
return fmt.Errorf("config file err: %s", err)
}
commands.RPCCfg = &preCfg.Config
commands.Jq = preCfg.Jq
return nil
}
return fmt.Errorf("config file err: %s", err)
}
fileCfg.configFile = preCfg.configFile
if cmd.Flag("rpcserver").Changed {
fileCfg.RPCServer = preCfg.RPCServer
}
if cmd.Flag("rpcuser").Changed {
fileCfg.RPCUser = preCfg.RPCUser
}
if cmd.Flag("rpcpassword").Changed {
fileCfg.RPCPassword = preCfg.RPCPassword
}
if cmd.Flag("rpccert").Changed {
fileCfg.RPCCert = preCfg.RPCCert
}
if cmd.Flag("notls").Changed {
fileCfg.NoTLS = preCfg.NoTLS
}
if cmd.Flag("tlsskipverify").Changed {
fileCfg.TLSSkipVerify = preCfg.TLSSkipVerify
}
if cmd.Flag("proxy").Changed {
fileCfg.Proxy = preCfg.Proxy
}
if cmd.Flag("proxyuser").Changed {
fileCfg.ProxyUser = preCfg.ProxyUser
}
if cmd.Flag("proxypass").Changed {
fileCfg.ProxyPass = preCfg.ProxyPass
}
if cmd.Flag("timeout").Changed {
fileCfg.Timeout = preCfg.Timeout
}
if cmd.Flag("debug").Changed {
fileCfg.Debug = preCfg.Debug
}
if cmd.Flag("jq").Changed {
fileCfg.Jq = preCfg.Jq
}
log.Debug("fileCfg: ", *fileCfg)
commands.RPCCfg = &fileCfg.Config
commands.Jq = fileCfg.Jq
return nil
//save
// buf := new(bytes.Buffer)
// if err := toml.NewEncoder(buf).Encode(*fileCfg); err != nil {
// log.Fatal(err)
// }
//return ioutil.WriteFile(fileCfg.configFile, buf.Bytes(), 0666)
}
// FileExists reports whether the named file or directory exists.
func FileExists(filePath string) (bool, error) {
_, err := os.Stat(filePath)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}