Родитель
715f1cbbb6
Коммит
5434f3676c
|
@ -6,6 +6,8 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/colinmarc/hdfs"
|
||||
)
|
||||
|
||||
func put(args []string) {
|
||||
|
@ -29,6 +31,40 @@ func put(args []string) {
|
|||
fatal(err)
|
||||
}
|
||||
|
||||
if filepath.Base(source) == "-" {
|
||||
putFromStdin(client, dest)
|
||||
} else {
|
||||
putFromFile(client, source, dest)
|
||||
}
|
||||
}
|
||||
|
||||
func putFromStdin(client *hdfs.Client, dest string) {
|
||||
// If the destination exists, regardless of what it is, bail out.
|
||||
_, err := client.Stat(dest)
|
||||
if err == nil {
|
||||
fatal(&os.PathError{"put", dest, os.ErrExist})
|
||||
} else if !os.IsNotExist(err) {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
mode := 0755 | os.ModeDir
|
||||
parentDir := filepath.Dir(dest)
|
||||
if parentDir != "." && parentDir != "/" {
|
||||
if err := client.MkdirAll(parentDir, mode); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
writer, err := client.Create(dest)
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
defer writer.Close()
|
||||
|
||||
io.Copy(writer, os.Stdin)
|
||||
}
|
||||
|
||||
func putFromFile(client *hdfs.Client, source string, dest string) {
|
||||
// If the destination is an existing directory, place it inside. Otherwise,
|
||||
// the destination is really the parent directory, and we need to rename the
|
||||
// source directory as we copy.
|
||||
|
|
|
@ -64,6 +64,33 @@ mkdir /_test_cmd/put/existing.txt: file already exists
|
|||
OUT
|
||||
}
|
||||
|
||||
@test "put stdin" {
|
||||
run bash -c "cat $ROOT_TEST_DIR/test/mobydick.txt | $HDFS put - /_test_cmd/put_stdin/mobydick_stdin.txt"
|
||||
assert_success
|
||||
|
||||
run bash -c "$HDFS cat /_test_cmd/put_stdin/mobydick_stdin.txt > $BATS_TMPDIR/mobydick_stdin_test.txt"
|
||||
assert_success
|
||||
|
||||
SHA=`shasum < $ROOT_TEST_DIR/test/mobydick.txt | awk '{ print $1 }'`
|
||||
assert_equal $SHA `shasum < $BATS_TMPDIR/mobydick_stdin_test.txt | awk '{ print $1 }'`
|
||||
}
|
||||
|
||||
@test "put stdin into file" {
|
||||
run bash -c "cat $ROOT_TEST_DIR/test/mobydick.txt | $HDFS put - /_test_cmd/put/existing.txt"
|
||||
assert_failure
|
||||
assert_output <<OUT
|
||||
put /_test_cmd/put/existing.txt: file already exists
|
||||
OUT
|
||||
}
|
||||
|
||||
@test "put stdin into dir" {
|
||||
run bash -c "cat $ROOT_TEST_DIR/test/mobydick.txt | $HDFS put - /_test_cmd/put/"
|
||||
assert_failure
|
||||
assert_output <<OUT
|
||||
put /_test_cmd/put: file already exists
|
||||
OUT
|
||||
}
|
||||
|
||||
teardown() {
|
||||
$HDFS rm -r /_test_cmd/put
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче