diff --git a/explorer/cmd/cmd.go b/explorer/cmd/cmd.go new file mode 100644 index 00000000..9bd0da7f --- /dev/null +++ b/explorer/cmd/cmd.go @@ -0,0 +1,21 @@ +package cmd + +import "github.com/ignite/cli/v28/ignite/services/plugin" + +// GetCommands returns the list of explorer app commands. +func GetCommands() []*plugin.Command { + return []*plugin.Command{ + { + Use: "explorer [command]", + Short: "Run chain explorer commands", + Aliases: []string{"e"}, + Commands: []*plugin.Command{ + { + Use: "gex [rpc_url]", + Short: "Run gex", + Aliases: []string{"g"}, + }, + }, + }, + } +} diff --git a/explorer/cmd/explorer.go b/explorer/cmd/explorer.go deleted file mode 100644 index 63c62e80..00000000 --- a/explorer/cmd/explorer.go +++ /dev/null @@ -1,24 +0,0 @@ -package cmd - -import ( - "github.com/spf13/cobra" -) - -// NewExplorer creates a new explorer command that holds -// some other sub commands related to running chain explorers like gex and etc. -func NewExplorer() *cobra.Command { - c := &cobra.Command{ - Use: "explorer [command]", - Aliases: []string{"e"}, - Short: "Run chain explorer commands", - SilenceUsage: true, - SilenceErrors: true, - } - - // add sub commands. - c.AddCommand( - NewGex(), - ) - - return c -} diff --git a/explorer/cmd/gex.go b/explorer/cmd/gex.go index fde52db8..2216faf5 100644 --- a/explorer/cmd/gex.go +++ b/explorer/cmd/gex.go @@ -1,57 +1,51 @@ package cmd import ( + "context" + "fmt" "net/url" "os" + "github.com/ignite/cli/v28/ignite/services/plugin" "github.com/pkg/errors" - "github.com/spf13/cobra" - "github.com/ignite/apps/explorer/pkg/gex" + "github.com/ignite/apps/explorer/gex" ) -const ( - defaultHost = "localhost" - defaultPort = "26657" -) +const maxNumArgs = 1 -func NewGex() *cobra.Command { - c := &cobra.Command{ - Use: "gex [rpc_url]", - Aliases: []string{"g"}, - Short: "Run gex", - Args: cobra.MaximumNArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - host := defaultHost - port := defaultPort - ssl := false - - if len(args) == 1 { - rpcURL, err := url.Parse(args[0]) - if err != nil { - return errors.Wrapf(err, "failed to parse rpc url %s", args[0]) - } - - host = rpcURL.Hostname() - port = rpcURL.Port() - ssl = rpcURL.Scheme == "https" - if port == "" { - if ssl { - port = "443" - } else { - port = "80" - } - } - } +// ExecuteGex executes explorer gex subcommand. +func ExecuteGex(ctx context.Context, cmd *plugin.ExecutedCommand) error { + argc := len(cmd.Args) + if argc > maxNumArgs { + return fmt.Errorf("accepts at most %d arg(s), received %d", maxNumArgs, argc) + } - g, err := gex.New() - if err != nil { - return errors.Wrap(err, "failed to initialize gex") + ssl := false + host := "localhost" + port := "26657" + + if argc == 1 { + rpcURL, err := url.Parse(cmd.Args[0]) + if err != nil { + return errors.Wrapf(err, "failed to parse RPC URL %s", cmd.Args[0]) + } + + ssl = rpcURL.Scheme == "https" + host = rpcURL.Hostname() + port = rpcURL.Port() + if port == "" { + if ssl { + port = "443" + } else { + port = "80" } - - return g.Run(cmd.Context(), os.Stdout, os.Stderr, host, port, ssl) - }, + } } - return c + g, err := gex.New() + if err != nil { + return errors.Wrap(err, "failed to initialize Gex") + } + return g.Run(ctx, os.Stdout, os.Stderr, host, port, ssl) } diff --git a/explorer/pkg/gex/gex.go b/explorer/gex/gex.go similarity index 94% rename from explorer/pkg/gex/gex.go rename to explorer/gex/gex.go index ccf03ca1..1452edf1 100644 --- a/explorer/pkg/gex/gex.go +++ b/explorer/gex/gex.go @@ -15,13 +15,13 @@ import ( "github.com/pkg/errors" ) -// Gex represents the gex binary structure. +// Gex represents the Gex binary structure. type Gex struct { path string cleanup func() } -// New returns the hermes binary executable. +// New returns the Gex binary executable. func New() (*Gex, error) { // untar the binary. gzr, err := gzip.NewReader(bytes.NewReader(gex.Binary())) diff --git a/explorer/go.mod b/explorer/go.mod index 516f11c8..1bc9f424 100644 --- a/explorer/go.mod +++ b/explorer/go.mod @@ -7,7 +7,6 @@ require ( github.com/ignite/cli/v28 v28.0.0 github.com/ignite/ignite-files/gex v0.0.1 github.com/pkg/errors v0.9.1 - github.com/spf13/cobra v1.8.0 ) require ( @@ -64,6 +63,7 @@ require ( github.com/skeema/knownhosts v1.2.0 // indirect github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d // indirect github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e // indirect + github.com/spf13/cobra v1.8.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect go.etcd.io/bbolt v1.3.8 // indirect diff --git a/explorer/main.go b/explorer/main.go index 9ad856cc..6b17260e 100644 --- a/explorer/main.go +++ b/explorer/main.go @@ -2,7 +2,7 @@ package main import ( "context" - "os" + "fmt" hplugin "github.com/hashicorp/go-plugin" "github.com/ignite/cli/v28/ignite/services/plugin" @@ -13,19 +13,22 @@ import ( type app struct{} func (app) Manifest(context.Context) (*plugin.Manifest, error) { - m := &plugin.Manifest{Name: "explorer"} - m.ImportCobraCommand(cmd.NewExplorer(), "ignite") - return m, nil + return &plugin.Manifest{ + Name: "explorer", + Commands: cmd.GetCommands(), + }, nil } -func (app) Execute(_ context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error { - // Instead of a switch on c.Use, we run the root command like if - // we were in a command line context. This implies to set os.Args - // correctly. - // Remove the first arg "ignite" from OSArgs because our explorer - // command root is "explorer" not "ignite". - os.Args = c.OsArgs[1:] - return cmd.NewExplorer().Execute() +func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error { + args := c.OsArgs + name := args[len(args)-1] + + switch name { + case "gex": + return cmd.ExecuteGex(ctx, c) + default: + return fmt.Errorf("unknown command: %s", c.Path) + } } func (app) ExecuteHookPre(context.Context, *plugin.ExecutedHook, plugin.ClientAPI) error {