From 97ab64af300881a9decfd0315b6460e704e09ec5 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Sun, 17 May 2015 20:54:07 +0200 Subject: [PATCH] parse: don't use stdin if there are arguments This should fix issue #1141 (ipfs cat "multihash too short" error when using stdin) and perhaps others. License: MIT Signed-off-by: Christian Couder --- commands/cli/parse.go | 23 ++++++++++++++++------- commands/cli/parse_test.go | 4 ++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/commands/cli/parse.go b/commands/cli/parse.go index 86835cbf4..06c6d477c 100644 --- a/commands/cli/parse.go +++ b/commands/cli/parse.go @@ -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 @@ -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) + } } } diff --git a/commands/cli/parse_test.go b/commands/cli/parse_test.go index f7a61bceb..5caf5bdae 100644 --- a/commands/cli/parse_test.go +++ b/commands/cli/parse_test.go @@ -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"})