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
  • Loading branch information
Pantani committed Jul 25, 2024
1 parent 6bcbe4e commit 54e7f52
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 60 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [#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

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.

70 changes: 42 additions & 28 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 54e7f52

Please sign in to comment.