From 97994d7aad54ce07afdb013a3c96967a1d328a67 Mon Sep 17 00:00:00 2001 From: Kent 'picat' Gruber Date: Tue, 2 Jan 2024 23:50:37 -0500 Subject: [PATCH] Move `ssadump` to `cmd/ssadump` --- {ssadump => cmd/ssadump}/main.go | 33 +++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) rename {ssadump => cmd/ssadump}/main.go (57%) diff --git a/ssadump/main.go b/cmd/ssadump/main.go similarity index 57% rename from ssadump/main.go rename to cmd/ssadump/main.go index def2d19..ab7eaf4 100644 --- a/ssadump/main.go +++ b/cmd/ssadump/main.go @@ -1,8 +1,13 @@ package main import ( + "context" "fmt" + "go/ast" + "go/parser" + "go/token" "os" + "os/signal" "golang.org/x/tools/go/packages" "golang.org/x/tools/go/ssa" @@ -10,9 +15,17 @@ import ( ) func main() { + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + defer cancel() + patterns := os.Args[1:] - var allNeeds = packages.NeedName | + if len(patterns) == 0 { + fmt.Fprintf(os.Stderr, "usage: %s \n", os.Args[0]) + os.Exit(1) + } + + loadMode := packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | @@ -22,8 +35,22 @@ func main() { packages.NeedTypesInfo | packages.NeedDeps - var cfg = &packages.Config{ - Mode: allNeeds, + parseMode := parser.SkipObjectResolution + + dir, err := os.Getwd() + if err != nil { + fmt.Fprintf(os.Stderr, "failed to get current working directory: %v\n", err.Error()) + os.Exit(1) + } + + cfg := &packages.Config{ + Mode: loadMode, + Context: ctx, + Dir: dir, + Env: os.Environ(), + ParseFile: func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) { + return parser.ParseFile(fset, filename, src, parseMode) + }, } initial, err := packages.Load(cfg, patterns...)