Skip to content

Commit 34be6ec

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

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

cmd/hdfs/count.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
const SNAPSHOT_FORMAT = "%18d %24d %24s %28s \n"
9+
10+
func count(args []string, humanReadable bool) {
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+
contentSize := formatBytesHuman(uint64(cs.Size()), humanReadable)
36+
fmt.Printf(SNAPSHOT_FORMAT, cs.DirectoryCount(), cs.FileCount(), contentSize, p)
37+
}
38+
}

cmd/hdfs/main.go

Lines changed: 8 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 [-h] [FILE]...
2627
rm [-rf] FILE...
2728
mv [-nT] SOURCE... DEST
2829
mkdir [-p] FILE...
@@ -66,6 +67,9 @@ Valid commands:
6667
chownOpts = getopt.New()
6768
chownR = chownOpts.Bool('R')
6869

70+
countOpts = getopt.New()
71+
counth = countOpts.Bool('h')
72+
6973
headTailOpts = getopt.New()
7074
headtailn = headTailOpts.Int64('n', -1)
7175
headtailc = headTailOpts.Int64('c', -1)
@@ -91,6 +95,7 @@ func init() {
9195
touchOpts.SetUsage(printHelp)
9296
chmodOpts.SetUsage(printHelp)
9397
chownOpts.SetUsage(printHelp)
98+
countOpts.SetUsage(printHelp)
9499
headTailOpts.SetUsage(printHelp)
95100
duOpts.SetUsage(printHelp)
96101
getmergeOpts.SetUsage(printHelp)
@@ -138,6 +143,9 @@ func main() {
138143
du(duOpts.Args(), *dus, *duh)
139144
case "checksum":
140145
checksum(argv[1:])
146+
case "count":
147+
countOpts.Parse(argv)
148+
count(countOpts.Args(), *counth)
141149
case "get":
142150
get(argv[1:])
143151
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+
}

cmd/hdfs/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"strconv"
56
)
67

78
func formatBytes(i uint64) string {
@@ -18,3 +19,10 @@ func formatBytes(i uint64) string {
1819
return fmt.Sprintf("%dB", i)
1920
}
2021
}
22+
23+
func formatBytesHuman(i uint64, humanReadable bool) string {
24+
if humanReadable {
25+
return formatBytes(i)
26+
}
27+
return strconv.FormatUint(i, 10)
28+
}

0 commit comments

Comments
 (0)