Commit f039aa44 authored by Jeromy's avatar Jeromy

cleanup

License: MIT
Signed-off-by: default avatarJeromy <why@ipfs.io>
parent 533643e9
......@@ -17,6 +17,11 @@ import (
var log = logging.Logger("commands/cli")
// stdinSpecialName is a name applied to the 'stdin' file so we can differentiate
// it from potential 'real' files being passed in. The '*' character is invalid in
// path names and won't appear otherwise.
const stdinSpecialName = "*stdin*"
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *cmds.Command, []string, error) {
......@@ -302,7 +307,7 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
return nil, nil, err
}
if istty {
fname = "*stdin*"
fname = stdinSpecialName
}
fileArgs[stdin.Name()] = files.NewReaderFile(fname, "", stdin, nil)
......
......@@ -200,8 +200,8 @@ func (r *request) Context() context.Context {
}
func (r *request) haveVarArgsFromStdin() bool {
// we expect varargs if we have a variadic required argument and no arguments to
// fill it
// we expect varargs if we have a string argument that supports stdin
// and not arguments to satisfy it
if len(r.cmd.Arguments) == 0 {
return false
}
......@@ -214,16 +214,8 @@ func (r *request) haveVarArgsFromStdin() bool {
// VarArgs can be used when you want string arguments as input
// and also want to be able to handle them in a streaming fashion
func (r *request) VarArgs(f func(string) error) error {
var i int
for i = 0; i < len(r.cmd.Arguments); i++ {
if r.cmd.Arguments[i].Variadic || r.cmd.Arguments[i].SupportsStdin {
break
}
}
args := r.arguments[i:]
if len(args) > 0 {
for _, arg := range args {
if len(r.arguments) >= len(r.cmd.Arguments) {
for _, arg := range r.arguments[len(r.cmd.Arguments)-1:] {
err := f(arg)
if err != nil {
return err
......@@ -231,29 +223,29 @@ func (r *request) VarArgs(f func(string) error) error {
}
return nil
} else {
if r.files != nil {
fi, err := r.files.NextFile()
if err != nil {
return err
}
}
if fi.FileName() == "*stdin*" {
fmt.Fprintln(os.Stderr, "ipfs: Reading from stdin; send Ctrl-d to stop.")
}
if r.files == nil {
return fmt.Errorf("expected more arguments from stdin")
}
scan := bufio.NewScanner(fi)
for scan.Scan() {
err := f(scan.Text())
if err != nil {
return err
}
}
return nil
} else {
return fmt.Errorf("expected more arguments from stdin")
fi, err := r.files.NextFile()
if err != nil {
return err
}
if fi.FileName() == "*stdin*" {
fmt.Fprintln(os.Stderr, "ipfs: Reading from stdin; send Ctrl-d to stop.")
}
scan := bufio.NewScanner(fi)
for scan.Scan() {
err := f(scan.Text())
if err != nil {
return err
}
}
return nil
}
func getContext(base context.Context, req Request) (context.Context, error) {
......
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