Skip to content

Commit

Permalink
fix: init app client API chainer only when chain info is available (#…
Browse files Browse the repository at this point in the history
…3963)

* fix: init app client API chainer only when chain info is available

* chore: update changelog

---------

Co-authored-by: Ehsan-saradar <[email protected]>
(cherry picked from commit ee46153)
  • Loading branch information
jeronimoalbi authored and mergify[bot] committed Feb 14, 2024
1 parent 1beb7b1 commit 2b73c6b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- [#3953](https://github.com/ignite/cli/pull/3953) Fix apps `Stdout` is redirected to `Stderr`
- [#3863](https://github.com/ignite/cli/pull/3963) Fix breaking issue for app client API when reading app chain info

## [`v28.2.0`](https://github.com/ignite/cli/releases/tag/v28.2.0)

Expand Down
45 changes: 28 additions & 17 deletions ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,14 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)
}
}

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

ctx := cmd.Context()
execHook := newExecutedHook(hook, cmd, args)
err = p.Interface.ExecuteHookPre(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))
err = p.Interface.ExecuteHookPre(ctx, execHook, api)
if err != nil {
return errors.Errorf("app %q ExecuteHookPre() error: %w", p.Path, err)
}
Expand All @@ -224,15 +223,14 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)
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 {
// Get chain when the plugin runs inside an blockchain app
c, err := newChainWithHomeFlags(cmd)
if err != nil && !errors.Is(err, gomodule.ErrGoModNotFound) {
api, err := newAppClientAPI(cmd)
if err != nil {

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

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin.go#L226-L227

Added lines #L226 - L227 were not covered by tests
return err
}

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

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

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin.go#L233

Added line #L233 was not covered by tests
if err != nil {
cmd.Printf("app %q ExecuteHookCleanUp() error: %v", p.Path, err)
}
Expand All @@ -246,17 +244,16 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)

postCmd := cmd.PostRunE
cmd.PostRunE = func(cmd *cobra.Command, args []string) error {
// Get chain when the plugin runs inside an blockchain app
c, err := newChainWithHomeFlags(cmd)
if err != nil && !errors.Is(err, gomodule.ErrGoModNotFound) {
api, err := newAppClientAPI(cmd)
if err != nil {
return err
}

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

defer func() {
err := p.Interface.ExecuteHookCleanUp(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))
err := p.Interface.ExecuteHookCleanUp(ctx, execHook, api)
if err != nil {
cmd.Printf("app %q ExecuteHookCleanUp() error: %v", p.Path, err)
}
Expand All @@ -270,7 +267,7 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)
}
}

err = p.Interface.ExecuteHookPost(ctx, execHook, plugin.NewClientAPI(plugin.WithChain(c)))
err = p.Interface.ExecuteHookPost(ctx, execHook, api)
if err != nil {
return errors.Errorf("app %q ExecuteHookPost() error : %w", p.Path, err)
}
Expand Down Expand Up @@ -333,9 +330,8 @@ func linkPluginCmd(rootCmd *cobra.Command, p *plugin.Plugin, pluginCmd *plugin.C
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) {
api, err := newAppClientAPI(cmd)
if err != nil {
return err
}

Expand All @@ -348,7 +344,7 @@ func linkPluginCmd(rootCmd *cobra.Command, p *plugin.Plugin, pluginCmd *plugin.C
With: p.With,
}
execCmd.ImportFlags(cmd)
err = p.Interface.Execute(ctx, execCmd, plugin.NewClientAPI(plugin.WithChain(c)))
err = p.Interface.Execute(ctx, execCmd, api)

// NOTE(tb): This pause gives enough time for go-plugin to sync the
// output from stdout/stderr of the plugin. Without that pause, this
Expand Down Expand Up @@ -696,6 +692,21 @@ func printPlugins(ctx context.Context, session *cliui.Session) error {
return nil
}

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

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

View check run for this annotation

Codecov / codecov/patch

ignite/cmd/plugin.go#L699-L700

Added lines #L699 - L700 were not covered by tests

var options []plugin.APIOption
if c != nil {
options = append(options, plugin.WithChain(c))
}

return plugin.NewClientAPI(options...), nil
}

func flagSetPluginsGlobal() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.BoolP(flagPluginsGlobal, "g", false, "use global plugins configuration ($HOME/.ignite/apps/igniteapps.yml)")
Expand Down

0 comments on commit 2b73c6b

Please sign in to comment.