-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (75 loc) · 1.81 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
)
func getSize(file os.FileInfo) string {
var fileSize string
if file.Size() == 0 {
fileSize = "empty"
} else {
fileSize = strconv.FormatInt(file.Size(), 10) + "b"
}
return fileSize
}
func dirLevel(out io.Writer, path string, printFiles bool, level int, prefix string) error {
// содержимое текущей директории
dirContent, err := ioutil.ReadDir(path)
if err != nil {
panic(err.Error())
}
var directories []os.FileInfo
// выкинуть файлы, если необходимо
for _, file := range dirContent {
if !printFiles && !file.IsDir() {
continue
}
directories = append(directories, file)
}
level++
for i, file := range directories {
fileName := file.Name()
if !file.IsDir() {
fileName += " (" + getSize(file) + ")"
}
if i == len(directories)-1 {
_, err = fmt.Fprintln(out, prefix+"└───"+fileName)
} else {
_, err = fmt.Fprintln(out, prefix+"├───"+fileName)
}
if err != nil {
return fmt.Errorf(err.Error())
}
currentPrefix := prefix
if file.IsDir() {
if i != len(directories)-1 {
currentPrefix = currentPrefix + "│ "
} else {
currentPrefix = currentPrefix + " "
}
err = dirLevel(out, path+string(os.PathSeparator)+file.Name(), printFiles, level, currentPrefix)
if err != nil {
return fmt.Errorf(err.Error())
}
}
}
return nil
}
func dirTree(out io.Writer, path string, printFiles bool) error {
return dirLevel(out, path, printFiles, -1, "")
}
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())
}
}