cli: move avm inspect from vm to contract command

Make inspect work with avms by default and with go files if told so. In the
end this makes our CLI interface more consistent and usable. Drop useless
CompileAndInspect() compiler method along the way.
This commit is contained in:
Roman Khimov 2019-10-29 12:56:44 +03:00
parent ae7687422c
commit e319c6c638
3 changed files with 21 additions and 54 deletions

View file

@ -6,14 +6,15 @@ import (
"context" "context"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"github.com/CityOfZion/neo-go/pkg/rpc" "github.com/CityOfZion/neo-go/pkg/rpc"
"github.com/CityOfZion/neo-go/pkg/vm"
"github.com/CityOfZion/neo-go/pkg/vm/compiler" "github.com/CityOfZion/neo-go/pkg/vm/compiler"
"github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -91,6 +92,10 @@ func NewCommands() []cli.Command {
Usage: "creates a user readable dump of the program instructions", Usage: "creates a user readable dump of the program instructions",
Action: inspect, Action: inspect,
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{
Name: "compile, c",
Usage: "compile input file (it should be go code then)",
},
cli.StringFlag{ cli.StringFlag{
Name: "in, i", Name: "in, i",
Usage: "input file of the program", Usage: "input file of the program",
@ -251,12 +256,24 @@ func parseContractDetails() ContractDetails {
} }
func inspect(ctx *cli.Context) error { func inspect(ctx *cli.Context) error {
src := ctx.String("in") in := ctx.String("in")
if len(src) == 0 { compile := ctx.Bool("compile")
if len(in) == 0 {
return cli.NewExitError(errNoInput, 1) return cli.NewExitError(errNoInput, 1)
} }
if err := compiler.CompileAndInspect(src); err != nil { b, err := ioutil.ReadFile(in)
if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
if compile {
b, err = compiler.Compile(bytes.NewReader(b), &compiler.Options{})
if err != nil {
return cli.NewExitError(errors.Wrap(err, "failed to compile"), 1)
}
}
v := vm.New()
v.LoadScript(b)
v.PrintOps()
return nil return nil
} }

View file

@ -1,10 +1,6 @@
package vm package vm
import ( import (
"errors"
"io/ioutil"
"github.com/CityOfZion/neo-go/pkg/vm"
vmcli "github.com/CityOfZion/neo-go/pkg/vm/cli" vmcli "github.com/CityOfZion/neo-go/pkg/vm/cli"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -18,19 +14,6 @@ func NewCommands() []cli.Command {
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.BoolFlag{Name: "debug, d"}, cli.BoolFlag{Name: "debug, d"},
}, },
Subcommands: []cli.Command{
{
Name: "inspect",
Usage: "dump instructions of the avm file given",
Action: inspect,
Flags: []cli.Flag{
cli.StringFlag{
Name: "in, i",
Usage: "input file of the program (AVM)",
},
},
},
},
}} }}
} }
@ -38,18 +21,3 @@ func startVMPrompt(ctx *cli.Context) error {
p := vmcli.New() p := vmcli.New()
return p.Run() return p.Run()
} }
func inspect(ctx *cli.Context) error {
avm := ctx.String("in")
if len(avm) == 0 {
return cli.NewExitError(errors.New("no input file given"), 1)
}
b, err := ioutil.ReadFile(avm)
if err != nil {
return cli.NewExitError(err, 1)
}
v := vm.New()
v.LoadScript(b)
v.PrintOps()
return nil
}

View file

@ -14,7 +14,6 @@ import (
"os" "os"
"strings" "strings"
"github.com/CityOfZion/neo-go/pkg/vm"
"golang.org/x/tools/go/loader" "golang.org/x/tools/go/loader"
) )
@ -96,23 +95,6 @@ func CompileAndSave(src string, o *Options) error {
return ioutil.WriteFile(out, b, os.ModePerm) return ioutil.WriteFile(out, b, os.ModePerm)
} }
// CompileAndInspect compiles the program and dumps the opcode in a user friendly format.
func CompileAndInspect(src string) error {
b, err := ioutil.ReadFile(src)
if err != nil {
return err
}
b, err = Compile(bytes.NewReader(b), &Options{})
if err != nil {
return err
}
v := vm.New()
v.LoadScript(b)
v.PrintOps()
return nil
}
func gopath() string { func gopath() string {
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")
if len(gopath) == 0 { if len(gopath) == 0 {