-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove Cobra dependency from explorer app (#38)
* refactor: remove Cobra dependency from explorer app * chore: move commands definitions to a public function
- Loading branch information
1 parent
3a9c9c3
commit 8221992
Showing
6 changed files
with
74 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters