From 90aae6e7e9683c1fd4e1661ab5e8674aa28e3bda Mon Sep 17 00:00:00 2001 From: Overbool Date: Sat, 3 Nov 2018 23:40:36 +0800 Subject: [PATCH] cmds/update: use new cmds lib License: MIT Signed-off-by: Overbool --- core/commands/external.go | 60 +++++++++++++++++++++------------------ core/commands/root.go | 2 +- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/core/commands/external.go b/core/commands/external.go index ad32678b1e8d..cd926fd330fe 100644 --- a/core/commands/external.go +++ b/core/commands/external.go @@ -8,9 +8,12 @@ import ( "os/exec" "strings" - cmds "github.com/ipfs/go-ipfs/commands" + oldcmds "github.com/ipfs/go-ipfs/commands" + cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" + e "github.com/ipfs/go-ipfs/core/commands/e" - "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" + cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds" + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) func ExternalBinary() *cmds.Command { @@ -19,29 +22,27 @@ func ExternalBinary() *cmds.Command { cmdkit.StringArg("args", false, true, "Arguments for subcommand."), }, External: true, - Run: func(req cmds.Request, res cmds.Response) { - binname := strings.Join(append([]string{"ipfs"}, req.Path()...), "-") + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + binname := strings.Join(append([]string{"ipfs"}, req.Path...), "-") _, err := exec.LookPath(binname) if err != nil { // special case for '--help' on uninstalled binaries. - for _, arg := range req.Arguments() { + for _, arg := range req.Arguments { if arg == "--help" || arg == "-h" { buf := new(bytes.Buffer) fmt.Fprintf(buf, "%s is an 'external' command.\n", binname) fmt.Fprintf(buf, "It does not currently appear to be installed.\n") fmt.Fprintf(buf, "Please refer to the ipfs documentation for instructions.\n") - res.SetOutput(buf) - return + return res.Emit(buf) } } - res.SetError(fmt.Errorf("%s not installed", binname), cmdkit.ErrNormal) - return + return fmt.Errorf("%s not installed", binname) } r, w := io.Pipe() - cmd := exec.Command(binname, req.Arguments()...) + cmd := exec.Command(binname, req.Arguments...) // TODO: make commands lib be able to pass stdin through daemon //cmd.Stdin = req.Stdin() @@ -50,39 +51,44 @@ func ExternalBinary() *cmds.Command { cmd.Stderr = w // setup env of child program - env := os.Environ() + osenv := os.Environ() // Get the node iff already defined. - if req.InvocContext().Online { - nd, err := req.InvocContext().GetNode() + ctx, ok := env.(*oldcmds.Context) + if !ok { + return e.TypeErr(ctx, env) + } + + if ctx.Online { + nd, err := cmdenv.GetNode(env) if err != nil { - res.SetError(fmt.Errorf( - "failed to start ipfs node: %s", - err, - ), cmdkit.ErrFatal) - return + return fmt.Errorf("failed to start ipfs node: %s", err) } - env = append(env, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode())) + osenv = append(osenv, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode())) } - cmd.Env = env + cmd.Env = osenv err = cmd.Start() if err != nil { - res.SetError(fmt.Errorf("failed to start subcommand: %s", err), cmdkit.ErrNormal) - return + return fmt.Errorf("failed to start subcommand: %s", err) } - res.SetOutput(r) + errC := make(chan error) go func() { + var err error + defer func() { errC <- err }() err = cmd.Wait() - if err != nil { - res.SetError(err, cmdkit.ErrNormal) - } - w.Close() }() + + err = res.Emit(r) + if err != nil { + return err + } + + return <-errC }, } } diff --git a/core/commands/root.go b/core/commands/root.go index 7796b7d65fc7..7a28525115f6 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -139,7 +139,7 @@ var rootSubcommands = map[string]*cmds.Command{ "swarm": SwarmCmd, "tar": TarCmd, "file": unixfs.UnixFSCmd, - "update": lgc.NewCommand(ExternalBinary()), + "update": ExternalBinary(), "urlstore": urlStoreCmd, "version": VersionCmd, "shutdown": daemonShutdownCmd,