Skip to content

Commit

Permalink
parse: don't use stdin if there are arguments
Browse files Browse the repository at this point in the history
This should fix issue #1141 (ipfs cat "multihash too short"
error when using stdin) and perhaps others.

License: MIT
Signed-off-by: Christian Couder <[email protected]>
  • Loading branch information
chriscool committed May 17, 2015
1 parent ff9cb9e commit 97ab64a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
23 changes: 16 additions & 7 deletions commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,17 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
stringArgs, inputs = appendString(stringArgs, inputs)

} else if argDef.SupportsStdin {
// if we have a stdin, read it in and use the data as a string value
stringArgs, stdin, err = appendStdinAsString(stringArgs, stdin)
if err != nil {
return nil, nil, err
if len(inputs) > 0 {
// don't use stdin if we have inputs
stdin = nil
} else {
// if we have a stdin, read it in and use the data as a string value
stringArgs, stdin, err = appendStdinAsString(stringArgs, stdin)
if err != nil {
return nil, nil, err
}
}
}

} else if argDef.Type == cmds.ArgFile {
if stdin == nil {
// treat stringArg values as file paths
Expand All @@ -271,8 +275,13 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
}

} else if argDef.SupportsStdin {
// if we have a stdin, create a file from it
fileArgs, stdin = appendStdinAsFile(fileArgs, stdin)
if len(inputs) > 0 {
// don't use stdin if we have inputs
stdin = nil
} else {
// if we have a stdin, create a file from it
fileArgs, stdin = appendStdinAsFile(fileArgs, stdin)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions commands/cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ func TestArgumentParsing(t *testing.T) {
fstdin := fileToSimulateStdin(t, "stdin1")

test([]string{"stdinenabled"}, fstdin, []string{"stdin1"})
test([]string{"stdinenabled", "value1"}, fstdin, []string{"stdin1", "value1"})
test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"stdin1", "value1", "value2"})
test([]string{"stdinenabled", "value1"}, fstdin, []string{"value1"})
test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"value1", "value2"})

fstdin = fileToSimulateStdin(t, "stdin1\nstdin2")
test([]string{"stdinenabled"}, fstdin, []string{"stdin1", "stdin2"})
Expand Down

0 comments on commit 97ab64a

Please sign in to comment.