cmds/update: use new cmds lib
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Nov 3, 2018 at 23:40 UTC
5002d59d8c9d5fdacfd2812d0f08bd47c06c1f87
2 files changed
+27
-28
core/commands/external.go
+26
-27
@@ -8,9 +8,10 @@ import (
8
"os/exec"
9
"strings"
10
11
- cmds "github.com/ipfs/go-ipfs/commands"
11
+ commands "github.com/ipfs/go-ipfs/commands"
12
13
- "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
13
+ cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
14
+ cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
15
)
16
17
func ExternalBinary() *cmds.Command {
@@ -19,29 +20,27 @@ func ExternalBinary() *cmds.Command {
20
cmdkit.StringArg("args", false, true, "Arguments for subcommand."),
21
},
22
External: true,
22
- Run: func(req cmds.Request, res cmds.Response) {
23
- binname := strings.Join(append([]string{"ipfs"}, req.Path()...), "-")
23
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
24
+ binname := strings.Join(append([]string{"ipfs"}, req.Path...), "-")
25
_, err := exec.LookPath(binname)
26
if err != nil {
27
// special case for '--help' on uninstalled binaries.
27
- for _, arg := range req.Arguments() {
28
+ for _, arg := range req.Arguments {
29
if arg == "--help" || arg == "-h" {
30
buf := new(bytes.Buffer)
31
fmt.Fprintf(buf, "%s is an 'external' command.\n", binname)
32
fmt.Fprintf(buf, "It does not currently appear to be installed.\n")
33
fmt.Fprintf(buf, "Please refer to the ipfs documentation for instructions.\n")
33
- res.SetOutput(buf)
34
- return
34
+ return res.Emit(buf)
35
}
36
}
37
38
- res.SetError(fmt.Errorf("%s not installed", binname), cmdkit.ErrNormal)
39
- return
38
+ return fmt.Errorf("%s not installed", binname)
39
}
40
41
r, w := io.Pipe()
42
44
- cmd := exec.Command(binname, req.Arguments()...)
43
+ cmd := exec.Command(binname, req.Arguments...)
44
45
// TODO: make commands lib be able to pass stdin through daemon
46
//cmd.Stdin = req.Stdin()
@@ -50,39 +49,39 @@ func ExternalBinary() *cmds.Command {
49
cmd.Stderr = w
50
51
// setup env of child program
53
- env := os.Environ()
52
+ osenv := os.Environ()
53
54
// Get the node iff already defined.
56
- if req.InvocContext().Online {
57
- nd, err := req.InvocContext().GetNode()
55
+ if cctx, ok := env.(*commands.Context); ok && cctx.Online {
56
+ nd, err := cctx.GetNode()
57
if err != nil {
59
- res.SetError(fmt.Errorf(
60
- "failed to start ipfs node: %s",
61
- err,
62
- ), cmdkit.ErrFatal)
63
- return
58
+ return fmt.Errorf("failed to start ipfs node: %s", err)
59
}
65
- env = append(env, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
60
+ osenv = append(osenv, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
61
}
62
68
- cmd.Env = env
63
+ cmd.Env = osenv
64
65
err = cmd.Start()
66
if err != nil {
72
- res.SetError(fmt.Errorf("failed to start subcommand: %s", err), cmdkit.ErrNormal)
73
- return
67
+ return fmt.Errorf("failed to start subcommand: %s", err)
68
}
69
76
- res.SetOutput(r)
70
+ errC := make(chan error)
71
72
go func() {
73
+ var err error
74
+ defer func() { errC <- err }()
75
err = cmd.Wait()
80
- if err != nil {
81
- res.SetError(err, cmdkit.ErrNormal)
82
- }
83
-
76
w.Close()
77
}()
78
+
79
+ err = res.Emit(r)
80
+ if err != nil {
81
+ return err
82
+ }
83
+
84
+ return <-errC
85
},
86
}
87
}
core/commands/root.go
+1
-1
@@ -139,7 +139,7 @@ var rootSubcommands = map[string]*cmds.Command{
139
"swarm": SwarmCmd,
140
"tar": TarCmd,
141
"file": unixfs.UnixFSCmd,
142
- "update": lgc.NewCommand(ExternalBinary()),
142
+ "update": ExternalBinary(),
143
"urlstore": urlStoreCmd,
144
"version": VersionCmd,
145
"shutdown": daemonShutdownCmd,