Skip to content

Commit

Permalink
cmd: use type safe method of removing config field
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
  • Loading branch information
Kubuxu committed Aug 30, 2016
1 parent 481f9b2 commit 3d57397
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3d57397

Please sign in to comment.