Skip to content

Commit

Permalink
added a count command
Browse files Browse the repository at this point in the history
  • Loading branch information
Colm Dougan committed Apr 9, 2022
1 parent 1dee011 commit ce07df8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cmd/hdfs/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
"os"
)

const SNAPSHOT_FORMAT = "%18d %24d %24d %28s \n";

func count(args []string) {
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
}

fmt.Printf(SNAPSHOT_FORMAT, cs.DirectoryCount(), cs.FileCount(), cs.Size(), p)
}
}
3 changes: 3 additions & 0 deletions cmd/hdfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The flags available are a subset of the POSIX ones, but should behave similarly.
Valid commands:
ls [-lah] [FILE]...
count [FILE]...
rm [-rf] FILE...
mv [-nT] SOURCE... DEST
mkdir [-p] FILE...
Expand Down Expand Up @@ -138,6 +139,8 @@ func main() {
du(duOpts.Args(), *dus, *duh)
case "checksum":
checksum(argv[1:])
case "count":
count(argv[1:])
case "get":
get(argv[1:])
case "getmerge":
Expand Down
49 changes: 49 additions & 0 deletions cmd/hdfs/test/count.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/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 <<OUT
0 1 4 /_test_cmd/count/foo.txt
OUT
}

@test "count dir" {
run $HDFS count /_test_cmd/count
assert_success
assert_output <<OUT
4 2 8 /_test_cmd/count
OUT
}

@test "count wildcard" {
run $HDFS count /_test_cmd/count/dir*
assert_success
assert_output <<OUT
1 1 4 /_test_cmd/count/dir1
1 0 0 /_test_cmd/count/dir2
1 0 0 /_test_cmd/count/dir3
OUT
}

@test "count nonexistent" {
run $HDFS count /_test_cmd/nonexistent
assert_failure
assert_output <<OUT
stat /_test_cmd/nonexistent: file does not exist
OUT
}

teardown() {
$HDFS rm -r /_test_cmd/count
}

0 comments on commit ce07df8

Please sign in to comment.