Change stdin handling to be only used if needed
or if user asks for it License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Jun 7, 2016 at 20:30 UTC
da4e54c3db9018aa958b794657d49eb9bbcccddc
1 file changed
+25
-34
commands/cli/parse.go
+25
-34
@@ -244,15 +244,6 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
244
stdin = nil
245
}
246
247
- // check if stdin is coming from terminal or is being piped in
248
- if stdin != nil {
249
- if term, err := isTerminal(stdin); err != nil {
250
- return nil, nil, err
251
- } else if term {
252
- stdin = nil // set to nil so we ignore it
253
- }
254
- }
255
-
247
// count required argument definitions
248
numRequired := 0
249
for _, argDef := range argDefs {
@@ -293,9 +284,18 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
284
numRequired--
285
}
286
287
+ fillingVariadic := argDefIndex+1 > len(argDefs)
288
+
289
var err error
290
if argDef.Type == cmds.ArgString {
298
- if stdin == nil {
291
+ if len(inputs) > 0 {
292
+ // If argument is "-" use stdin
293
+ if inputs[0] == "-" && argDef.SupportsStdin {
294
+ stringArgs, stdin, err = appendStdinAsString(stringArgs, stdin)
295
+ if err != nil {
296
+ return nil, nil, err
297
+ }
298
+ }
299
// add string values
300
stringArgs, inputs = appendString(stringArgs, inputs)
301
} else if !argDef.SupportsStdin {
@@ -307,35 +307,39 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
307
308
stringArgs, inputs = appendString(stringArgs, inputs)
309
} else {
310
- if len(inputs) > 0 {
311
- // don't use stdin if we have inputs
312
- stdin = nil
313
- } else {
310
+ if stdin != nil && argDef.Required && !fillingVariadic {
311
// if we have a stdin, read it in and use the data as a string value
312
stringArgs, stdin, err = appendStdinAsString(stringArgs, stdin)
313
if err != nil {
314
return nil, nil, err
315
}
316
+ } else {
317
+ break
318
}
319
}
320
} else if argDef.Type == cmds.ArgFile {
322
- if stdin == nil || !argDef.SupportsStdin {
321
+ if len(inputs) > 0 {
322
// treat stringArg values as file paths
323
fpath := inputs[0]
324
inputs = inputs[1:]
326
- file, err := appendFile(fpath, argDef, recursive, hidden)
325
+ var file files.File
326
+ var err error
327
+ if fpath == "-" {
328
+ file = files.NewReaderFile("", "", stdin, nil)
329
+ } else {
330
+ file, err = appendFile(fpath, argDef, recursive, hidden)
331
+ }
332
if err != nil {
333
return nil, nil, err
334
}
335
336
fileArgs[fpath] = file
337
} else {
333
- if len(inputs) > 0 {
334
- // don't use stdin if we have inputs
335
- stdin = nil
336
- } else {
337
- // if we have a stdin, create a file from it
338
+ if stdin != nil && argDef.SupportsStdin &&
339
+ argDef.Required && !fillingVariadic {
340
fileArgs[""] = files.NewReaderFile("", "", stdin, nil)
341
+ } else {
342
+ break
343
}
344
}
345
}
@@ -431,16 +435,3 @@ func appendFile(fpath string, argDef *cmds.Argument, recursive, hidden bool) (fi
435
436
return files.NewSerialFile(path.Base(fpath), fpath, hidden, stat)
437
}
434
-
435
-// isTerminal returns true if stdin is a Stdin pipe (e.g. `cat file | ipfs`),
436
-// and false otherwise (e.g. nothing is being piped in, so stdin is
437
-// coming from the terminal)
438
-func isTerminal(stdin *os.File) (bool, error) {
439
- stat, err := stdin.Stat()
440
- if err != nil {
441
- return false, err
442
- }
443
-
444
- // if stdin is a CharDevice, return true
445
- return ((stat.Mode() & os.ModeCharDevice) != 0), nil
446
-}