Skip to content

Commit aa621cc

Browse files
added human output
1 parent 3221a43 commit aa621cc

File tree

8 files changed

+61
-5
lines changed

8 files changed

+61
-5
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ build:
44
run:
55
./bin/hexlet-path-size $(path)
66

7+
run-human:
8+
./bin/hexlet-path-size --human $(path)
9+
710
lint:
811
golangci-lint run
912

bin/hexlet-path-size

17.8 KB
Binary file not shown.

cmd/hexlet-path-size/main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ func main() {
1414
cmd := &cli.Command{
1515
Name: "hexlet-path-size",
1616
Usage: "print size of a file or directory",
17+
Flags: []cli.Flag{
18+
&cli.BoolFlag{
19+
Name: "human",
20+
Value: false,
21+
Usage: "human-readable sizes (auto-select unit)",
22+
},
23+
},
1724
Action: func(_ context.Context, cmd *cli.Command) error {
18-
path := cmd.Args().Get(0)
19-
size, err := pathsize.GetPathSize(path, false, true, false)
25+
path := "."
26+
if cmd.NArg() > 0 {
27+
path = cmd.Args().Get(0)
28+
}
29+
human := cmd.Bool("human")
30+
size, err := pathsize.GetPathSize(path, false, human, false)
2031
if err != nil {
2132
fmt.Println(err.Error())
2233
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.25.4
44

55
require (
66
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf // indirect
78
github.com/pmezard/go-difflib v1.0.0 // indirect
89
github.com/stretchr/testify v1.11.1 // indirect
910
github.com/urfave/cli/v3 v3.6.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf h1:FtEj8sfIcaaBfAKrE1Cwb61YDtYq9JxChK1c7AKce7s=
4+
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf/go.mod h1:yrqSXGoD/4EKfF26AOGzscPOgTTJcyAwM2rpixWT+t4=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=

pathsize/formatter.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ package pathsize
33
import (
44
"fmt"
55
"strconv"
6+
7+
"github.com/inhies/go-bytesize"
68
)
79

8-
func Format(size int, path string, _ bool) string {
10+
func Format(size int, path string, human bool) string {
11+
if human {
12+
humanSize := bytesize.New(float64(size))
13+
return fmt.Sprintf("%s\t%s", humanSize, path)
14+
}
15+
916
return fmt.Sprintf("%s\t%s", strconv.Itoa(size), path)
1017
}

testdata/image.jpg

42.1 KB
Loading

tests/pathsize_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212

1313
func TestGetPathSizeFile(t *testing.T) {
1414
path := filepath.Join("../testdata/testfile.txt")
15-
result, err := pathsize.GetPathSize(path, false, true, false)
15+
human := false
16+
result, err := pathsize.GetPathSize(path, false, human, false)
1617
if err != nil {
1718
t.Errorf("Error: %s", err.Error())
1819
}
@@ -24,9 +25,40 @@ func TestGetPathSizeFile(t *testing.T) {
2425
}
2526
}
2627

28+
func TestGetPathSizeFileHumanInBytes(t *testing.T) {
29+
path := filepath.Join("../testdata/testfile.txt")
30+
human := true
31+
result, err := pathsize.GetPathSize(path, false, human, false)
32+
if err != nil {
33+
t.Errorf("Error: %s", err.Error())
34+
}
35+
36+
expectedResult := fmt.Sprintf("%s\t%s", "10.00B", path)
37+
require.Equal(t, result, expectedResult)
38+
if result != expectedResult {
39+
t.Errorf("Actual result %s does not match the expected result %s", result, expectedResult)
40+
}
41+
}
42+
43+
func TestGetPathSizeFileHumanInKiloBytes(t *testing.T) {
44+
path := filepath.Join("../testdata/image.jpg")
45+
human := true
46+
result, err := pathsize.GetPathSize(path, false, human, false)
47+
if err != nil {
48+
t.Errorf("Error: %s", err.Error())
49+
}
50+
51+
expectedResult := fmt.Sprintf("%s\t%s", "42.14KB", path)
52+
require.Equal(t, result, expectedResult)
53+
if result != expectedResult {
54+
t.Errorf("Actual result %s does not match the expected result %s", result, expectedResult)
55+
}
56+
}
57+
2758
func TestGetPathSizeDir(t *testing.T) {
2859
path := filepath.Join("../testdata/testdir")
29-
result, err := pathsize.GetPathSize(path, false, true, false)
60+
human := false
61+
result, err := pathsize.GetPathSize(path, false, human, false)
3062
if err != nil {
3163
t.Errorf("Error: %s", err.Error())
3264
}

0 commit comments

Comments
 (0)