Unverified 提交 085217eb 作者: Steven Allen 提交者: GitHub

Merge pull request #5730 from overbool/refactor/cmds/update

cmds/update: use new cmds lib
...@@ -8,9 +8,10 @@ import ( ...@@ -8,9 +8,10 @@ import (
"os/exec" "os/exec"
"strings" "strings"
cmds "github.com/ipfs/go-ipfs/commands" commands "github.com/ipfs/go-ipfs/commands"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
) )
func ExternalBinary() *cmds.Command { func ExternalBinary() *cmds.Command {
...@@ -19,29 +20,27 @@ func ExternalBinary() *cmds.Command { ...@@ -19,29 +20,27 @@ func ExternalBinary() *cmds.Command {
cmdkit.StringArg("args", false, true, "Arguments for subcommand."), cmdkit.StringArg("args", false, true, "Arguments for subcommand."),
}, },
External: true, External: true,
Run: func(req cmds.Request, res cmds.Response) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
binname := strings.Join(append([]string{"ipfs"}, req.Path()...), "-") binname := strings.Join(append([]string{"ipfs"}, req.Path...), "-")
_, err := exec.LookPath(binname) _, err := exec.LookPath(binname)
if err != nil { if err != nil {
// special case for '--help' on uninstalled binaries. // special case for '--help' on uninstalled binaries.
for _, arg := range req.Arguments() { for _, arg := range req.Arguments {
if arg == "--help" || arg == "-h" { if arg == "--help" || arg == "-h" {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%s is an 'external' command.\n", binname) 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, "It does not currently appear to be installed.\n")
fmt.Fprintf(buf, "Please refer to the ipfs documentation for instructions.\n") fmt.Fprintf(buf, "Please refer to the ipfs documentation for instructions.\n")
res.SetOutput(buf) return res.Emit(buf)
return
} }
} }
res.SetError(fmt.Errorf("%s not installed", binname), cmdkit.ErrNormal) return fmt.Errorf("%s not installed", binname)
return
} }
r, w := io.Pipe() 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 // TODO: make commands lib be able to pass stdin through daemon
//cmd.Stdin = req.Stdin() //cmd.Stdin = req.Stdin()
...@@ -50,39 +49,39 @@ func ExternalBinary() *cmds.Command { ...@@ -50,39 +49,39 @@ func ExternalBinary() *cmds.Command {
cmd.Stderr = w cmd.Stderr = w
// setup env of child program // setup env of child program
env := os.Environ() osenv := os.Environ()
// Get the node iff already defined. // Get the node iff already defined.
if req.InvocContext().Online { if cctx, ok := env.(*commands.Context); ok && cctx.Online {
nd, err := req.InvocContext().GetNode() nd, err := cctx.GetNode()
if err != nil { if err != nil {
res.SetError(fmt.Errorf( return fmt.Errorf("failed to start ipfs node: %s", err)
"failed to start ipfs node: %s",
err,
), cmdkit.ErrFatal)
return
} }
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() err = cmd.Start()
if err != nil { if err != nil {
res.SetError(fmt.Errorf("failed to start subcommand: %s", err), cmdkit.ErrNormal) return fmt.Errorf("failed to start subcommand: %s", err)
return
} }
res.SetOutput(r) errC := make(chan error)
go func() { go func() {
var err error
defer func() { errC <- err }()
err = cmd.Wait() err = cmd.Wait()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
}
w.Close() w.Close()
}() }()
err = res.Emit(r)
if err != nil {
return err
}
return <-errC
}, },
} }
} }
...@@ -139,7 +139,7 @@ var rootSubcommands = map[string]*cmds.Command{ ...@@ -139,7 +139,7 @@ var rootSubcommands = map[string]*cmds.Command{
"swarm": SwarmCmd, "swarm": SwarmCmd,
"tar": TarCmd, "tar": TarCmd,
"file": unixfs.UnixFSCmd, "file": unixfs.UnixFSCmd,
"update": lgc.NewCommand(ExternalBinary()), "update": ExternalBinary(),
"urlstore": urlStoreCmd, "urlstore": urlStoreCmd,
"version": VersionCmd, "version": VersionCmd,
"shutdown": daemonShutdownCmd, "shutdown": daemonShutdownCmd,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论