From 3d57397cad1aa7ac090e39d0039ea9bc8c439be1 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Tue, 30 Aug 2016 09:20:21 +0200 Subject: [PATCH] cmd: use type safe method of removing config field License: MIT Signed-off-by: Jakub Sztandera --- core/commands/config.go | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/core/commands/config.go b/core/commands/config.go index b2d953f43358..c16df43b474f 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "os/exec" "strings" @@ -144,48 +143,26 @@ included in the output of this command. }, Run: func(req cmds.Request, res cmds.Response) { - fname, err := config.Filename(req.InvocContext().ConfigRoot) + oConf, err := req.InvocContext().GetConfig() if err != nil { res.SetError(err, cmds.ErrNormal) return } - data, err := ioutil.ReadFile(fname) + // make copy of a current config + cMap, err := config.ToMap(oConf) if err != nil { res.SetError(err, cmds.ErrNormal) return } - - var cfg map[string]interface{} - err = json.Unmarshal(data, &cfg) + copyConfig, err := config.FromMap(cMap) if err != nil { res.SetError(err, cmds.ErrNormal) return } + copyConfig.Identity.PrivKey = "" - idmap, ok := cfg["Identity"].(map[string]interface{}) - if !ok { - res.SetError(errors.New("config has no identity"), cmds.ErrNormal) - return - } - - privKeyKey := "" // make sure we both find the name of privkey and we delete it - for key, _ := range idmap { - if strings.ToLower(key) == "privkey" { - if privKeyKey != "" { - res.SetError(errors.New("found multiple PrivKey keys"), cmds.ErrNormal) - return - } - privKeyKey = key - } - } - if privKeyKey == "" { - res.SetError(errors.New("haven't found PriveKey key"), cmds.ErrNormal) - } - - delete(idmap, privKeyKey) - - output, err := config.HumanOutput(cfg) + output, err := config.HumanOutput(copyConfig) if err != nil { res.SetError(err, cmds.ErrNormal) return