@cryptotaxi247 / kubo / commits / 068c6d221

commands/cli: fix reading from stdin message

Only print "Reading from /dev/stdin" message when we actually read from stdin (and not in other cases such as ipfs add --help). License: MIT Signed-off-by: Jeromy <why@ipfs.io>

Jeromy committed Aug 3, 2016 at 06:43 UTC 068c6d2211078f84d06e03544f72ee33c58c71a6
2 files changed +57 -16
commands/cli/parse.go
+48 -16
@@ -2,6 +2,7 @@ package cli
2
3 import (
4 "fmt"
5 + "io"
6 "os"
7 "path"
8 "path/filepath"
@@ -305,8 +306,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
306 if len(inputs) > 0 {
307 stringArgs, inputs = append(stringArgs, inputs[0]), inputs[1:]
308 } else if stdin != nil && argDef.SupportsStdin && !fillingVariadic {
308 - if err := printReadInfo(stdin, msgStdinInfo); err == nil {
309 - fileArgs[stdin.Name()] = files.NewReaderFile("stdin", "", stdin, nil)
309 + if r, err := maybeWrapStdin(stdin, msgStdinInfo); err == nil {
310 + fileArgs[stdin.Name()] = files.NewReaderFile("stdin", "", r, nil)
311 stdin = nil
312 }
313 }
@@ -316,27 +317,33 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
317 fpath := inputs[0]
318 inputs = inputs[1:]
319 var file files.File
319 - var err error
320 if fpath == "-" {
321 - if err = printReadInfo(stdin, msgStdinInfo); err == nil {
322 - fpath = stdin.Name()
323 - file = files.NewReaderFile("", fpath, stdin, nil)
321 + r, err := maybeWrapStdin(stdin, msgStdinInfo)
322 + if err != nil {
323 + return nil, nil, err
324 }
325 +
326 + fpath = stdin.Name()
327 + file = files.NewReaderFile("", fpath, r, nil)
328 } else {
326 - file, err = appendFile(fpath, argDef, recursive, hidden)
327 - }
328 - if err != nil {
329 - return nil, nil, err
329 + nf, err := appendFile(fpath, argDef, recursive, hidden)
330 + if err != nil {
331 + return nil, nil, err
332 + }
333 +
334 + file = nf
335 }
336
337 fileArgs[fpath] = file
338 } else if stdin != nil && argDef.SupportsStdin &&
339 argDef.Required && !fillingVariadic {
335 - if err := printReadInfo(stdin, msgStdinInfo); err != nil {
340 + r, err := maybeWrapStdin(stdin, msgStdinInfo)
341 + if err != nil {
342 return nil, nil, err
343 }
344 +
345 fpath := stdin.Name()
339 - fileArgs[fpath] = files.NewReaderFile("", fpath, stdin, nil)
346 + fileArgs[fpath] = files.NewReaderFile("", fpath, r, nil)
347 }
348 }
349
@@ -423,17 +430,17 @@ func appendFile(fpath string, argDef *cmds.Argument, recursive, hidden bool) (fi
430 }
431
432 // Inform the user if a file is waiting on input
426 -func printReadInfo(f *os.File, msg string) error {
433 +func maybeWrapStdin(f *os.File, msg string) (io.ReadCloser, error) {
434 isTty, err := isTty(f)
435 if err != nil {
429 - return err
436 + return nil, err
437 }
438
439 if isTty {
433 - fmt.Fprintf(os.Stderr, msg, f.Name())
440 + return newMessageReader(f, fmt.Sprintf(msg, f.Name())), nil
441 }
442
436 - return nil
443 + return f, nil
444 }
445
446 func isTty(f *os.File) (bool, error) {
@@ -445,3 +452,28 @@ func isTty(f *os.File) (bool, error) {
452
453 return (fInfo.Mode() & os.ModeCharDevice) != 0, nil
454 }
455 +
456 +type messageReader struct {
457 + r io.ReadCloser
458 + done bool
459 + message string
460 +}
461 +
462 +func newMessageReader(r io.ReadCloser, msg string) io.ReadCloser {
463 + return &messageReader{
464 + r: r,
465 + message: msg,
466 + }
467 +}
468 +
469 +func (r *messageReader) Read(b []byte) (int, error) {
470 + if !r.done {
471 + fmt.Fprintln(os.Stderr, r.message)
472 + }
473 +
474 + return r.r.Read(b)
475 +}
476 +
477 +func (r *messageReader) Close() error {
478 + return r.r.Close()
479 +}
test/sharness/t0040-add-and-cat.sh
+9
@@ -9,6 +9,15 @@ test_description="Test add and cat commands"
9 . lib/test-lib.sh
10
11 test_add_cat_file() {
12 + test_expect_success "ipfs add --help works" '
13 + ipfs add --help 2> add_help_err > /dev/null
14 + '
15 +
16 + test_expect_success "stdin reading message doesnt show up" '
17 + test_expect_code 1 grep "ipfs: Reading from" add_help_err &&
18 + test_expect_code 1 grep "send Ctrl-d to stop." add_help_err
19 + '
20 +
21 test_expect_success "ipfs add succeeds" '
22 echo "Hello Worlds!" >mountdir/hello.txt &&
23 ipfs add mountdir/hello.txt >actual