From 12dd3e3c7f205e712745ea0d74cbec4fd2543690 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 18 Nov 2021 14:14:43 -0800 Subject: [PATCH] cmd/allsrc: WIP tool to print out all a program's source Change-Id: Ie8ed3ad744af5b5b7772cb9b4516a9b8e2f2866d Signed-off-by: Brad Fitzpatrick --- cmd/allsrc/allsrc.go | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 cmd/allsrc/allsrc.go diff --git a/cmd/allsrc/allsrc.go b/cmd/allsrc/allsrc.go new file mode 100644 index 000000000..0ca4b8b06 --- /dev/null +++ b/cmd/allsrc/allsrc.go @@ -0,0 +1,84 @@ +package main + +import ( + "flag" + "fmt" + "log" + "sort" + + "golang.org/x/tools/go/packages" +) + +var cfg = &packages.Config{ + Mode: (0 | + packages.NeedName | + packages.NeedFiles | + packages.NeedCompiledGoFiles | + packages.NeedImports | + packages.NeedDeps | + packages.NeedModule | + packages.NeedTypes | + packages.NeedSyntax | + 0), +} + +func main() { + flag.Parse() + + var w walker + w.walk("tailscale.com/cmd/tailscaled") +} + +type walker struct { + done map[string]bool +} + +func (w *walker) walk(mainPkg string) { + pkgs, err := packages.Load(cfg, mainPkg) + if err != nil { + log.Fatalf("packages.Load: %v", err) + } + for _, pkg := range pkgs { + w.walkPackage(pkg) + } +} + +func (w *walker) walkPackage(pkg *packages.Package) { + if w.done[pkg.PkgPath] { + return + } + if w.done == nil { + w.done = map[string]bool{} + } + w.done[pkg.PkgPath] = true + + fmt.Printf("\n### PACKAGE %v\n", pkg.PkgPath) + + if len(pkg.Errors) > 0 { + log.Fatalf("errors reading %q: %q", pkg.PkgPath, pkg.Errors) + } + + var imports []*packages.Package + for _, p := range pkg.Imports { + imports = append(imports, p) + } + sort.Slice(imports, func(i, j int) bool { + return imports[i].PkgPath < imports[j].PkgPath + }) + for _, f := range pkg.GoFiles { + fmt.Printf("file.go %q\n", f) + } + for _, f := range pkg.OtherFiles { + fmt.Printf("file.other %q\n", f) + } + for _, p := range imports { + fmt.Printf("import %q => %q\n", pkg.PkgPath, p.PkgPath) + } + fmt.Printf("Fset: %p\n", pkg.Fset) + fmt.Printf("Syntax: %v\n", len(pkg.Syntax)) + fmt.Printf("Modules: %+v\n", pkg.Module) + + for _, p := range imports { + w.walkPackage(p) + } +}