diff --git a/gnovm/cmd/gno/main.go b/gnovm/cmd/gno/main.go index 7a5799f2835..60930e86e4d 100644 --- a/gnovm/cmd/gno/main.go +++ b/gnovm/cmd/gno/main.go @@ -35,6 +35,7 @@ func newGnocliCmd(io commands.IO) *commands.Command { newEnvCmd(io), newBugCmd(io), newFmtCmd(io), + newPkgAddrCmd(io), // graph // vendor -- download deps from the chain in vendor/ // list -- list packages diff --git a/gnovm/cmd/gno/pkgaddr.go b/gnovm/cmd/gno/pkgaddr.go new file mode 100644 index 00000000000..6f2b38ad172 --- /dev/null +++ b/gnovm/cmd/gno/pkgaddr.go @@ -0,0 +1,33 @@ +package main + +import ( + "context" + "flag" + + "github.com/gnolang/gno/gnovm/pkg/gnolang" + "github.com/gnolang/gno/tm2/pkg/commands" +) + +func newPkgAddrCmd(io commands.IO) *commands.Command { + return commands.NewCommand( + commands.Metadata{ + Name: "pkgaddr", + ShortUsage: "pkgaddr ", + ShortHelp: "`pkgaddr` converts a package path to a package address", + }, + nil, + func(_ context.Context, args []string) error { + return execPkgAddr(args, io) + }, + ) +} + +func execPkgAddr(args []string, io commands.IO) error { + if len(args) != 1 { + return flag.ErrHelp + } + + io.Println(gnolang.DerivePkgAddr(args[0])) + + return nil +} diff --git a/gnovm/cmd/gno/pkgaddr_test.go b/gnovm/cmd/gno/pkgaddr_test.go new file mode 100644 index 00000000000..1969172ed0b --- /dev/null +++ b/gnovm/cmd/gno/pkgaddr_test.go @@ -0,0 +1,26 @@ +package main + +import ( + "testing" +) + +func TestPkgAddrApp(t *testing.T) { + tc := []testMainCase{ + { + args: []string{"pkgaddr"}, + errShouldContain: "flag: help requested", + }, + + { + args: []string{"pkgaddr", "bli", "blu"}, + errShouldContain: "flag: help requested", + }, + + { + args: []string{"pkgaddr", "gno.land/r/demo/users"}, + stdoutShouldBe: "g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s\n", + }, + } + + testMainCaseRun(t, tc) +}