forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goscan.go
51 lines (41 loc) · 1010 Bytes
/
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
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,
)
cmd.Execute(context.Background(), os.Args[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
}