提交 b034a0d8 作者: Jeromy

and special case that one thing because why not

License: MIT
Signed-off-by: 's avatarJeromy <why@ipfs.io>
上级 5d301cb8
...@@ -61,6 +61,16 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c ...@@ -61,6 +61,16 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
} }
} }
// This is an ugly hack to maintain our current CLI interface while fixing
// other stdin usage bugs. Let this serve as a warning, be careful about the
// choices you make, they will haunt you forever.
if len(path) == 2 && path[0] == "bootstrap" {
if (path[1] == "add" && opts["default"] == true) ||
(path[1] == "rm" && opts["all"] == true) {
stdin = nil
}
}
stringArgs, fileArgs, err := parseArgs(stringVals, stdin, cmd.Arguments, recursive, hidden, root) stringArgs, fileArgs, err := parseArgs(stringVals, stdin, cmd.Arguments, recursive, hidden, root)
if err != nil { if err != nil {
return req, cmd, path, err return req, cmd, path, err
...@@ -301,17 +311,10 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi ...@@ -301,17 +311,10 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
stringArgs, inputs = append(stringArgs, inputs[0]), inputs[1:] stringArgs, inputs = append(stringArgs, inputs[0]), inputs[1:]
} else { } else {
if stdin != nil && argDef.SupportsStdin && !fillingVariadic { if stdin != nil && argDef.SupportsStdin && !fillingVariadic {
fname := "" if err := printReadInfo(stdin, msgStdinInfo); err == nil {
istty, err := isTty(stdin) fileArgs[stdin.Name()] = files.NewReaderFile(stdinSpecialName, "", stdin, nil)
if err != nil { stdin = nil
return nil, nil, err
}
if istty {
fname = stdinSpecialName
} }
fileArgs[stdin.Name()] = files.NewReaderFile(fname, "", stdin, nil)
stdin = nil
} }
} }
case cmds.ArgFile: case cmds.ArgFile:
......
...@@ -128,7 +128,7 @@ func getQuery(req cmds.Request) (string, error) { ...@@ -128,7 +128,7 @@ func getQuery(req cmds.Request) (string, error) {
query.Set(k, str) query.Set(k, str)
} }
args := req.Arguments() args := req.StringArguments()
argDefs := req.Command().Arguments argDefs := req.Command().Arguments
argDefIndex := 0 argDefIndex := 0
......
...@@ -56,7 +56,7 @@ func (rl *ReqLog) Add(req Request) *ReqLogEntry { ...@@ -56,7 +56,7 @@ func (rl *ReqLog) Add(req Request) *ReqLogEntry {
Active: true, Active: true,
Command: strings.Join(req.Path(), "/"), Command: strings.Join(req.Path(), "/"),
Options: req.Options(), Options: req.Options(),
Args: req.Arguments(), Args: req.StringArguments(),
ID: rl.nextID, ID: rl.nextID,
req: req, req: req,
log: rl, log: rl,
......
...@@ -71,6 +71,7 @@ type Request interface { ...@@ -71,6 +71,7 @@ type Request interface {
SetOption(name string, val interface{}) SetOption(name string, val interface{})
SetOptions(opts OptMap) error SetOptions(opts OptMap) error
Arguments() []string Arguments() []string
StringArguments() []string
SetArguments([]string) SetArguments([]string)
Files() files.File Files() files.File
SetFiles(files.File) SetFiles(files.File)
...@@ -168,6 +169,10 @@ func (r *request) SetOptions(opts OptMap) error { ...@@ -168,6 +169,10 @@ func (r *request) SetOptions(opts OptMap) error {
return r.ConvertOptions() return r.ConvertOptions()
} }
func (r *request) StringArguments() []string {
return r.arguments
}
// Arguments returns the arguments slice // Arguments returns the arguments slice
func (r *request) Arguments() []string { func (r *request) Arguments() []string {
if r.haveVarArgsFromStdin() { if r.haveVarArgsFromStdin() {
...@@ -207,7 +212,7 @@ func (r *request) haveVarArgsFromStdin() bool { ...@@ -207,7 +212,7 @@ func (r *request) haveVarArgsFromStdin() bool {
} }
last := r.cmd.Arguments[len(r.cmd.Arguments)-1] last := r.cmd.Arguments[len(r.cmd.Arguments)-1]
return last.SupportsStdin && last.Type == ArgString && return last.SupportsStdin && last.Type == ArgString && (last.Required || last.Variadic) &&
len(r.arguments) < len(r.cmd.Arguments) len(r.arguments) < len(r.cmd.Arguments)
} }
...@@ -234,10 +239,6 @@ func (r *request) VarArgs(f func(string) error) error { ...@@ -234,10 +239,6 @@ func (r *request) VarArgs(f func(string) error) error {
return err return err
} }
if fi.FileName() == "*stdin*" {
fmt.Fprintln(os.Stderr, "ipfs: Reading from stdin; send Ctrl-d to stop.")
}
scan := bufio.NewScanner(fi) scan := bufio.NewScanner(fi)
for scan.Scan() { for scan.Scan() {
err := f(scan.Text()) err := f(scan.Text())
......
...@@ -58,7 +58,7 @@ EXAMPLE: ...@@ -58,7 +58,7 @@ EXAMPLE:
`, `,
}, },
Arguments: []cmds.Argument{ Arguments: []cmds.Argument{
cmds.StringArg("peerid", false, false, "Peer.ID of node to look up.").EnableStdin(), cmds.StringArg("peerid", false, false, "Peer.ID of node to look up."),
}, },
Options: []cmds.Option{ Options: []cmds.Option{
cmds.StringOption("format", "f", "Optional output format."), cmds.StringOption("format", "f", "Optional output format."),
......
...@@ -117,6 +117,30 @@ test_bootstrap_cmd() { ...@@ -117,6 +117,30 @@ test_bootstrap_cmd() {
' '
test_bootstrap_list_cmd test_bootstrap_list_cmd
test_expect_success "'ipfs bootstrap add' accepts args from stdin" '
echo $BP1 > bpeers &&
echo $BP2 >> bpeers &&
echo $BP3 >> bpeers &&
echo $BP4 >> bpeers &&
cat bpeers | ipfs bootstrap add > add_stdin_actual
'
test_expect_success "output looks good" '
test_cmp add_stdin_actual bpeers
'
test_bootstrap_list_cmd $BP1 $BP2 $BP3 $BP4
test_expect_success "'ipfs bootstrap rm' accepts args from stdin" '
cat bpeers | ipfs bootstrap rm > rm_stdin_actual
'
test_expect_success "output looks good" '
test_cmp rm_stdin_actual bpeers
'
test_bootstrap_list_cmd
} }
# should work offline # should work offline
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论