@cryptotaxi247 / kubo / commits / 96a072675

address CR feedback

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Dec 6, 2015 at 15:20 UTC 96a0726759ea29521e5814c9a16e618f08872c55
3 files changed +79 -68
commands/command.go
-67
@@ -9,12 +9,9 @@ output to the user, including text, JSON, and XML marshallers.
9 package commands
10
11 import (
12 - "bytes"
12 "errors"
13 "fmt"
14 "io"
16 - "os"
17 - "os/exec"
15 "reflect"
16 "strings"
17
@@ -269,67 +266,3 @@ func checkArgValue(v string, found bool, def Argument) error {
266 func ClientError(msg string) error {
267 return &Error{Code: ErrClient, Message: msg}
268 }
272 -
273 -func ExternalBinary() *Command {
274 - return &Command{
275 - Arguments: []Argument{
276 - StringArg("args", false, true, "arguments for subcommand"),
277 - },
278 - External: true,
279 - Run: func(req Request, res Response) {
280 - binname := strings.Join(append([]string{"ipfs"}, req.Path()...), "-")
281 - _, err := exec.LookPath(binname)
282 - if err != nil {
283 - // special case for '--help' on uninstalled binaries.
284 - if req.Arguments()[0] == "--help" {
285 - buf := new(bytes.Buffer)
286 - fmt.Fprintf(buf, "%s is an 'external' command.\n", binname)
287 - fmt.Fprintf(buf, "it does not currently appear to be installed.\n")
288 - fmt.Fprintf(buf, "please refer to the ipfs documentation for instructions\n")
289 - res.SetOutput(buf)
290 - return
291 - }
292 -
293 - res.SetError(fmt.Errorf("%s not installed."), ErrNormal)
294 - return
295 - }
296 -
297 - r, w := io.Pipe()
298 -
299 - cmd := exec.Command(binname, req.Arguments()...)
300 -
301 - // TODO: make commands lib be able to pass stdin through daemon
302 - //cmd.Stdin = req.Stdin()
303 - cmd.Stdin = io.LimitReader(nil, 0)
304 - cmd.Stdout = w
305 - cmd.Stderr = w
306 -
307 - // setup env of child program
308 - env := os.Environ()
309 -
310 - nd, err := req.InvocContext().GetNode()
311 - if err == nil {
312 - env = append(env, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
313 - }
314 -
315 - cmd.Env = env
316 -
317 - err = cmd.Start()
318 - if err != nil {
319 - res.SetError(fmt.Errorf("failed to start subcommand: %s", err), ErrNormal)
320 - return
321 - }
322 -
323 - res.SetOutput(r)
324 -
325 - go func() {
326 - err = cmd.Wait()
327 - if err != nil {
328 - res.SetError(err, ErrNormal)
329 - }
330 -
331 - w.Close()
332 - }()
333 - },
334 - }
335 -}
core/commands/external.go new
+78
@@ -0,0 +1,78 @@
1 +package commands
2 +
3 +import (
4 + "bytes"
5 + "fmt"
6 + "io"
7 + "os"
8 + "os/exec"
9 + "strings"
10 +
11 + cmds "github.com/ipfs/go-ipfs/commands"
12 +)
13 +
14 +func ExternalBinary() *cmds.Command {
15 + return &cmds.Command{
16 + Arguments: []cmds.Argument{
17 + cmds.StringArg("args", false, true, "arguments for subcommand"),
18 + },
19 + External: true,
20 + Run: func(req cmds.Request, res cmds.Response) {
21 + binname := strings.Join(append([]string{"ipfs"}, req.Path()...), "-")
22 + _, err := exec.LookPath(binname)
23 + if err != nil {
24 + // special case for '--help' on uninstalled binaries.
25 + for _, arg := range req.Arguments() {
26 + if arg == "--help" || arg == "-h" {
27 + buf := new(bytes.Buffer)
28 + fmt.Fprintf(buf, "%s is an 'external' command.\n", binname)
29 + fmt.Fprintf(buf, "it does not currently appear to be installed.\n")
30 + fmt.Fprintf(buf, "please refer to the ipfs documentation for instructions\n")
31 + res.SetOutput(buf)
32 + return
33 + }
34 + }
35 +
36 + res.SetError(fmt.Errorf("%s not installed."), cmds.ErrNormal)
37 + return
38 + }
39 +
40 + r, w := io.Pipe()
41 +
42 + cmd := exec.Command(binname, req.Arguments()...)
43 +
44 + // TODO: make commands lib be able to pass stdin through daemon
45 + //cmd.Stdin = req.Stdin()
46 + cmd.Stdin = io.LimitReader(nil, 0)
47 + cmd.Stdout = w
48 + cmd.Stderr = w
49 +
50 + // setup env of child program
51 + env := os.Environ()
52 +
53 + nd, err := req.InvocContext().GetNode()
54 + if err == nil {
55 + env = append(env, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
56 + }
57 +
58 + cmd.Env = env
59 +
60 + err = cmd.Start()
61 + if err != nil {
62 + res.SetError(fmt.Errorf("failed to start subcommand: %s", err), cmds.ErrNormal)
63 + return
64 + }
65 +
66 + res.SetOutput(r)
67 +
68 + go func() {
69 + err = cmd.Wait()
70 + if err != nil {
71 + res.SetError(err, cmds.ErrNormal)
72 + }
73 +
74 + w.Close()
75 + }()
76 + },
77 + }
78 +}
core/commands/root.go
+1 -1
@@ -111,7 +111,7 @@ var rootSubcommands = map[string]*cmds.Command{
111 "tar": TarCmd,
112 "tour": tourCmd,
113 "file": unixfs.UnixFSCmd,
114 - "update": cmds.ExternalBinary(),
114 + "update": ExternalBinary(),
115 "version": VersionCmd,
116 "bitswap": BitswapCmd,
117 }