Skip to content

Commit

Permalink
feat: add flags to the extension hooks commands (#4270)
Browse files Browse the repository at this point in the history
* add flags to the hook commands

* add flags to executed hook cmd

* create hook.ImportFlags method

* add changelog

* Update changelog.md

(cherry picked from commit 54e7f52)

# Conflicts:
#	ignite/services/plugin/grpc/v1/interface.pb.go
  • Loading branch information
Pantani authored and mergify[bot] committed Jul 25, 2024
1 parent 0cb8d9d commit 49ac599
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 55 deletions.
17 changes: 17 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
## Unreleased

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

- [#3707](https://github.com/ignite/cli/pull/3707) and [#4094](https://github.com/ignite/cli/pull/4094) Add collections support.
- [#3977](https://github.com/ignite/cli/pull/3977) Add `chain lint` command to lint the chain's codebase using `golangci-lint`
- [#3770](https://github.com/ignite/cli/pull/3770) Add `scaffold configs` and `scaffold params` commands
- [#4001](https://github.com/ignite/cli/pull/4001) Improve `xgenny` dry run
- [#3967](https://github.com/ignite/cli/issues/3967) Add HD wallet parameters `address index` and `account number` to the chain account config
- [#4004](https://github.com/ignite/cli/pull/4004) Remove all import placeholders using the `xast` pkg
- [#4071](https://github.com/ignite/cli/pull/4071) Support custom proto path
- [#3718](https://github.com/ignite/cli/pull/3718) Add `gen-mig-diffs` tool app to compare scaffold output of two versions of ignite
- [#4100](https://github.com/ignite/cli/pull/4100) Set the `proto-dir` flag only for the `scaffold chain` command and use the proto path from the config
- [#4111](https://github.com/ignite/cli/pull/4111) Remove vuex generation
- [#4113](https://github.com/ignite/cli/pull/4113) Generate chain config documentation automatically
- [#4131](https://github.com/ignite/cli/pull/4131) Support `bytes` as data type in the `scaffold` commands
- [#4270](https://github.com/ignite/cli/pull/4270) Add flags to the extension hooks commands
- [#4269](https://github.com/ignite/cli/pull/4269) Add custom flag parser for extensions
- [#4276](https://github.com/ignite/cli/pull/4276) Add `cosmosclient.CreateTxWithOptions` method to facilite more custom tx creation

### Changes

Expand Down
19 changes: 17 additions & 2 deletions ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)
}

newExecutedHook := func(hook *plugin.Hook, cmd *cobra.Command, args []string) *plugin.ExecutedHook {
hook.ImportFlags(cmd)
execHook := &plugin.ExecutedHook{
Hook: hook,
ExecutedCommand: &plugin.ExecutedCommand{
Expand All @@ -188,12 +189,27 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)
Args: args,
OsArgs: os.Args,
With: p.With,
Flags: hook.Flags,
},
}
execHook.ExecutedCommand.ImportFlags(cmd)
return execHook
}

for _, f := range hook.Flags {
var fs *flag.FlagSet
if f.Persistent {
fs = cmd.PersistentFlags()
} else {
fs = cmd.Flags()
}

if err := f.ExportToFlagSet(fs); err != nil {
p.Error = errors.Errorf("can't attach hook flags %q to command %q", hook.Flags, hook.PlaceHookOn)
return
}
}

preRun := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if preRun != nil {
Expand All @@ -218,11 +234,10 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook *plugin.Hook)
}

runCmd := cmd.RunE

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if runCmd != nil {
err := runCmd(cmd, args)
// if the command has failed the `PostRun` will not execute. here we execute the cleanup step before returnning.
// if the command has failed the `PostRun` will not execute. here we execute the cleanup step before returning.
if err != nil {
api, err := newAppClientAPI(cmd)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions ignite/services/plugin/grpc/v1/client_api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 45 additions & 23 deletions ignite/services/plugin/grpc/v1/interface.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions ignite/services/plugin/grpc/v1/interface_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Command) ToCobraCommand() (*cobra.Command, error) {
fs = cmd.Flags()
}

if err := f.exportToFlagSet(fs); err != nil {
if err := f.ExportToFlagSet(fs); err != nil {
return nil, err
}
}
Expand All @@ -54,7 +54,7 @@ func (c *ExecutedCommand) NewFlags() (*pflag.FlagSet, error) {
continue
}

if err := f.exportToFlagSet(fs); err != nil {
if err := f.ExportToFlagSet(fs); err != nil {
return nil, err
}
}
Expand All @@ -71,7 +71,7 @@ func (c *ExecutedCommand) NewPersistentFlags() (*pflag.FlagSet, error) {
continue
}

if err := f.exportToFlagSet(fs); err != nil {
if err := f.ExportToFlagSet(fs); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/services/plugin/grpc/v1/interface_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newDefaultFlagValueError(typeName, value string) error {
return errors.Errorf("invalid default value for plugin command %s flag: %s", typeName, value)
}

func (f *Flag) exportToFlagSet(fs *pflag.FlagSet) error {
func (f *Flag) ExportToFlagSet(fs *pflag.FlagSet) error {
switch f.Type { //nolint:exhaustive
case Flag_TYPE_FLAG_BOOL,
Flag_TYPE_FLAG_INT,
Expand Down
7 changes: 7 additions & 0 deletions ignite/services/plugin/grpc/v1/interface_hook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package v1

import "github.com/spf13/cobra"

// CommandPath returns the absolute command path including the binary name as prefix.
func (h *Hook) CommandPath() string {
return ensureFullCommandPath(h.PlaceHookOn)
}

// ImportFlags imports flags from a Cobra command.
func (h *Hook) ImportFlags(cmd *cobra.Command) {
h.Flags = extractCobraFlags(cmd)
}
Loading

0 comments on commit 49ac599

Please sign in to comment.