-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
186 lines (150 loc) · 3.45 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
const (
version = "1.0.0"
bold = "\033[1m"
reset = "\033[0m"
gap = " "
ascChild = "├── "
ascSubChild = "│ "
ascEndChild = "└── "
txtChild = "|-- "
txtSubChild = "| "
txtEndChild = "+-- "
)
func main() {
versionFlag := flag.Bool("version", false, "Print version number")
helpFlag := flag.Bool("help", false, "Print help message")
flag.Usage = func() {
fmt.Println("Use --help for usage instructions")
}
flag.Parse()
if *versionFlag {
fmt.Println("treegen", version)
return
}
if *helpFlag {
helpMessage()
return
}
if flag.NArg() > 0 {
for _, path := range flag.Args() {
content := readFromFile(path)
generate(content)
}
} else {
content := readFromStdIn()
generate(content)
}
}
func helpMessage() {
fmt.Println(fmt.Sprintf("\n%sUsage:%s treegen [OPTIONS] [STDIN|FILE...]", bold, reset))
fmt.Println(fmt.Sprintf("\n%sOptions:%s", bold, reset))
fmt.Println("\t--help Print help message")
fmt.Println("\t--version Print version number")
fmt.Println(fmt.Sprintf("\n%sExamples:%s", bold, reset))
fmt.Println("\t$ treegen tree_structure.txt")
fmt.Println("\t$ cat tree_structure.txt | treegen")
fmt.Println("\t$ treegen < tree_structure.txt")
fmt.Println("\t$ treegen <<-EOF")
fmt.Println("\t ...")
fmt.Println("\tEOF\n")
}
func readFromFile(path string) string {
file, err := os.Open(path)
if err != nil {
fmt.Println("File not found:", path)
os.Exit(1)
}
content, err := io.ReadAll(file)
if err != nil {
fmt.Println("File not readable:", path)
os.Exit(1)
}
_ = file.Close()
return string(content)
}
func readFromStdIn() string {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
helpMessage()
os.Exit(2)
}
content, _ := io.ReadAll(os.Stdin)
return string(content)
}
func generate(tree string) {
lines := strings.Split(tree, "\n")
level := 0
dir := lines[0]
createDirectory(dir)
for i, line := range lines[1:] {
currentLevel := nodeLevel(line)
if currentLevel > level {
parentDir := nodeName(lines[i]) // name of previous line
dir = dir + parentDir
level++
} else if currentLevel < level {
dir = strings.TrimRight(dir, "/")
dir = moveUpDirectories(dir, level-currentLevel)
dir = dir + "/"
level = currentLevel // could drop down many levels
}
nodePath := dir + nodeName(line)
if strings.HasSuffix(nodePath, "/") {
createDirectory(nodePath)
} else {
createFile(nodePath)
}
}
fmt.Println("Directory structure created successfully")
}
func nodeLevel(line string) int {
level := 0
for {
if strings.HasPrefix(line, ascSubChild) {
level++
line = line[len(ascSubChild):]
} else if strings.HasPrefix(line, txtSubChild) {
level++
line = line[len(txtSubChild):]
} else if strings.HasPrefix(line, gap) {
level++
line = line[len(gap):]
} else {
break
}
}
return level
}
func nodeName(line string) string {
return strings.TrimLeft(line, "└├ ─│|+-")
}
func moveUpDirectories(path string, n int) string {
for i := 0; i < n; i++ {
path = filepath.Dir(path)
}
return path
}
func createDirectory(path string) {
err := os.MkdirAll(path, 0755)
if err != nil {
fmt.Println("Directory not created:", path)
os.Exit(1)
}
}
func createFile(path string) {
file, err := os.Create(path)
if err != nil {
fmt.Println("File not created:", path)
os.Exit(1)
}
_ = file.Close()
}