forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
87 lines (72 loc) · 1.51 KB
/
run.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"context"
"flag"
"github.com/gnolang/gno/pkgs/commands"
gno "github.com/gnolang/gno/pkgs/gnolang"
"github.com/gnolang/gno/tests"
)
type runCfg struct {
verbose bool
rootDir string
}
func newRunCmd(io *commands.IO) *commands.Command {
cfg := &runCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "run",
ShortUsage: "run [flags] <file> [<file>...]",
ShortHelp: "Runs the specified gno files",
},
cfg,
func(_ context.Context, args []string) error {
return execRun(cfg, args, io)
},
)
}
func (c *runCfg) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(
&c.verbose,
"verbose",
false,
"verbose output when running",
)
fs.StringVar(
&c.rootDir,
"root-dir",
"",
"clone location of github.com/gnolang/gno (gnodev tries to guess it)",
)
}
func execRun(cfg *runCfg, args []string, io *commands.IO) error {
if len(args) == 0 {
return flag.ErrHelp
}
if cfg.rootDir == "" {
cfg.rootDir = guessRootDir()
}
stdin := io.In
stdout := io.Out
stderr := io.Err
// init store and machine
testStore := tests.TestStore(cfg.rootDir,
"", stdin, stdout, stderr,
tests.ImportModeStdlibsPreferred)
if cfg.verbose {
testStore.SetLogStoreOps(true)
}
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "main",
Output: stdout,
Store: testStore,
})
// read files
files := make([]*gno.FileNode, len(args))
for i, fname := range args {
files[i] = gno.MustReadFile(fname)
}
// run files
m.RunFiles(files...)
m.RunMain()
return nil
}