-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
60 lines (48 loc) · 1.15 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/alexflint/go-arg"
)
var args struct {
Path string `arg:"-p,--path" default:"." help:"the directory to search"`
}
func byteCountFromDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}
func findMatch(path string, f os.FileInfo, err error) error {
for ext, description := range fileExtentions {
if strings.HasSuffix(path, ext) {
fmt.Printf("[%s%sM%s] %s%spath%s: %s, %s%stype%s: %s, %s%ssize%s: %s\n",
yellow, bold, end, red, bold, end, path,
green, bold, end, description,
blue, bold, end, byteCountFromDecimal(f.Size()))
}
}
return nil
}
func walk(path string) {
if err := filepath.Walk(path, findMatch); err != nil {
fmt.Printf("%serror%s: %s\n", red, end, err.Error())
}
}
func main() {
arg.MustParse(&args)
now := time.Now()
path := filepath.Clean(args.Path)
walk(path)
end := time.Since(now)
fmt.Printf("\nTime elapsed: %s\n", end)
}