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

fix: change ignite apps to be able to run in any directory #3827

Merged
merged 6 commits into from
Dec 12, 2023
Merged
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- [#3827](https://github.com/ignite/cli/pull/3827) Change ignite apps to be able to run in any directory
- [#3831](https://github.com/ignite/cli/pull/3831) Correct ignite app gRPC server stop memory issue
- [#3825](https://github.com/ignite/cli/pull/3825) Fix a minor Keplr type-checking bug in TS client
- [#3836](https://github.com/ignite/cli/pull/3836) Add missing IBC commands for scaffolded chain
Expand Down
50 changes: 29 additions & 21 deletions ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
"github.com/ignite/cli/v28/ignite/pkg/cosmosanalysis"
"github.com/ignite/cli/v28/ignite/pkg/gomodule"
"github.com/ignite/cli/v28/ignite/pkg/xgit"
"github.com/ignite/cli/v28/ignite/services/plugin"
)
Expand Down Expand Up @@ -197,21 +198,22 @@

preRun := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

if preRun != nil {
err := preRun(cmd, args)
if err != nil {
return err
}
}

execHook := newExecutedHook(hook, cmd, args)
// Get chain when the plugin runs inside an blockchain app
c, err := newChainWithHomeFlags(cmd)
if err != nil {
if err != nil && !errors.Is(err, gomodule.ErrGoModNotFound) {
return err
}
err = p.Interface.ExecuteHookPre(ctx, execHook, plugin.NewClientAPI(c))

ctx := cmd.Context()
execHook := newExecutedHook(hook, cmd, args)
err = p.Interface.ExecuteHookPre(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))
if err != nil {
return fmt.Errorf("app %q ExecuteHookPre() error: %w", p.Path, err)
}
Expand All @@ -225,13 +227,15 @@
err := runCmd(cmd, args)
// if the command has failed the `PostRun` will not execute. here we execute the cleanup step before returnning.
if err != nil {
ctx := cmd.Context()
execHook := newExecutedHook(hook, cmd, args)
// Get chain when the plugin runs inside an blockchain app

Check warning on line 230 in ignite/cmd/plugin.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin.go#L230

Added line #L230 was not covered by tests
c, err := newChainWithHomeFlags(cmd)
if err != nil {
if err != nil && !errors.Is(err, gomodule.ErrGoModNotFound) {

Check warning on line 232 in ignite/cmd/plugin.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin.go#L232

Added line #L232 was not covered by tests
return err
}
err = p.Interface.ExecuteHookCleanUp(ctx, execHook, plugin.NewClientAPI(c))

ctx := cmd.Context()
execHook := newExecutedHook(hook, cmd, args)
err = p.Interface.ExecuteHookCleanUp(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))

Check warning on line 238 in ignite/cmd/plugin.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin.go#L236-L238

Added lines #L236 - L238 were not covered by tests
if err != nil {
cmd.Printf("app %q ExecuteHookCleanUp() error: %v", p.Path, err)
}
Expand All @@ -245,16 +249,17 @@

postCmd := cmd.PostRunE
cmd.PostRunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
execHook := newExecutedHook(hook, cmd, args)

// Get chain when the plugin runs inside an blockchain app
c, err := newChainWithHomeFlags(cmd)
if err != nil {
if err != nil && !errors.Is(err, gomodule.ErrGoModNotFound) {
return err
}

ctx := cmd.Context()
execHook := newExecutedHook(hook, cmd, args)

defer func() {
err := p.Interface.ExecuteHookCleanUp(ctx, execHook, plugin.NewClientAPI(c))
err := p.Interface.ExecuteHookCleanUp(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))
if err != nil {
cmd.Printf("app %q ExecuteHookCleanUp() error: %v", p.Path, err)
}
Expand All @@ -268,7 +273,7 @@
}
}

err = p.Interface.ExecuteHookPost(ctx, execHook, plugin.NewClientAPI(c))
err = p.Interface.ExecuteHookPost(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))
if err != nil {
return fmt.Errorf("app %q ExecuteHookPost() error : %w", p.Path, err)
}
Expand Down Expand Up @@ -331,6 +336,13 @@
newCmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
return clictx.Do(ctx, func() error {
// Get chain when the plugin runs inside an blockchain app
c, err := newChainWithHomeFlags(cmd)
if err != nil && !errors.Is(err, gomodule.ErrGoModNotFound) {
return err
}

Check warning on line 343 in ignite/cmd/plugin.go

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin.go#L342-L343

Added lines #L342 - L343 were not covered by tests

// Call the plugin Execute
execCmd := &plugin.ExecutedCommand{
Use: cmd.Use,
Path: cmd.CommandPath(),
Expand All @@ -339,12 +351,8 @@
With: p.With,
}
execCmd.ImportFlags(cmd)
// Call the plugin Execute
c, err := newChainWithHomeFlags(cmd)
if err != nil {
return err
}
err = p.Interface.Execute(ctx, execCmd, plugin.NewClientAPI(c))
err = p.Interface.Execute(ctx, execCmd, plugin.NewClientAPI(plugin.WithChain(c)))

// NOTE(tb): This pause gives enough time for go-plugin to sync the
// output from stdout/stderr of the plugin. Without that pause, this
// output can be discarded and not printed in the user console.
Expand Down
48 changes: 41 additions & 7 deletions ignite/services/plugin/client_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import (
"context"
"errors"
)

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

type Chainer interface {
// AppPath returns the configured App's path.
AppPath() string
Expand All @@ -18,30 +22,60 @@
RPCPublicAddress() (string, error)
}

// APIOption defines options for the client API.
type APIOption func(*apiOptions)

type apiOptions struct {
chain Chainer
}

// WithChain configures the chain to use for the client API.
func WithChain(c Chainer) APIOption {
return func(o *apiOptions) {
o.chain = c
}
}

// NewClientAPI creates a new app ClientAPI.
func NewClientAPI(c Chainer) ClientAPI {
return clientAPI{chain: c}
func NewClientAPI(options ...APIOption) ClientAPI {
o := apiOptions{}
for _, apply := range options {
apply(&o)
}
return clientAPI{o}
}

type clientAPI struct {
chain Chainer
o apiOptions
}

func (api clientAPI) GetChainInfo(context.Context) (*ChainInfo, error) {
chainID, err := api.chain.ID()
chain, err := api.getChain()
if err != nil {
return nil, err
}

Check warning on line 56 in ignite/services/plugin/client_api.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/plugin/client_api.go#L53-L56

Added lines #L53 - L56 were not covered by tests

chainID, err := chain.ID()

Check warning on line 58 in ignite/services/plugin/client_api.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/plugin/client_api.go#L58

Added line #L58 was not covered by tests
if err != nil {
return nil, err
}

rpc, err := api.chain.RPCPublicAddress()
rpc, err := chain.RPCPublicAddress()

Check warning on line 63 in ignite/services/plugin/client_api.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/plugin/client_api.go#L63

Added line #L63 was not covered by tests
if err != nil {
return nil, err
}

return &ChainInfo{
ChainId: chainID,
AppPath: api.chain.AppPath(),
ConfigPath: api.chain.ConfigPath(),
AppPath: chain.AppPath(),
ConfigPath: chain.ConfigPath(),

Check warning on line 71 in ignite/services/plugin/client_api.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/plugin/client_api.go#L70-L71

Added lines #L70 - L71 were not covered by tests
RpcAddress: rpc,
}, nil
}

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

Check warning on line 80 in ignite/services/plugin/client_api.go

View check run for this annotation

Codecov / codecov/patch

ignite/services/plugin/client_api.go#L76-L80

Added lines #L76 - L80 were not covered by tests
}
Loading