/
sizeof.go
47 lines (37 loc) 路 921 Bytes
/
sizeof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package sizeof
import (
"os"
"path/filepath"
humanize "github.com/dustin/go-humanize"
"github.com/itchio/butler/comm"
"github.com/itchio/butler/mansion"
)
var args = struct {
path *string
}{}
func Register(ctx *mansion.Context) {
cmd := ctx.App.Command("sizeof", "Compute the total size of a directory").Hidden()
args.path = cmd.Arg("path", "Directory to compute the size of").Required().String()
ctx.Register(cmd, do)
}
func do(ctx *mansion.Context) {
path := *args.path
totalSize, err := Do(path)
ctx.Must(err)
comm.ResultOrPrint(totalSize, func() {
comm.Logf("Total size of %s: %s", path, humanize.IBytes(uint64(totalSize)))
})
}
func Do(path string) (int64, error) {
var totalSize int64
inc := func(_ string, f os.FileInfo, err error) error {
if err != nil {
// just skip'em
return nil
}
totalSize += f.Size()
return nil
}
filepath.Walk(path, inc)
return totalSize, nil
}