-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (97 loc) · 2.47 KB
/
main.go
File metadata and controls
120 lines (97 loc) · 2.47 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"toolTreeDir/sizeByteConvert"
)
type Node interface {
fmt.Stringer
}
type Directory struct {
name string
folders []Node
}
type File struct {
name string
size int64
modTime string
}
func main() {
stdOut := os.Stdout
fmt.Fprintf(stdOut, "%s\n", os.Args[0])
if !(len(os.Args) == 2 || len(os.Args) == 3) {
fmt.Println(`
Enter the relative directory path.
--help, more info.
`)
return
} else if os.Args[1] == `--help` {
fmt.Println(` Displays the folder tree of the specified directory.
Specify the full path, or use "." for current.
-f flag will display information about the nested files (size in bytes, last modified date).
`)
return
} else {
path := os.Args[1]
printFiles := len(os.Args) == 3 && os.Args[2] == "-f"
err := dirTree(stdOut, path, printFiles)
if err != nil {
fmt.Printf("Open %v: no such file or directory\n", os.Args[1])
return
}
}
}
func (file *File) String() string {
if file.size == 0 {
return file.name + " (empty) " + file.modTime[:19]
}
return fmt.Sprintf("%v (%+v), %+v", file.name, sizeByteConvert.Convert(file.size), file.modTime[:19])
}
func (directory *Directory) String() string {
return directory.name
}
func readDir(path string, nodes []Node, withFiles bool) (error, []Node) {
files, err := ioutil.ReadDir(path)
for _, info := range files {
if !(info.IsDir() || withFiles) {
continue
}
var newNode Node
if info.IsDir() {
_, folders := readDir(filepath.Join(path, info.Name()), []Node{}, withFiles)
newNode = &Directory{info.Name(), folders}
} else {
newNode = &File{info.Name(), info.Size(), info.ModTime().String()}
}
nodes = append(nodes, newNode)
}
return err, nodes
}
func printDir(out io.Writer, nodes []Node, prefixes []string) {
if len(nodes) == 0 {
return
}
fmt.Fprintf(out, "%s", strings.Join(prefixes, ""))
node := nodes[0]
if len(nodes) == 1 {
fmt.Fprintf(out, "%s%s\n", "└───", node)
if directory, ok := node.(*Directory); ok {
printDir(out, directory.folders, append(prefixes, " "))
}
return
}
fmt.Fprintf(out, "%s%s\n", "├───", node)
if directory, ok := node.(*Directory); ok {
printDir(out, directory.folders, append(prefixes, "│ "))
}
printDir(out, nodes[1:], prefixes)
}
func dirTree(out io.Writer, path string, pritnFiles bool) error {
err, nodes := readDir(path, []Node{}, pritnFiles)
printDir(out, nodes, []string{})
return err
}