path tree
This commit is contained in:
parent
652f7a78f3
commit
057fa5238d
61
main.go
61
main.go
|
@ -1,65 +1,24 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
|
||||||
"go/format"
|
"go-mod.ewintr.nl/henk/structure"
|
||||||
"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
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
structure.PrintTree(tree, "")
|
||||||
|
|
||||||
|
// err := structure.ProcessGoFile(filePath)
|
||||||
|
// if err != nil {
|
||||||
|
// fmt.Println(err)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
// startDir := "."
|
// startDir := "."
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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+" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue