path tree

This commit is contained in:
Erik Winter 2025-02-25 09:22:10 +01:00
parent 652f7a78f3
commit 057fa5238d
3 changed files with 122 additions and 51 deletions

61
main.go
View File

@ -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 := "."

54
structure/ast.go Normal file
View File

@ -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
}

58
structure/tree.go Normal file
View File

@ -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+" ")
}
}
}