extendable tree
This commit is contained in:
parent
057fa5238d
commit
23dc5c05a5
6
main.go
6
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"go-mod.ewintr.nl/henk/structure"
|
"go-mod.ewintr.nl/henk/structure"
|
||||||
)
|
)
|
||||||
|
@ -9,11 +10,12 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
filePath := "." // Replace with your Go file path
|
filePath := "." // Replace with your Go file path
|
||||||
|
|
||||||
tree, err := structure.BuildTree(filePath)
|
project, err := structure.NewProject(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
structure.PrintTree(tree, "")
|
fmt.Printf("%s\n", project.Tree())
|
||||||
|
|
||||||
// err := structure.ProcessGoFile(filePath)
|
// err := structure.ProcessGoFile(filePath)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
|
|
|
@ -0,0 +1,156 @@
|
||||||
|
package structure
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ElementType string
|
||||||
|
|
||||||
|
type Element struct {
|
||||||
|
Type ElementType
|
||||||
|
Description string
|
||||||
|
Content string
|
||||||
|
}
|
||||||
|
|
||||||
|
type File struct {
|
||||||
|
Description string
|
||||||
|
Path string
|
||||||
|
Elements []Element
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *File) Name() string { return filepath.Base(f.Path) }
|
||||||
|
|
||||||
|
type Directory struct {
|
||||||
|
Module bool
|
||||||
|
Path string
|
||||||
|
Subs map[string]*Directory
|
||||||
|
Files map[string]*File
|
||||||
|
}
|
||||||
|
|
||||||
|
type Project struct {
|
||||||
|
Dirs map[string]*Directory
|
||||||
|
Files map[string]*File
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewProject(path string) (*Project, error) {
|
||||||
|
fileInfo, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !fileInfo.IsDir() {
|
||||||
|
return nil, fmt.Errorf("path is not a directory")
|
||||||
|
}
|
||||||
|
|
||||||
|
project := &Project{
|
||||||
|
Dirs: make(map[string]*Directory),
|
||||||
|
Files: make(map[string]*File),
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := os.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
// skip hidden files for now
|
||||||
|
if strings.HasPrefix(file.Name(), ".") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
childPath := filepath.Join(path, file.Name())
|
||||||
|
childInfo, err := os.Stat(childPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if childInfo.IsDir() {
|
||||||
|
m, err := NewDirectory(childPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
project.Dirs[childPath] = m
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
f, err := NewFile(childPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
project.Files[childPath] = f
|
||||||
|
}
|
||||||
|
|
||||||
|
return project, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Project) Tree() string {
|
||||||
|
res := make([]string, 0)
|
||||||
|
for _, d := range p.Dirs {
|
||||||
|
res = append(res, d.Tree(2)...)
|
||||||
|
}
|
||||||
|
for _, f := range p.Files {
|
||||||
|
res = append(res, f.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(res, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFile(path string) (*File, error) {
|
||||||
|
// fmt.Println(path)
|
||||||
|
file := &File{
|
||||||
|
Path: path,
|
||||||
|
}
|
||||||
|
|
||||||
|
return file, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDirectory(path string) (*Directory, error) {
|
||||||
|
dir := &Directory{
|
||||||
|
Path: path,
|
||||||
|
Subs: make(map[string]*Directory),
|
||||||
|
Files: make(map[string]*File),
|
||||||
|
}
|
||||||
|
|
||||||
|
paths, err := os.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, p := range paths {
|
||||||
|
childPath := filepath.Join(path, p.Name())
|
||||||
|
childInfo, err := os.Stat(childPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if childInfo.IsDir() {
|
||||||
|
d, err := NewDirectory(childPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dir.Subs[childPath] = d
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
f, err := NewFile(childPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dir.Files[childPath] = f
|
||||||
|
}
|
||||||
|
|
||||||
|
return dir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Directory) Tree(indent int) []string {
|
||||||
|
in := ""
|
||||||
|
for i := 0; i < indent; i++ {
|
||||||
|
in += " "
|
||||||
|
}
|
||||||
|
res := []string{d.Path}
|
||||||
|
for _, d := range d.Subs {
|
||||||
|
res = append(res, d.Tree(indent+2)...)
|
||||||
|
}
|
||||||
|
for _, f := range d.Files {
|
||||||
|
res = append(res, fmt.Sprintf("%s%s", in, f.Name()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
}
|
|
@ -1,58 +0,0 @@
|
||||||
package structure
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type PathTree struct {
|
|
||||||
Name string
|
|
||||||
IsDir bool
|
|
||||||
Children []*PathTree
|
|
||||||
}
|
|
||||||
|
|
||||||
func BuildTree(path string) (*PathTree, error) {
|
|
||||||
fileInfo, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
node := &PathTree{
|
|
||||||
Name: filepath.Base(path),
|
|
||||||
IsDir: fileInfo.IsDir(),
|
|
||||||
}
|
|
||||||
|
|
||||||
if !node.IsDir {
|
|
||||||
return node, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
files, err := os.ReadDir(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, file := range files {
|
|
||||||
if strings.HasPrefix(file.Name(), ".") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
childPath := filepath.Join(path, file.Name())
|
|
||||||
childNode, err := BuildTree(childPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
node.Children = append(node.Children, childNode)
|
|
||||||
}
|
|
||||||
|
|
||||||
return node, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintTree(node *PathTree, prefix string) {
|
|
||||||
fmt.Printf("%v* %s\n", prefix, node.Name)
|
|
||||||
newPrefix := prefix + " "
|
|
||||||
if len(node.Children) > 0 {
|
|
||||||
for _, child := range node.Children {
|
|
||||||
PrintTree(child, newPrefix+" ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue