ollie/tools/codeintel/cmd/codebase_overview/main.go

52 lines
1.0 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"ollie/tools/codeintel/internal/codeintel"
)
type args struct {
Path string `json:"path"`
}
func main() {
var a args
if err := json.NewDecoder(os.Stdin).Decode(&a); err != nil {
fail(err)
}
root, err := filepath.Abs(a.Path)
if err != nil {
fail(err)
}
files, err := codeintel.Files(root)
if err != nil {
fail(err)
}
fmt.Printf("# Codebase Overview\n\nPath: `%s`\n\n", root)
for _, f := range files {
d, err := codeintel.Parse(f.Path, f.Language)
if err != nil {
continue
}
fmt.Printf("## `%s` (%s)\n\n", rel(root, f.Path), f.Language)
for _, s := range codeintel.Symbols(d) {
fmt.Printf("- %s `%s` — line %d\n", strings.Repeat(" ", min(s.Depth, 4)), s.Name, s.Line)
}
fmt.Println()
d.Close()
}
}
func rel(root, path string) string { p, _ := filepath.Rel(root, path); return p }
func min(a, b int) int {
if a < b {
return a
}
return b
}
func fail(err error) { fmt.Fprintln(os.Stderr, "error:", err); os.Exit(1) }