Skip to content

Commit

Permalink
cmds: use w.Write directly
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Nov 6, 2018
1 parent 01dcf34 commit 51b05c6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
55 changes: 39 additions & 16 deletions core/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"os/exec"
"strings"

oldcmds "github.com/ipfs/go-ipfs/commands"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
repo "github.com/ipfs/go-ipfs/repo"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"

"gx/ipfs/QmP2i47tnU23ijdshrZtuvrSkQPtf9HhsMb9fwGVe8owj2/jsondiff"
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
config "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

// ConfigUpdateOutput is config profile apply command's output
Expand Down Expand Up @@ -86,8 +86,11 @@ Set the value of the 'Datastore.Path' key:
default:
}

ctx := env.(*oldcmds.Context)
r, err := fsrepo.Open(ctx.ConfigRoot)
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}
r, err := fsrepo.Open(cfgRoot)
if err != nil {
return err
}
Expand Down Expand Up @@ -128,7 +131,9 @@ Set the value of the 'Datastore.Path' key:
if err != nil {
return err
}
fmt.Fprintln(w, string(buf))
buf = append(buf, byte('\n'))

w.Write(buf)
return nil
}),
},
Expand All @@ -144,9 +149,12 @@ NOTE: For security reasons, this command will omit your private key. If you woul
},
Type: map[string]interface{}{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := env.(*oldcmds.Context)
cfgPath := ctx.ConfigRoot
fname, err := config.Filename(cfgPath)
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}

fname, err := config.Filename(cfgRoot)
if err != nil {
return err
}
Expand Down Expand Up @@ -175,7 +183,9 @@ NOTE: For security reasons, this command will omit your private key. If you woul
if err != nil {
return err
}
fmt.Fprintln(w, string(buf))
buf = append(buf, byte('\n'))
w.Write(buf)

return nil
}),
},
Expand Down Expand Up @@ -232,8 +242,12 @@ variable set to your preferred text editor.
},

Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := env.(*oldcmds.Context)
filename, err := config.Filename(ctx.ConfigRoot)
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}

filename, err := config.Filename(cfgRoot)
if err != nil {
return err
}
Expand All @@ -255,8 +269,12 @@ can't be undone.
cmdkit.FileArg("file", true, false, "The file to use as the new config."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := env.(*oldcmds.Context)
r, err := fsrepo.Open(ctx.ConfigRoot)
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}

r, err := fsrepo.Open(cfgRoot)
if err != nil {
return err
}
Expand Down Expand Up @@ -303,8 +321,12 @@ var configProfileApplyCmd = &cmds.Command{
}

dryRun, _ := req.Options["dry-run"].(bool)
ctx := env.(*oldcmds.Context)
oldCfg, newCfg, err := transformConfig(ctx.ConfigRoot, req.Arguments[0], profile.Transform, dryRun)
cfgRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}

oldCfg, newCfg, err := transformConfig(cfgRoot, req.Arguments[0], profile.Transform, dryRun)
if err != nil {
return err
}
Expand All @@ -329,7 +351,8 @@ var configProfileApplyCmd = &cmds.Command{
diff := jsondiff.Compare(out.OldCfg, out.NewCfg)
buf := jsondiff.Format(diff)

fmt.Fprint(w, string(buf))
w.Write(buf)

return nil
}),
},
Expand Down
8 changes: 4 additions & 4 deletions core/commands/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ baz

// error if we aren't running node in online mode
if nd.LocalMode() {
return err
return ErrNotOnline
}

fsdir, found := req.Options[mountIPFSPathOptionName].(string)
Expand All @@ -120,9 +120,9 @@ baz
Type: config.Mounts{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, mounts *config.Mounts) error {
s := fmt.Sprintf("IPFS mounted at: %s\n", mounts.IPFS)
s += fmt.Sprintf("IPNS mounted at: %s\n", mounts.IPNS)
fmt.Fprint(w, s)
fmt.Fprintf(w, "IPFS mounted at: %s\n", mounts.IPFS)
fmt.Fprintf(w, "IPNS mounted at: %s\n", mounts.IPNS)

return nil
}),
},
Expand Down

0 comments on commit 51b05c6

Please sign in to comment.