Skip to content

Commit d51bcc6

Browse files
overboolStebalien
authored andcommitted
cmds/update: use new cmds lib
License: MIT Signed-off-by: Overbool <[email protected]>
1 parent 19a6620 commit d51bcc6

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

core/commands/external.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"os/exec"
99
"strings"
1010

11-
cmds "github.com/ipfs/go-ipfs/commands"
11+
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
1212

13-
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
13+
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
14+
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
1415
)
1516

1617
func ExternalBinary() *cmds.Command {
@@ -19,29 +20,27 @@ func ExternalBinary() *cmds.Command {
1920
cmdkit.StringArg("args", false, true, "Arguments for subcommand."),
2021
},
2122
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...), "-")
2425
_, err := exec.LookPath(binname)
2526
if err != nil {
2627
// special case for '--help' on uninstalled binaries.
27-
for _, arg := range req.Arguments() {
28+
for _, arg := range req.Arguments {
2829
if arg == "--help" || arg == "-h" {
2930
buf := new(bytes.Buffer)
3031
fmt.Fprintf(buf, "%s is an 'external' command.\n", binname)
3132
fmt.Fprintf(buf, "It does not currently appear to be installed.\n")
3233
fmt.Fprintf(buf, "Please refer to the ipfs documentation for instructions.\n")
33-
res.SetOutput(buf)
34-
return
34+
return res.Emit(buf)
3535
}
3636
}
3737

38-
res.SetError(fmt.Errorf("%s not installed", binname), cmdkit.ErrNormal)
39-
return
38+
return fmt.Errorf("%s not installed", binname)
4039
}
4140

4241
r, w := io.Pipe()
4342

44-
cmd := exec.Command(binname, req.Arguments()...)
43+
cmd := exec.Command(binname, req.Arguments...)
4544

4645
// TODO: make commands lib be able to pass stdin through daemon
4746
//cmd.Stdin = req.Stdin()
@@ -50,39 +49,40 @@ func ExternalBinary() *cmds.Command {
5049
cmd.Stderr = w
5150

5251
// setup env of child program
53-
env := os.Environ()
52+
e := os.Environ()
5453

5554
// Get the node iff already defined.
56-
if req.InvocContext().Online {
57-
nd, err := req.InvocContext().GetNode()
58-
if err != nil {
59-
res.SetError(fmt.Errorf(
60-
"failed to start ipfs node: %s",
61-
err,
62-
), cmdkit.ErrFatal)
63-
return
64-
}
65-
env = append(env, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
55+
nd, err := cmdenv.GetNode(env)
56+
if err != nil {
57+
return fmt.Errorf("failed to start ipfs node: %s", err)
6658
}
6759

68-
cmd.Env = env
60+
if nd.OnlineMode() {
61+
e = append(e, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
62+
}
63+
64+
cmd.Env = e
6965

7066
err = cmd.Start()
7167
if err != nil {
72-
res.SetError(fmt.Errorf("failed to start subcommand: %s", err), cmdkit.ErrNormal)
73-
return
68+
return fmt.Errorf("failed to start subcommand: %s", err)
7469
}
7570

76-
res.SetOutput(r)
71+
errC := make(chan error)
7772

7873
go func() {
74+
var err error
75+
defer func() { errC <- err }()
7976
err = cmd.Wait()
80-
if err != nil {
81-
res.SetError(err, cmdkit.ErrNormal)
82-
}
83-
8477
w.Close()
8578
}()
79+
80+
err = res.Emit(r)
81+
if err != nil {
82+
return err
83+
}
84+
85+
return <-errC
8686
},
8787
}
8888
}

core/commands/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ var rootSubcommands = map[string]*cmds.Command{
139139
"swarm": SwarmCmd,
140140
"tar": TarCmd,
141141
"file": unixfs.UnixFSCmd,
142-
"update": lgc.NewCommand(ExternalBinary()),
142+
"update": ExternalBinary(),
143143
"urlstore": urlStoreCmd,
144144
"version": VersionCmd,
145145
"shutdown": daemonShutdownCmd,

0 commit comments

Comments
 (0)