From 057fa5238d495e4f988857802061ae62e97737cd Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Tue, 25 Feb 2025 09:22:10 +0100 Subject: [PATCH] path tree --- main.go | 61 ++++++++--------------------------------------- structure/ast.go | 54 +++++++++++++++++++++++++++++++++++++++++ structure/tree.go | 58 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 51 deletions(-) create mode 100644 structure/ast.go create mode 100644 structure/tree.go diff --git a/main.go b/main.go index 965574b..a712b47 100644 --- a/main.go +++ b/main.go @@ -1,65 +1,24 @@ package main import ( - "bytes" "fmt" - "go/ast" - "go/format" - "go/parser" - "go/token" + + "go-mod.ewintr.nl/henk/structure" ) -// printNode prints a single AST node back to Go source code. -func printNode(node ast.Node) (string, error) { - var writer bytes.Buffer - err := format.Node(&writer, token.NewFileSet(), node) - if err != nil { - return "", err - } - return writer.String(), nil -} - -// walkFile walks through the AST and collects top-level declarations. -func walkFile(f *ast.File) ([]ast.Decl, error) { - var topLevelDecls []ast.Decl - - for _, decl := range f.Decls { - topLevelDecls = append(topLevelDecls, decl) - } - return topLevelDecls, nil -} - -// processGoFile processes a Go file and prints each top-level declaration. -func processGoFile(filePath string) error { - fset := token.NewFileSet() - f, err := parser.ParseFile(fset, filePath, nil, parser.AllErrors|parser.ParseComments) - if err != nil { - return fmt.Errorf("error parsing %s: %w", filePath, err) - } - - topLevelDecls, err := walkFile(f) - if err != nil { - return fmt.Errorf("error walking file: %w", err) - } - - for i, decl := range topLevelDecls { - snippet, err := printNode(decl) - if err != nil { - return fmt.Errorf("error printing node: %w", err) - } - fmt.Printf("Top-level Declaration %d:\n%s\n---\n", i+1, snippet) - } - - return nil -} - func main() { - filePath := "llm/ollama.go" // Replace with your Go file path + filePath := "." // Replace with your Go file path - err := processGoFile(filePath) + tree, err := structure.BuildTree(filePath) if err != nil { fmt.Println(err) } + structure.PrintTree(tree, "") + + // err := structure.ProcessGoFile(filePath) + // if err != nil { + // fmt.Println(err) + // } } // startDir := "." diff --git a/structure/ast.go b/structure/ast.go new file mode 100644 index 0000000..0e10d18 --- /dev/null +++ b/structure/ast.go @@ -0,0 +1,54 @@ +package structure + +import ( + "bytes" + "fmt" + "go/ast" + "go/format" + "go/parser" + "go/token" +) + +// printNode prints a single AST node back to Go source code. +func printNode(node ast.Node) (string, error) { + var writer bytes.Buffer + err := format.Node(&writer, token.NewFileSet(), node) + if err != nil { + return "", err + } + return writer.String(), nil +} + +// walkFile walks through the AST and collects top-level declarations. +func walkFile(f *ast.File) ([]ast.Decl, error) { + var topLevelDecls []ast.Decl + + for _, decl := range f.Decls { + topLevelDecls = append(topLevelDecls, decl) + } + return topLevelDecls, nil +} + +// processGoFile processes a Go file and prints each top-level declaration. +func ProcessGoFile(filePath string) error { + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, filePath, nil, parser.AllErrors|parser.ParseComments) + if err != nil { + return fmt.Errorf("error parsing %s: %w", filePath, err) + } + + topLevelDecls, err := walkFile(f) + if err != nil { + return fmt.Errorf("error walking file: %w", err) + } + + for i, decl := range topLevelDecls { + snippet, err := printNode(decl) + if err != nil { + return fmt.Errorf("error printing node: %w", err) + } + fmt.Printf("Top-level Declaration %d:\n%s\n---\n", i+1, snippet) + } + + return nil +} diff --git a/structure/tree.go b/structure/tree.go new file mode 100644 index 0000000..c985b3d --- /dev/null +++ b/structure/tree.go @@ -0,0 +1,58 @@ +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+" ") + } + } +}