Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext): add the ability to call ignite commands from the extension #4252

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ func newAppClientAPI(cmd *cobra.Command) (plugin.ClientAPI, error) {
return nil, err
}

var options []plugin.APIOption
options := []plugin.APIOption{plugin.WithCmd(cmd)}
if c != nil {
options = append(options, plugin.WithChain(c))
}
Expand Down
21 changes: 17 additions & 4 deletions ignite/internal/plugin/consumer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plugininternal
package plugininternal_test

import (
"context"
Expand All @@ -8,11 +8,17 @@ import (

"github.com/stretchr/testify/require"

ignitecmd "github.com/ignite/cli/v29/ignite/cmd"
plugininternal "github.com/ignite/cli/v29/ignite/internal/plugin"
"github.com/ignite/cli/v29/ignite/services/plugin"
"github.com/ignite/cli/v29/ignite/services/plugin/mocks"
)

func TestConsumerPlugin(t *testing.T) {
cmd, cleanup, err := ignitecmd.New(context.Background())
require.NoError(t, err)
t.Cleanup(cleanup)

tests := []struct {
name string
args []string
Expand Down Expand Up @@ -97,7 +103,13 @@ func TestConsumerPlugin(t *testing.T) {
chainer.EXPECT().ConfigPath().Return("configpath").Maybe()
chainer.EXPECT().Home().Return(path, nil).Maybe()
chainer.EXPECT().RPCPublicAddress().Return("rpcPublicAddress", nil).Maybe()
_, err = Execute(context.Background(), PluginConsumerPath, []string{"writeGenesis"}, plugin.WithChain(chainer))
_, err = plugininternal.Execute(
context.Background(),
plugininternal.PluginConsumerPath,
[]string{"writeGenesis"},
plugin.WithChain(chainer),
plugin.WithCmd(cmd),
)
require.NoError(t, err)
},
expectedOutput: "true",
Expand All @@ -118,11 +130,12 @@ func TestConsumerPlugin(t *testing.T) {
tt.setup(t, homePath)
}

out, err := Execute(
out, err := plugininternal.Execute(
context.Background(),
PluginConsumerPath,
plugininternal.PluginConsumerPath,
tt.args,
plugin.WithChain(chainer),
plugin.WithCmd(cmd),
)

if tt.expectedError != "" {
Expand Down
16 changes: 14 additions & 2 deletions ignite/internal/plugin/execute_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plugininternal
package plugininternal_test

import (
"context"
Expand All @@ -9,11 +9,17 @@ import (

"github.com/stretchr/testify/require"

ignitecmd "github.com/ignite/cli/v29/ignite/cmd"
plugininternal "github.com/ignite/cli/v29/ignite/internal/plugin"
"github.com/ignite/cli/v29/ignite/services/plugin"
"github.com/ignite/cli/v29/ignite/services/plugin/mocks"
)

func TestPluginExecute(t *testing.T) {
cmd, cleanup, err := ignitecmd.New(context.Background())
require.NoError(t, err)
t.Cleanup(cleanup)

tests := []struct {
name string
pluginPath string
Expand All @@ -35,6 +41,11 @@ func TestPluginExecute(t *testing.T) {
pluginPath: "testdata/execute_fail",
expectedError: "fail",
},
{
name: "ok: plugin run command execute",
pluginPath: "testdata/run_command",
expectedError: "....",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -52,11 +63,12 @@ func TestPluginExecute(t *testing.T) {
chainer.EXPECT().Home().Return("home", nil).Maybe()
chainer.EXPECT().RPCPublicAddress().Return("rpcPublicAddress", nil).Maybe()

out, err := Execute(
out, err := plugininternal.Execute(
context.Background(),
pluginPath,
[]string{"arg1", "arg2"},
plugin.WithChain(chainer),
plugin.WithCmd(cmd),
)

if tt.expectedError != "" {
Expand Down
14 changes: 7 additions & 7 deletions ignite/internal/plugin/testdata/execute_fail/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.etcd.io/bbolt v1.3.9 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
14 changes: 7 additions & 7 deletions ignite/internal/plugin/testdata/execute_ok/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.etcd.io/bbolt v1.3.9 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions ignite/internal/plugin/testdata/run_command/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_command*
98 changes: 98 additions & 0 deletions ignite/internal/plugin/testdata/run_command/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module execute_ok

go 1.22

toolchain go1.22.3

require (
github.com/hashicorp/go-plugin v1.6.0
github.com/ignite/cli/v29 v29.0.0
)

replace github.com/ignite/cli/v29 => ../../../../..

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/charmbracelet/lipgloss v0.6.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-sdk v0.50.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.12.0 // indirect
github.com/gobuffalo/flect v0.3.0 // indirect
github.com/gobuffalo/genny/v2 v2.1.0 // indirect
github.com/gobuffalo/github_flavored_markdown v1.1.4 // indirect
github.com/gobuffalo/helpers v0.6.7 // indirect
github.com/gobuffalo/logger v1.0.7 // indirect
github.com/gobuffalo/packd v1.0.2 // indirect
github.com/gobuffalo/plush/v4 v4.1.19 // indirect
github.com/gobuffalo/tags/v3 v3.1.4 // indirect
github.com/gobuffalo/validate/v3 v3.3.3 // indirect
github.com/goccy/go-yaml v1.11.3 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-github/v48 v48.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.14.0 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.2 // 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.9 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
43 changes: 43 additions & 0 deletions ignite/internal/plugin/testdata/run_command/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"context"

hplugin "github.com/hashicorp/go-plugin"

"github.com/ignite/cli/v29/ignite/services/plugin"
)

type app struct{}

func (app) Manifest(ctx context.Context) (*plugin.Manifest, error) {
return &plugin.Manifest{
Name: "run_command",
}, nil
}

func (app) Execute(ctx context.Context, cmd *plugin.ExecutedCommand, api plugin.ClientAPI) error {
return api.RunCommand(ctx, "version")
}

func (app) ExecuteHookPre(ctx context.Context, h *plugin.ExecutedHook, api plugin.ClientAPI) error {
return nil
}

func (app) ExecuteHookPost(ctx context.Context, h *plugin.ExecutedHook, api plugin.ClientAPI) error {
return nil
}

func (app) ExecuteHookCleanUp(ctx context.Context, h *plugin.ExecutedHook, api plugin.ClientAPI) error {
return nil
}

func main() {
hplugin.Serve(&hplugin.ServeConfig{
HandshakeConfig: plugin.HandshakeConfig(),
Plugins: map[string]hplugin.Plugin{
"run_command": plugin.NewGRPC(&app{}),
},
GRPCServer: hplugin.DefaultGRPCServer,
})
}
34 changes: 32 additions & 2 deletions ignite/services/plugin/client_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package plugin
import (
"context"

"github.com/spf13/cobra"

"github.com/ignite/cli/v29/ignite/pkg/errors"
)

// ErrAppChainNotFound indicates that the plugin command is not running inside a blockchain app.
var ErrAppChainNotFound = errors.New("blockchain app not found")
var (
// ErrAppChainNotFound indicates that the plugin command is not running inside a blockchain app.
ErrAppChainNotFound = errors.New("blockchain app not found")
// ErrCmdNotFound indicates that the plugin command is not running exist in the blockchain app.
ErrCmdNotFound = errors.New("blockchain CLI commands not found")
)

//go:generate mockery --srcpkg . --name Chainer --structname ChainerInterface --filename chainer.go --with-expecter
type Chainer interface {
Expand All @@ -31,6 +37,7 @@ type Chainer interface {
type APIOption func(*apiOptions)

type apiOptions struct {
cmd *cobra.Command
chain Chainer
}

Expand All @@ -41,6 +48,13 @@ func WithChain(c Chainer) APIOption {
}
}

// WithCmd configures the chain CLI commands to use for the client API.
func WithCmd(cmd *cobra.Command) APIOption {
return func(o *apiOptions) {
o.cmd = cmd
}
}

// NewClientAPI creates a new app ClientAPI.
func NewClientAPI(options ...APIOption) ClientAPI {
o := apiOptions{}
Expand Down Expand Up @@ -84,9 +98,25 @@ func (api clientAPI) GetChainInfo(context.Context) (*ChainInfo, error) {
}, nil
}

func (api clientAPI) RunCommand(ctx context.Context, command ...string) error {
cmd, err := api.getCmd()
if err != nil {
return err
}
cmd.SetArgs(command)
return cmd.ExecuteContext(ctx)
}

func (api clientAPI) getChain() (Chainer, error) {
if api.o.chain == nil {
return nil, ErrAppChainNotFound
}
return api.o.chain, nil
}

func (api clientAPI) getCmd() (*cobra.Command, error) {
if api.o.cmd == nil {
return nil, ErrCmdNotFound
}
return api.o.cmd, nil
}
Loading
Loading