forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goscan.go
55 lines (44 loc) · 1.08 KB
/
goscan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"flag"
"fmt"
"go/parser"
"go/token"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/gnolang/gno/tm2/pkg/commands"
)
func main() {
cmd := commands.NewCommand(
commands.Metadata{
ShortUsage: "<file-path>",
LongHelp: "Prints out the imports for a given file's AST",
},
commands.NewEmptyConfig(),
execScan,
)
if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}
func execScan(_ context.Context, args []string) error {
if len(args) < 1 {
return flag.ErrHelp
}
fset := token.NewFileSet() // positions are relative to fset
filename := args[0]
bz, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("unable to read file, %w", err)
}
// Parse src but stop after processing the imports.
f, err := parser.ParseFile(fset, "", string(bz), parser.ParseComments|parser.DeclarationErrors)
if err != nil {
return fmt.Errorf("unable to parse file, %w", err)
}
// Print the imports from the file's AST.
spew.Dump(f)
return nil
}