Skip to content

Commit c7a2889

Browse files
committed
fix: reference on nil pointer
1 parent 1c0a3d4 commit c7a2889

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

cmd/main.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"net/http"
@@ -169,6 +170,13 @@ var runCmd = &cli.Command{
169170
}
170171
}
171172

173+
b, err := json.MarshalIndent(cfg, "", " ")
174+
if err != nil {
175+
return err
176+
}
177+
fmt.Printf("config: \n %s \n ", string(b))
178+
log.Infof("config: \n %s \n ", string(b))
179+
172180
// todo replace it with stub
173181
if cfg.GetMessagerAPI().Addr == "" {
174182
return errors.New("messager api url is empty")
@@ -262,7 +270,7 @@ func updateFlag(cfg *config.Config, ctx *cli.Context) {
262270
cfg.Server.BoardPath = boardPath
263271
// todo: parse relative path to absolute path
264272

265-
updateApi := func(apiStr string, apiCfg *config.APIInfo) {
273+
updateApi := func(apiStr string, apiCfg *config.APIInfo) *config.APIInfo {
266274
if apiCfg == nil {
267275
apiCfg = &config.APIInfo{}
268276
}
@@ -275,30 +283,32 @@ func updateFlag(cfg *config.Config, ctx *cli.Context) {
275283
} else if commonToken != "" {
276284
apiCfg.Token = commonToken
277285
}
286+
287+
return apiCfg
278288
}
279289

280290
if ctx.IsSet(flagListen.Name) {
281291
cfg.Server.ListenAddr = ctx.String(flagListen.Name)
282292
}
283293
if ctx.IsSet(flagNodeAPI.Name) {
284-
updateApi(ctx.String(flagNodeAPI.Name), cfg.NodeAPI)
294+
cfg.NodeAPI = updateApi(ctx.String(flagNodeAPI.Name), cfg.NodeAPI)
285295
}
286296
if ctx.IsSet(flagMsgAPI.Name) {
287-
updateApi(ctx.String(flagMsgAPI.Name), cfg.MessagerAPI)
297+
cfg.MessagerAPI = updateApi(ctx.String(flagMsgAPI.Name), cfg.MessagerAPI)
288298
}
289299
if ctx.IsSet(flagMarketAPI.Name) {
290-
updateApi(ctx.String(flagMarketAPI.Name), cfg.MarketAPI)
300+
cfg.MarketAPI = updateApi(ctx.String(flagMarketAPI.Name), cfg.MarketAPI)
301+
}
302+
if ctx.IsSet(flagAuthAPI.Name) {
303+
cfg.AuthAPI = updateApi(ctx.String(flagAuthAPI.Name), cfg.AuthAPI)
304+
}
305+
if ctx.IsSet(flagMinerAPI.Name) {
306+
cfg.MinerAPI = updateApi(ctx.String(flagMinerAPI.Name), cfg.MinerAPI)
291307
}
292308
if ctx.IsSet(flagWalletAPI.Name) {
293309
updateApi(ctx.String(flagWalletAPI.Name), &cfg.WalletAPI)
294310
}
295-
if ctx.IsSet(flagAuthAPI.Name) {
296-
updateApi(ctx.String(flagAuthAPI.Name), cfg.AuthAPI)
297-
}
298311
if ctx.IsSet(flagDamoclesAPI.Name) {
299312
updateApi(ctx.String(flagDamoclesAPI.Name), &cfg.DamoclesAPI)
300313
}
301-
if ctx.IsSet(flagMinerAPI.Name) {
302-
updateApi(ctx.String(flagMinerAPI.Name), cfg.MinerAPI)
303-
}
304314
}

repo/config/config.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,43 @@ func mergeAPIInfo(prior, alternative *APIInfo) *APIInfo {
4444
}
4545

4646
func (c Config) GetNodeAPI() APIInfo {
47-
return *mergeAPIInfo(c.NodeAPI, c.ChainService)
47+
p := mergeAPIInfo(c.NodeAPI, c.ChainService)
48+
if p == nil {
49+
return APIInfo{}
50+
}
51+
return *p
4852
}
4953

5054
func (c Config) GetMessagerAPI() APIInfo {
51-
return *mergeAPIInfo(c.MessagerAPI, c.ChainService)
55+
p := mergeAPIInfo(c.MessagerAPI, c.ChainService)
56+
if p == nil {
57+
return APIInfo{}
58+
}
59+
return *p
5260
}
5361

5462
func (c Config) GetMarketAPI() APIInfo {
55-
return *mergeAPIInfo(c.MarketAPI, c.ChainService)
63+
p := mergeAPIInfo(c.MarketAPI, c.ChainService)
64+
if p == nil {
65+
return APIInfo{}
66+
}
67+
return *p
5668
}
5769

5870
func (c Config) GetAuthAPI() APIInfo {
59-
return *mergeAPIInfo(c.AuthAPI, c.ChainService)
71+
p := mergeAPIInfo(c.AuthAPI, c.ChainService)
72+
if p == nil {
73+
return APIInfo{}
74+
}
75+
return *p
6076
}
6177

6278
func (c Config) GetMinerAPI() APIInfo {
63-
return *mergeAPIInfo(c.MinerAPI, c.ChainService)
79+
p := mergeAPIInfo(c.MinerAPI, c.ChainService)
80+
if p == nil {
81+
return APIInfo{}
82+
}
83+
return *p
6484
}
6585

6686
type ServerConfig struct {

0 commit comments

Comments
 (0)