1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 19:44:01 +08:00

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>
This commit is contained in:
Jeromy
2016-08-03 06:43:35 -07:00
parent 16c5a89dd4
commit 068c6d2211
2 changed files with 57 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package cli
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -305,8 +306,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
if len(inputs) > 0 { if len(inputs) > 0 {
stringArgs, inputs = append(stringArgs, inputs[0]), inputs[1:] stringArgs, inputs = append(stringArgs, inputs[0]), inputs[1:]
} else if stdin != nil && argDef.SupportsStdin && !fillingVariadic { } else if stdin != nil && argDef.SupportsStdin && !fillingVariadic {
if err := printReadInfo(stdin, msgStdinInfo); err == nil { if r, err := maybeWrapStdin(stdin, msgStdinInfo); err == nil {
fileArgs[stdin.Name()] = files.NewReaderFile("stdin", "", stdin, nil) fileArgs[stdin.Name()] = files.NewReaderFile("stdin", "", r, nil)
stdin = nil stdin = nil
} }
} }
@ -316,27 +317,33 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
fpath := inputs[0] fpath := inputs[0]
inputs = inputs[1:] inputs = inputs[1:]
var file files.File var file files.File
var err error
if fpath == "-" { if fpath == "-" {
if err = printReadInfo(stdin, msgStdinInfo); err == nil { r, err := maybeWrapStdin(stdin, msgStdinInfo)
fpath = stdin.Name() if err != nil {
file = files.NewReaderFile("", fpath, stdin, nil) return nil, nil, err
} }
fpath = stdin.Name()
file = files.NewReaderFile("", fpath, r, nil)
} else { } else {
file, err = appendFile(fpath, argDef, recursive, hidden) nf, err := appendFile(fpath, argDef, recursive, hidden)
} if err != nil {
if err != nil { return nil, nil, err
return nil, nil, err }
file = nf
} }
fileArgs[fpath] = file fileArgs[fpath] = file
} else if stdin != nil && argDef.SupportsStdin && } else if stdin != nil && argDef.SupportsStdin &&
argDef.Required && !fillingVariadic { argDef.Required && !fillingVariadic {
if err := printReadInfo(stdin, msgStdinInfo); err != nil { r, err := maybeWrapStdin(stdin, msgStdinInfo)
if err != nil {
return nil, nil, err return nil, nil, err
} }
fpath := stdin.Name() fpath := stdin.Name()
fileArgs[fpath] = files.NewReaderFile("", fpath, stdin, nil) fileArgs[fpath] = files.NewReaderFile("", fpath, r, nil)
} }
} }
@ -423,17 +430,17 @@ func appendFile(fpath string, argDef *cmds.Argument, recursive, hidden bool) (fi
} }
// Inform the user if a file is waiting on input // Inform the user if a file is waiting on input
func printReadInfo(f *os.File, msg string) error { func maybeWrapStdin(f *os.File, msg string) (io.ReadCloser, error) {
isTty, err := isTty(f) isTty, err := isTty(f)
if err != nil { if err != nil {
return err return nil, err
} }
if isTty { if isTty {
fmt.Fprintf(os.Stderr, msg, f.Name()) return newMessageReader(f, fmt.Sprintf(msg, f.Name())), nil
} }
return nil return f, nil
} }
func isTty(f *os.File) (bool, error) { func isTty(f *os.File) (bool, error) {
@ -445,3 +452,28 @@ func isTty(f *os.File) (bool, error) {
return (fInfo.Mode() & os.ModeCharDevice) != 0, nil return (fInfo.Mode() & os.ModeCharDevice) != 0, nil
} }
type messageReader struct {
r io.ReadCloser
done bool
message string
}
func newMessageReader(r io.ReadCloser, msg string) io.ReadCloser {
return &messageReader{
r: r,
message: msg,
}
}
func (r *messageReader) Read(b []byte) (int, error) {
if !r.done {
fmt.Fprintln(os.Stderr, r.message)
}
return r.r.Read(b)
}
func (r *messageReader) Close() error {
return r.r.Close()
}

View File

@ -9,6 +9,15 @@ test_description="Test add and cat commands"
. lib/test-lib.sh . lib/test-lib.sh
test_add_cat_file() { test_add_cat_file() {
test_expect_success "ipfs add --help works" '
ipfs add --help 2> add_help_err > /dev/null
'
test_expect_success "stdin reading message doesnt show up" '
test_expect_code 1 grep "ipfs: Reading from" add_help_err &&
test_expect_code 1 grep "send Ctrl-d to stop." add_help_err
'
test_expect_success "ipfs add succeeds" ' test_expect_success "ipfs add succeeds" '
echo "Hello Worlds!" >mountdir/hello.txt && echo "Hello Worlds!" >mountdir/hello.txt &&
ipfs add mountdir/hello.txt >actual ipfs add mountdir/hello.txt >actual