Skip to content

Commit

Permalink
refactor: remove Cobra dependency from explorer app (#38)
Browse files Browse the repository at this point in the history
* refactor: remove Cobra dependency from explorer app

* chore: move commands definitions to a public function
  • Loading branch information
jeronimoalbi authored Jan 16, 2024
1 parent 3a9c9c3 commit 8221992
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 80 deletions.
21 changes: 21 additions & 0 deletions explorer/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -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"},
},
},
},
}
}
24 changes: 0 additions & 24 deletions explorer/cmd/explorer.go

This file was deleted.

76 changes: 35 additions & 41 deletions explorer/cmd/gex.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions explorer/pkg/gex/gex.go → explorer/gex/gex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion explorer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
"os"
"fmt"

hplugin "github.com/hashicorp/go-plugin"
"github.com/ignite/cli/v28/ignite/services/plugin"
Expand All @@ -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 {
Expand Down

0 comments on commit 8221992

Please sign in to comment.