Commit 96a07267 authored by Jeromy's avatar Jeromy

address CR feedback

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent b99b7782
......@@ -9,12 +9,9 @@ output to the user, including text, JSON, and XML marshallers.
package commands
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"reflect"
"strings"
......@@ -269,67 +266,3 @@ func checkArgValue(v string, found bool, def Argument) error {
func ClientError(msg string) error {
return &Error{Code: ErrClient, Message: msg}
}
func ExternalBinary() *Command {
return &Command{
Arguments: []Argument{
StringArg("args", false, true, "arguments for subcommand"),
},
External: true,
Run: func(req Request, res Response) {
binname := strings.Join(append([]string{"ipfs"}, req.Path()...), "-")
_, err := exec.LookPath(binname)
if err != nil {
// special case for '--help' on uninstalled binaries.
if req.Arguments()[0] == "--help" {
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
}
res.SetError(fmt.Errorf("%s not installed."), ErrNormal)
return
}
r, w := io.Pipe()
cmd := exec.Command(binname, req.Arguments()...)
// TODO: make commands lib be able to pass stdin through daemon
//cmd.Stdin = req.Stdin()
cmd.Stdin = io.LimitReader(nil, 0)
cmd.Stdout = w
cmd.Stderr = w
// setup env of child program
env := os.Environ()
nd, err := req.InvocContext().GetNode()
if err == nil {
env = append(env, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
}
cmd.Env = env
err = cmd.Start()
if err != nil {
res.SetError(fmt.Errorf("failed to start subcommand: %s", err), ErrNormal)
return
}
res.SetOutput(r)
go func() {
err = cmd.Wait()
if err != nil {
res.SetError(err, ErrNormal)
}
w.Close()
}()
},
}
}
package commands
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
)
func ExternalBinary() *cmds.Command {
return &cmds.Command{
Arguments: []cmds.Argument{
cmds.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()...), "-")
_, err := exec.LookPath(binname)
if err != nil {
// special case for '--help' on uninstalled binaries.
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
}
}
res.SetError(fmt.Errorf("%s not installed."), cmds.ErrNormal)
return
}
r, w := io.Pipe()
cmd := exec.Command(binname, req.Arguments()...)
// TODO: make commands lib be able to pass stdin through daemon
//cmd.Stdin = req.Stdin()
cmd.Stdin = io.LimitReader(nil, 0)
cmd.Stdout = w
cmd.Stderr = w
// setup env of child program
env := os.Environ()
nd, err := req.InvocContext().GetNode()
if err == nil {
env = append(env, fmt.Sprintf("IPFS_ONLINE=%t", nd.OnlineMode()))
}
cmd.Env = env
err = cmd.Start()
if err != nil {
res.SetError(fmt.Errorf("failed to start subcommand: %s", err), cmds.ErrNormal)
return
}
res.SetOutput(r)
go func() {
err = cmd.Wait()
if err != nil {
res.SetError(err, cmds.ErrNormal)
}
w.Close()
}()
},
}
}
......@@ -111,7 +111,7 @@ var rootSubcommands = map[string]*cmds.Command{
"tar": TarCmd,
"tour": tourCmd,
"file": unixfs.UnixFSCmd,
"update": cmds.ExternalBinary(),
"update": ExternalBinary(),
"version": VersionCmd,
"bitswap": BitswapCmd,
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment