2021-10-09 11:01:31 -06:00
|
|
|
package main
|
|
|
|
|
2021-10-09 13:59:22 -06:00
|
|
|
import (
|
2021-10-11 00:12:00 -06:00
|
|
|
"fmt"
|
2021-10-17 10:21:01 -06:00
|
|
|
"strings"
|
2021-10-09 13:59:22 -06:00
|
|
|
)
|
|
|
|
|
2021-10-17 10:21:01 -06:00
|
|
|
type FileNode struct {
|
|
|
|
children []FileNode
|
|
|
|
path string
|
|
|
|
parent *FileNode
|
|
|
|
absolutePath string
|
2021-10-09 11:01:31 -06:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:01 -06:00
|
|
|
func (f *FileNode) addChildren(path string) {
|
|
|
|
if f.path != "" {
|
|
|
|
f.children = append(f.children, FileNode{children: make([]FileNode, 0), path: path, parent: f, absolutePath: f.absolutePath + "/" + path})
|
|
|
|
} else {
|
|
|
|
f.children = append(f.children, FileNode{children: make([]FileNode, 0), path: path, parent: f, absolutePath: f.absolutePath + path})
|
|
|
|
}
|
2021-10-09 11:01:31 -06:00
|
|
|
}
|
2021-10-09 13:59:22 -06:00
|
|
|
|
2021-10-17 10:21:01 -06:00
|
|
|
func (f *FileNode) addChildNode(m FileNode) {
|
2021-10-09 13:59:22 -06:00
|
|
|
m.parent = f
|
|
|
|
f.children = append(f.children, m)
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:01 -06:00
|
|
|
func generateDirectoryTree(path []string) *FileNode {
|
2021-10-09 13:59:22 -06:00
|
|
|
var head *FileNode = new(FileNode)
|
2021-10-11 00:12:00 -06:00
|
|
|
var head1 *FileNode = head
|
2021-10-17 10:21:01 -06:00
|
|
|
for i := range path {
|
2021-10-11 00:12:00 -06:00
|
|
|
sepPaths := strings.Split(path[i], "/")
|
2021-10-17 10:21:01 -06:00
|
|
|
for j := range sepPaths {
|
|
|
|
if len(head.children) == 0 {
|
2021-10-11 00:12:00 -06:00
|
|
|
head.addChildren(sepPaths[j])
|
2021-10-17 10:21:01 -06:00
|
|
|
head = &(head.children[len(head.children)-1])
|
2021-10-11 00:12:00 -06:00
|
|
|
} else {
|
|
|
|
var headIsChanged = false
|
2021-10-17 10:21:01 -06:00
|
|
|
for k := range head.children {
|
2021-10-11 00:12:00 -06:00
|
|
|
if head.children[k].path == sepPaths[j] {
|
|
|
|
head = &(head.children[k])
|
|
|
|
headIsChanged = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-10-17 10:21:01 -06:00
|
|
|
if !headIsChanged {
|
2021-10-11 00:12:00 -06:00
|
|
|
head.addChildren(sepPaths[j])
|
2021-10-17 10:21:01 -06:00
|
|
|
head = &(head.children[len(head.children)-1])
|
2021-10-11 00:12:00 -06:00
|
|
|
}
|
2021-10-09 13:59:22 -06:00
|
|
|
}
|
|
|
|
}
|
2021-10-11 00:12:00 -06:00
|
|
|
head = head1
|
2021-10-09 13:59:22 -06:00
|
|
|
}
|
2021-10-11 00:12:00 -06:00
|
|
|
return head
|
2021-10-09 13:59:22 -06:00
|
|
|
}
|
2021-10-11 02:24:51 -06:00
|
|
|
|
2021-10-17 10:21:01 -06:00
|
|
|
func (f FileNode) Print(count int) {
|
|
|
|
if len(f.children) == 0 {
|
2021-10-11 02:24:51 -06:00
|
|
|
return
|
2021-10-17 10:21:01 -06:00
|
|
|
} else {
|
|
|
|
for i := range f.children {
|
|
|
|
for j := 0; j < count; j++ {
|
2021-10-11 02:24:51 -06:00
|
|
|
fmt.Print("---")
|
2021-10-17 10:21:01 -06:00
|
|
|
}
|
|
|
|
fmt.Println(f.children[i].absolutePath)
|
2021-10-11 02:24:51 -06:00
|
|
|
f.children[i].Print(count + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|