Merge pull request #1258 from nspcc-dev/feature/parse

Allow to use `vm parse` directly
This commit is contained in:
Roman Khimov 2020-08-05 09:59:38 +03:00 committed by GitHub
commit fe779937ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 9 deletions

View file

@ -5,6 +5,7 @@ import (
"github.com/nspcc-dev/neo-go/cli/server" "github.com/nspcc-dev/neo-go/cli/server"
"github.com/nspcc-dev/neo-go/cli/smartcontract" "github.com/nspcc-dev/neo-go/cli/smartcontract"
"github.com/nspcc-dev/neo-go/cli/util"
"github.com/nspcc-dev/neo-go/cli/vm" "github.com/nspcc-dev/neo-go/cli/vm"
"github.com/nspcc-dev/neo-go/cli/wallet" "github.com/nspcc-dev/neo-go/cli/wallet"
"github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config"
@ -21,6 +22,7 @@ func main() {
ctl.Commands = append(ctl.Commands, smartcontract.NewCommands()...) ctl.Commands = append(ctl.Commands, smartcontract.NewCommands()...)
ctl.Commands = append(ctl.Commands, wallet.NewCommands()...) ctl.Commands = append(ctl.Commands, wallet.NewCommands()...)
ctl.Commands = append(ctl.Commands, vm.NewCommands()...) ctl.Commands = append(ctl.Commands, vm.NewCommands()...)
ctl.Commands = append(ctl.Commands, util.NewCommands()...)
if err := ctl.Run(os.Args); err != nil { if err := ctl.Run(os.Args); err != nil {
panic(err) panic(err)

37
cli/util/convert.go Normal file
View file

@ -0,0 +1,37 @@
package util
import (
"fmt"
vmcli "github.com/nspcc-dev/neo-go/pkg/vm/cli"
"github.com/urfave/cli"
)
func NewCommands() []cli.Command {
return []cli.Command{
{
Name: "util",
Usage: "Various helper commands",
Subcommands: []cli.Command{
{
Name: "convert",
Usage: "Convert provided argument into other possible formats",
UsageText: `convert <arg>
<arg> is an argument which is tried to be interpreted as an item of different types
and converted to other formats. Strings are escaped and output in quotes.`,
Action: handleParse,
},
},
},
}
}
func handleParse(ctx *cli.Context) error {
res, err := vmcli.Parse(ctx.Args())
if err != nil {
return cli.NewExitError(err, 1)
}
fmt.Print(res)
return nil
}

View file

@ -454,11 +454,20 @@ func (c *VMCLI) Run() error {
} }
func handleParse(c *ishell.Context) { func handleParse(c *ishell.Context) {
if len(c.Args) < 1 { res, err := Parse(c.Args)
c.Err(errors.New("missing argument")) if err != nil {
c.Err(err)
return return
} }
arg := c.Args[0] c.Print(res)
}
// Parse converts it's argument to other formats.
func Parse(args []string) (string, error) {
if len(args) < 1 {
return "", errors.New("missing argument")
}
arg := args[0]
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
if val, err := strconv.ParseInt(arg, 10, 64); err == nil { if val, err := strconv.ParseInt(arg, 10, 64); err == nil {
bs := bigint.ToBytes(big.NewInt(val)) bs := bigint.ToBytes(big.NewInt(val))
@ -491,16 +500,14 @@ func handleParse(c *ishell.Context) {
out := buf.Bytes() out := buf.Bytes()
buf = bytes.NewBuffer(nil) buf = bytes.NewBuffer(nil)
w := tabwriter.NewWriter(buf, 0, 0, 4, ' ', 0) w := tabwriter.NewWriter(buf, 0, 4, 4, '\t', 0)
if _, err := w.Write(out); err != nil { if _, err := w.Write(out); err != nil {
c.Err(err) return "", err
return
} }
if err := w.Flush(); err != nil { if err := w.Flush(); err != nil {
c.Err(err) return "", err
return
} }
c.Print(string(buf.Bytes())) return string(buf.Bytes()), nil
} }
func parseArgs(args []string) ([]stackitem.Item, error) { func parseArgs(args []string) ([]stackitem.Item, error) {