From fbc608eae3ebf6e70d0345b7ddccc411248f8d00 Mon Sep 17 00:00:00 2001 From: Norman Date: Sat, 28 Dec 2024 14:48:24 +0100 Subject: [PATCH 1/3] feat(cmd/gno): pkgaddr subcommand Signed-off-by: Norman --- gnovm/cmd/gno/main.go | 1 + gnovm/cmd/gno/pkgaddr.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 gnovm/cmd/gno/pkgaddr.go 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..baf4700de95 --- /dev/null +++ b/gnovm/cmd/gno/pkgaddr.go @@ -0,0 +1,33 @@ +package main + +import ( + "context" + "fmt" + + "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 fmt.Errorf("expected 1 arg, got %d", len(args)) + } + + io.Println(gnolang.DerivePkgAddr(args[0])) + + return nil +} From 3a519b3baa9b10ab34ce875d062c7a835796bbad Mon Sep 17 00:00:00 2001 From: Norman Date: Sat, 28 Dec 2024 15:09:52 +0100 Subject: [PATCH 2/3] chore: add pkgaddr test Signed-off-by: Norman --- gnovm/cmd/gno/pkgaddr_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 gnovm/cmd/gno/pkgaddr_test.go diff --git a/gnovm/cmd/gno/pkgaddr_test.go b/gnovm/cmd/gno/pkgaddr_test.go new file mode 100644 index 00000000000..936daedbf4c --- /dev/null +++ b/gnovm/cmd/gno/pkgaddr_test.go @@ -0,0 +1,21 @@ +package main + +import ( + "testing" +) + +func TestPkgAddrApp(t *testing.T) { + tc := []testMainCase{ + { + args: []string{"pkgaddr"}, + errShouldBe: "expected 1 arg, got 0", + }, + + { + args: []string{"pkgaddr", "gno.land/r/demo/users"}, + stdoutShouldBe: "g17m4ga9t9dxn8uf06p3cahdavzfexe33ecg8v2s\n", + }, + } + + testMainCaseRun(t, tc) +} From 8b1ddaf91fde5688bb71405f66f5388ac3b8e8cf Mon Sep 17 00:00:00 2001 From: Norman Date: Sat, 28 Dec 2024 15:12:55 +0100 Subject: [PATCH 3/3] chore: better error handling Signed-off-by: Norman --- gnovm/cmd/gno/pkgaddr.go | 4 ++-- gnovm/cmd/gno/pkgaddr_test.go | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/gnovm/cmd/gno/pkgaddr.go b/gnovm/cmd/gno/pkgaddr.go index baf4700de95..6f2b38ad172 100644 --- a/gnovm/cmd/gno/pkgaddr.go +++ b/gnovm/cmd/gno/pkgaddr.go @@ -2,7 +2,7 @@ package main import ( "context" - "fmt" + "flag" "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/tm2/pkg/commands" @@ -24,7 +24,7 @@ func newPkgAddrCmd(io commands.IO) *commands.Command { func execPkgAddr(args []string, io commands.IO) error { if len(args) != 1 { - return fmt.Errorf("expected 1 arg, got %d", len(args)) + return flag.ErrHelp } io.Println(gnolang.DerivePkgAddr(args[0])) diff --git a/gnovm/cmd/gno/pkgaddr_test.go b/gnovm/cmd/gno/pkgaddr_test.go index 936daedbf4c..1969172ed0b 100644 --- a/gnovm/cmd/gno/pkgaddr_test.go +++ b/gnovm/cmd/gno/pkgaddr_test.go @@ -7,8 +7,13 @@ import ( func TestPkgAddrApp(t *testing.T) { tc := []testMainCase{ { - args: []string{"pkgaddr"}, - errShouldBe: "expected 1 arg, got 0", + args: []string{"pkgaddr"}, + errShouldContain: "flag: help requested", + }, + + { + args: []string{"pkgaddr", "bli", "blu"}, + errShouldContain: "flag: help requested", }, {