Skip to content

Commit ce07df8

Browse files
author
Colm Dougan
committed
added a count command
1 parent 1dee011 commit ce07df8

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

cmd/hdfs/count.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
const SNAPSHOT_FORMAT = "%18d %24d %24d %28s \n";
9+
10+
func count(args []string) {
11+
if len(args) == 0 {
12+
fatalWithUsage()
13+
}
14+
15+
expanded, client, err := getClientAndExpandedPaths(args)
16+
if err != nil {
17+
fatal(err)
18+
}
19+
20+
for _, p := range expanded {
21+
_, err := client.Stat(p)
22+
if err != nil {
23+
fmt.Fprintln(os.Stderr, err)
24+
status = 1
25+
continue
26+
}
27+
28+
cs, err := client.GetContentSummary(p)
29+
if err != nil {
30+
fmt.Fprintln(os.Stderr, err)
31+
status = 1
32+
continue
33+
}
34+
35+
fmt.Printf(SNAPSHOT_FORMAT, cs.DirectoryCount(), cs.FileCount(), cs.Size(), p)
36+
}
37+
}

cmd/hdfs/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The flags available are a subset of the POSIX ones, but should behave similarly.
2323
2424
Valid commands:
2525
ls [-lah] [FILE]...
26+
count [FILE]...
2627
rm [-rf] FILE...
2728
mv [-nT] SOURCE... DEST
2829
mkdir [-p] FILE...
@@ -138,6 +139,8 @@ func main() {
138139
du(duOpts.Args(), *dus, *duh)
139140
case "checksum":
140141
checksum(argv[1:])
142+
case "count":
143+
count(argv[1:])
141144
case "get":
142145
get(argv[1:])
143146
case "getmerge":

cmd/hdfs/test/count.bats

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bats
2+
3+
load helper
4+
5+
setup() {
6+
$HDFS mkdir -p /_test_cmd/count/dir1
7+
$HDFS mkdir -p /_test_cmd/count/dir2
8+
$HDFS mkdir -p /_test_cmd/count/dir3
9+
$HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/count/foo.txt
10+
$HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/count/dir1/foo1.txt
11+
}
12+
13+
@test "count" {
14+
run $HDFS count /_test_cmd/count/foo.txt
15+
assert_success
16+
assert_output <<OUT
17+
0 1 4 /_test_cmd/count/foo.txt
18+
OUT
19+
}
20+
21+
@test "count dir" {
22+
run $HDFS count /_test_cmd/count
23+
assert_success
24+
assert_output <<OUT
25+
4 2 8 /_test_cmd/count
26+
OUT
27+
}
28+
29+
@test "count wildcard" {
30+
run $HDFS count /_test_cmd/count/dir*
31+
assert_success
32+
assert_output <<OUT
33+
1 1 4 /_test_cmd/count/dir1
34+
1 0 0 /_test_cmd/count/dir2
35+
1 0 0 /_test_cmd/count/dir3
36+
OUT
37+
}
38+
39+
@test "count nonexistent" {
40+
run $HDFS count /_test_cmd/nonexistent
41+
assert_failure
42+
assert_output <<OUT
43+
stat /_test_cmd/nonexistent: file does not exist
44+
OUT
45+
}
46+
47+
teardown() {
48+
$HDFS rm -r /_test_cmd/count
49+
}

0 commit comments

Comments
 (0)