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: add flags to the extension hooks commands #4270

Merged
merged 8 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -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 extestion hooks commands
Pantani marked this conversation as resolved.
Show resolved Hide resolved
- [#4269](https://github.com/ignite/cli/pull/4269) Add custom flag parser for extensions

### 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)
Pantani marked this conversation as resolved.
Show resolved Hide resolved
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
Loading