@cryptotaxi247 / kubo / commits / 97ab64af3

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 <chriscool@tuxfamily.org>

Christian Couder committed May 17, 2015 at 20:54 UTC 97ab64af300881a9decfd0315b6460e704e09ec5
2 files changed +18 -9
commands/cli/parse.go
+16 -7
@@ -255,13 +255,17 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
255 stringArgs, inputs = appendString(stringArgs, inputs)
256
257 } else if argDef.SupportsStdin {
258 - // if we have a stdin, read it in and use the data as a string value
259 - stringArgs, stdin, err = appendStdinAsString(stringArgs, stdin)
260 - if err != nil {
261 - return nil, nil, err
258 + if len(inputs) > 0 {
259 + // don't use stdin if we have inputs
260 + stdin = nil
261 + } else {
262 + // if we have a stdin, read it in and use the data as a string value
263 + stringArgs, stdin, err = appendStdinAsString(stringArgs, stdin)
264 + if err != nil {
265 + return nil, nil, err
266 + }
267 }
268 }
264 -
269 } else if argDef.Type == cmds.ArgFile {
270 if stdin == nil {
271 // treat stringArg values as file paths
@@ -271,8 +275,13 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
275 }
276
277 } else if argDef.SupportsStdin {
274 - // if we have a stdin, create a file from it
275 - fileArgs, stdin = appendStdinAsFile(fileArgs, stdin)
278 + if len(inputs) > 0 {
279 + // don't use stdin if we have inputs
280 + stdin = nil
281 + } else {
282 + // if we have a stdin, create a file from it
283 + fileArgs, stdin = appendStdinAsFile(fileArgs, stdin)
284 + }
285 }
286 }
287
commands/cli/parse_test.go
+2 -2
@@ -222,8 +222,8 @@ func TestArgumentParsing(t *testing.T) {
222 fstdin := fileToSimulateStdin(t, "stdin1")
223
224 test([]string{"stdinenabled"}, fstdin, []string{"stdin1"})
225 - test([]string{"stdinenabled", "value1"}, fstdin, []string{"stdin1", "value1"})
226 - test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"stdin1", "value1", "value2"})
225 + test([]string{"stdinenabled", "value1"}, fstdin, []string{"value1"})
226 + test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"value1", "value2"})
227
228 fstdin = fileToSimulateStdin(t, "stdin1\nstdin2")
229 test([]string{"stdinenabled"}, fstdin, []string{"stdin1", "stdin2"})