-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (61 loc) · 1.18 KB
/
main.go
File metadata and controls
70 lines (61 loc) · 1.18 KB
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
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"io"
"os"
"path/filepath"
)
func main() {
out := os.Stdout
if !(len(os.Args) == 2 || len(os.Args) == 3) {
panic("usage go run main.go . [-f]")
}
path := os.Args[1]
printFiles := len(os.Args) == 3 && os.Args[2] == "-f"
err := dirTree(out, path, printFiles)
if err != nil {
panic(err.Error())
}
}
func dirTree(out io.Writer, path string, op bool) error {
op = false
out = nil
return dirTreeWithPrefix(path, "")
}
func dirTreeWithPrefix(path string, prefix string) error {
files, err := os.ReadDir(path)
if err != nil {
return err
}
for i, file := range files {
isLast := i == len(files)-1
if isLast {
fmt.Print(prefix + "└───")
} else {
fmt.Print(prefix + "├───")
}
fmt.Print(file.Name())
if !file.IsDir() {
info, err := file.Info()
if err != nil {
return err
}
if info.Size() == 0 {
fmt.Print(" (empty)")
} else {
fmt.Printf(" (%db)", info.Size())
}
}
fmt.Println()
if file.IsDir() {
newPrefix := prefix
if isLast {
newPrefix += "\t"
} else {
newPrefix += "│\t"
}
dirTreeWithPrefix(filepath.Join(path, file.Name()), newPrefix)
}
}
return nil
}