diff --git a/cmd/hdfs/count.go b/cmd/hdfs/count.go new file mode 100644 index 00000000..456c115a --- /dev/null +++ b/cmd/hdfs/count.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "os" +) + +const SNAPSHOT_FORMAT = "%18d %24d %24s %28s \n" + +func count(args []string, humanReadable bool) { + if len(args) == 0 { + fatalWithUsage() + } + + expanded, client, err := getClientAndExpandedPaths(args) + if err != nil { + fatal(err) + } + + for _, p := range expanded { + _, err := client.Stat(p) + if err != nil { + fmt.Fprintln(os.Stderr, err) + status = 1 + continue + } + + cs, err := client.GetContentSummary(p) + if err != nil { + fmt.Fprintln(os.Stderr, err) + status = 1 + continue + } + + contentSize := formatBytesHuman(uint64(cs.Size()), humanReadable) + fmt.Printf(SNAPSHOT_FORMAT, cs.DirectoryCount(), cs.FileCount(), contentSize, p) + } +} diff --git a/cmd/hdfs/main.go b/cmd/hdfs/main.go index da783d3c..70596469 100755 --- a/cmd/hdfs/main.go +++ b/cmd/hdfs/main.go @@ -23,6 +23,7 @@ The flags available are a subset of the POSIX ones, but should behave similarly. Valid commands: ls [-lah] [FILE]... + count [-h] [FILE]... rm [-rf] FILE... mv [-nT] SOURCE... DEST mkdir [-p] FILE... @@ -66,6 +67,9 @@ Valid commands: chownOpts = getopt.New() chownR = chownOpts.Bool('R') + countOpts = getopt.New() + counth = countOpts.Bool('h') + headTailOpts = getopt.New() headtailn = headTailOpts.Int64('n', -1) headtailc = headTailOpts.Int64('c', -1) @@ -91,6 +95,7 @@ func init() { touchOpts.SetUsage(printHelp) chmodOpts.SetUsage(printHelp) chownOpts.SetUsage(printHelp) + countOpts.SetUsage(printHelp) headTailOpts.SetUsage(printHelp) duOpts.SetUsage(printHelp) getmergeOpts.SetUsage(printHelp) @@ -138,6 +143,9 @@ func main() { du(duOpts.Args(), *dus, *duh) case "checksum": checksum(argv[1:]) + case "count": + countOpts.Parse(argv) + count(countOpts.Args(), *counth) case "get": get(argv[1:]) case "getmerge": diff --git a/cmd/hdfs/test/count.bats b/cmd/hdfs/test/count.bats new file mode 100644 index 00000000..515c04c6 --- /dev/null +++ b/cmd/hdfs/test/count.bats @@ -0,0 +1,57 @@ +#!/usr/bin/env bats + +load helper + +setup() { + $HDFS mkdir -p /_test_cmd/count/dir1 + $HDFS mkdir -p /_test_cmd/count/dir2 + $HDFS mkdir -p /_test_cmd/count/dir3 + $HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/count/foo.txt + $HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/count/dir1/foo1.txt +} + +@test "count" { + run $HDFS count /_test_cmd/count/foo.txt + assert_success + assert_output <