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(plugin): support command's flags #3060

Merged
merged 50 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8250a82
wip
tbruyelle Oct 21, 2022
1a41498
wip: implements gob decoder/encoder
tbruyelle Oct 21, 2022
cc74492
flags data are passing the network
tbruyelle Oct 23, 2022
a5a6f76
hide flags field and expose Flags() func like cobra.Command
tbruyelle Oct 23, 2022
67b2283
Remove cobra.Command field from plugin.Command
tbruyelle Oct 23, 2022
777c977
comments
tbruyelle Oct 31, 2022
6e17c21
add int64 flag type
tbruyelle Nov 2, 2022
857f800
test: add flags
tbruyelle Nov 3, 2022
4cf7476
test: plugin use current ignite/cli
tbruyelle Nov 3, 2022
b63cb88
chore: upgrade plugin CLI dep
tbruyelle Nov 4, 2022
ef0bb8b
fix: InitAndCommit doesn't create repo if already exists
tbruyelle Nov 4, 2022
3bdaa04
chore: improve plugin default code
tbruyelle Nov 4, 2022
b772233
add CL entry
tbruyelle Nov 4, 2022
701eb6a
chore: update plugin CLI dep
tbruyelle Nov 4, 2022
5cbb62b
Merge branch 'develop' into feat/plugin-cmd-flags
Nov 4, 2022
dce5612
Merge branch 'develop' into feat/plugin-cmd-flags
Nov 4, 2022
2e514d2
fix after merge
tbruyelle Nov 4, 2022
71a1417
dont remove band now
tbruyelle Nov 4, 2022
51647ac
Merge branch 'develop' into feat/plugin-cmd-flags
tbruyelle Nov 7, 2022
9753111
Merge branch 'develop' into feat/plugin-cmd-flags
tbruyelle Nov 8, 2022
ef6e010
remove replace in plugin gomod and use latest version
tbruyelle Nov 8, 2022
f2752e3
fix: plugin.Hooks can return an error
tbruyelle Nov 8, 2022
752e05e
update plugin dep version
tbruyelle Nov 8, 2022
47de29a
add spinner during plugin scaffolding
tbruyelle Nov 8, 2022
dc9a434
fix: populate hook.with with plugin config
tbruyelle Nov 8, 2022
479cdd3
test: execute commands on linkPluginCmds
tbruyelle Nov 8, 2022
5b3e2a5
test: assert args in TestLinkPluginCmds
tbruyelle Nov 8, 2022
a0bc403
test: global args in TestLinkPluginHooks
tbruyelle Nov 8, 2022
6e6bbd2
refac(plugin): merge Commands and Hooks into Manifest
tbruyelle Nov 8, 2022
822c656
fix lint error
tbruyelle Nov 8, 2022
77c66ba
improve plugin error handling
tbruyelle Nov 9, 2022
6e45a55
feat(plugin): support more flag types
tbruyelle Nov 9, 2022
ffdc2d0
update plugin cli version and other fixes
tbruyelle Nov 9, 2022
ad31cca
Merge branch 'develop' into feat/plugin-cmd-flags
tbruyelle Nov 9, 2022
c193512
test: try to fix
tbruyelle Nov 9, 2022
d138126
docs: comment
tbruyelle Nov 9, 2022
22bf4d0
docs: more comments
tbruyelle Nov 9, 2022
fb458c8
docs: more comments
tbruyelle Nov 9, 2022
7345a0c
docs: comments
tbruyelle Nov 9, 2022
0d1380c
remove slack private lnk
tbruyelle Nov 9, 2022
9d36ee7
Merge branch 'develop' into feat/plugin-cmd-flags
tbruyelle Nov 9, 2022
b2d4662
remove private slack link
tbruyelle Nov 9, 2022
651e399
run make format
tbruyelle Nov 10, 2022
6bf925d
docs: add code comments
tbruyelle Nov 10, 2022
f3ff026
Merge branch 'develop' into feat/plugin-cmd-flags
tbruyelle Nov 10, 2022
869bf71
fix: ExecuteCommand embedding
tbruyelle Nov 10, 2022
1dfc3de
Merge branch 'develop' into feat/plugin-cmd-flags
tbruyelle Nov 10, 2022
af6e9dd
Merge branch 'develop' into feat/plugin-cmd-flags
Nov 15, 2022
50d1f34
Merge branch 'main' into feat/plugin-cmd-flags
Nov 15, 2022
e9b6346
Merge branch 'main' into feat/plugin-cmd-flags
Nov 15, 2022
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 @@ -11,6 +11,7 @@
- [#2892](https://github.com/ignite/cli/pull/2982/) Add `ignite generate hooks` command.
- [#2955](https://github.com/ignite/cli/pull/2955/) Add `ignite network request add-account` command.
- [#2877](https://github.com/ignite/cli/pull/2877) Plugin system
- [#3060](https://github.com/ignite/cli/pull/3060) Plugin system flag support
- [#2995](https://github.com/ignite/cli/pull/2995/) Add `ignite network request remove-validator` command.
- [#2999](https://github.com/ignite/cli/pull/2999/) Add `ignite network request remove-account` command.

Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func chainBuildHandler(cmd *cobra.Command, _ []string) error {
chainOption = append(chainOption, chain.CheckDependencies())
}

c, err := NewChainWithHomeFlags(cmd, chainOption...)
c, err := newChainWithHomeFlags(cmd, chainOption...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func chainFaucetHandler(cmd *cobra.Command, args []string) error {
chain.CollectEvents(session.EventBus()),
}

c, err := NewChainWithHomeFlags(cmd, chainOption...)
c, err := newChainWithHomeFlags(cmd, chainOption...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func chainInitHandler(cmd *cobra.Command, _ []string) error {
chainOption = append(chainOption, chain.CheckDependencies())
}

c, err := NewChainWithHomeFlags(cmd, chainOption...)
c, err := newChainWithHomeFlags(cmd, chainOption...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/chain_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func chainServeHandler(cmd *cobra.Command, args []string) error {
}

// create the chain
c, err := NewChainWithHomeFlags(cmd, chainOption...)
c, err := newChainWithHomeFlags(cmd, chainOption...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func flagGetClearCache(cmd *cobra.Command) bool {
return clearCache
}

func NewChainWithHomeFlags(cmd *cobra.Command, chainOption ...chain.Option) (*chain.Chain, error) {
func newChainWithHomeFlags(cmd *cobra.Command, chainOption ...chain.Option) (*chain.Chain, error) {
// Check if custom home is provided
if home := getHome(cmd); home != "" {
chainOption = append(chainOption, chain.HomePath(home))
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_composables.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func generateComposablesHandler(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusGenerating))
defer session.End()

c, err := NewChainWithHomeFlags(
c, err := newChainWithHomeFlags(
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func generateGoHandler(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusGenerating))
defer session.End()

c, err := NewChainWithHomeFlags(
c, err := newChainWithHomeFlags(
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func generateHooksHandler(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusGenerating))
defer session.End()

c, err := NewChainWithHomeFlags(
c, err := newChainWithHomeFlags(
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func generateOpenAPIHandler(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusGenerating))
defer session.End()

c, err := NewChainWithHomeFlags(
c, err := newChainWithHomeFlags(
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_typescript_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func generateTSClientHandler(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusGenerating))
defer session.End()

c, err := NewChainWithHomeFlags(
c, err := newChainWithHomeFlags(
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_vuex.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func generateVuexHandler(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusGenerating))
defer session.End()

c, err := NewChainWithHomeFlags(
c, err := newChainWithHomeFlags(
cmd,
chain.WithOutputer(session),
chain.CollectEvents(session.EventBus()),
Expand Down
107 changes: 83 additions & 24 deletions ignite/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/ignite/cli/ignite/pkg/cliui"
"github.com/ignite/cli/ignite/pkg/cliui/entrywriter"
"github.com/ignite/cli/ignite/pkg/xgit"
"github.com/ignite/cli/ignite/services/plugin"
Expand All @@ -27,7 +28,7 @@ const (
// If no configuration found, it returns w/o error.
func LoadPlugins(ctx context.Context, rootCmd *cobra.Command) error {
// NOTE(tb) Not sure if it's the right place to load this.
chain, err := NewChainWithHomeFlags(rootCmd)
chain, err := newChainWithHomeFlags(rootCmd)
if err != nil {
// Binary is run outside of an chain app, plugins can't be loaded
return nil
Expand All @@ -36,16 +37,31 @@ func LoadPlugins(ctx context.Context, rootCmd *cobra.Command) error {
if err != nil {
return err
}
return loadPlugins(rootCmd, plugins)
}

func loadPlugins(rootCmd *cobra.Command, plugins []*plugin.Plugin) error {
// Link plugins to related commands
var loadErrors []string
for _, p := range plugins {
linkPluginHooks(rootCmd, p)
if p.Error != nil {
loadErrors = append(loadErrors, p.Path)
continue
}
manifest, err := p.Interface.Manifest()
if err != nil {
p.Error = fmt.Errorf("Manifest() error: %w", err)
continue
}
linkPluginHooks(rootCmd, p, manifest.Hooks)
if p.Error != nil {
loadErrors = append(loadErrors, p.Path)
continue
}
linkPluginCmds(rootCmd, p)
linkPluginCmds(rootCmd, p, manifest.Commands)
if p.Error != nil {
loadErrors = append(loadErrors, p.Path)
continue
}
}
if len(loadErrors) > 0 {
Expand All @@ -65,12 +81,11 @@ func UnloadPlugins() {
}
}

func linkPluginHooks(rootCmd *cobra.Command, p *plugin.Plugin) {
func linkPluginHooks(rootCmd *cobra.Command, p *plugin.Plugin, hooks []plugin.Hook) {
if p.Error != nil {
return
}

for _, hook := range p.Interface.Hooks() {
for _, hook := range hooks {
linkPluginHook(rootCmd, p, hook)
}
}
Expand Down Expand Up @@ -98,6 +113,20 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook plugin.Hook)
return
}

newExecutedHook := func(hook plugin.Hook, cmd *cobra.Command, args []string) plugin.ExecutedHook {
execHook := plugin.ExecutedHook{
Hook: hook,
ExecutedCommand: plugin.ExecutedCommand{
Use: cmd.Use,
Path: cmd.CommandPath(),
Args: args,
With: p.With,
},
}
execHook.SetFlags(cmd.Flags())
return execHook
}

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

return p.Interface.ExecuteHookPre(hook, args)
err := p.Interface.ExecuteHookPre(newExecutedHook(hook, cmd, args))
if err != nil {
return fmt.Errorf("plugin %q ExecuteHookPre() error: %w", p.Path, err)
}
return nil
}

runCmd := cmd.RunE
Expand All @@ -117,9 +149,11 @@ 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 {
p.Interface.ExecuteHookCleanUp(hook, args)
err := p.Interface.ExecuteHookCleanUp(newExecutedHook(hook, cmd, args))
if err != nil {
fmt.Printf("plugin %q ExecuteHookCleanUp() error: %v", p.Path, err)
aljo242 marked this conversation as resolved.
Show resolved Hide resolved
}
}

return err
}

Expand All @@ -129,7 +163,14 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook plugin.Hook)

postCmd := cmd.PostRunE
cmd.PostRunE = func(cmd *cobra.Command, args []string) error {
defer p.Interface.ExecuteHookCleanUp(hook, args)
execHook := newExecutedHook(hook, cmd, args)

defer func() {
err := p.Interface.ExecuteHookCleanUp(execHook)
if err != nil {
fmt.Printf("plugin %q ExecuteHookCleanUp() error: %v", p.Path, err)
aljo242 marked this conversation as resolved.
Show resolved Hide resolved
}
}()

if preRun != nil {
err := postCmd(cmd, args)
Expand All @@ -139,17 +180,21 @@ func linkPluginHook(rootCmd *cobra.Command, p *plugin.Plugin, hook plugin.Hook)
}
}

return p.Interface.ExecuteHookPost(hook, args)
err := p.Interface.ExecuteHookPost(execHook)
if err != nil {
return fmt.Errorf("plugin %q ExecuteHookPost() error : %w", p.Path, err)
}
return nil
}
}

// linkPluginCmds tries to add the plugin commands to the legacy ignite
// commands.
func linkPluginCmds(rootCmd *cobra.Command, p *plugin.Plugin) {
func linkPluginCmds(rootCmd *cobra.Command, p *plugin.Plugin, pluginCmds []plugin.Command) {
if p.Error != nil {
return
}
for _, pluginCmd := range p.Interface.Commands() {
for _, pluginCmd := range pluginCmds {
linkPluginCmd(rootCmd, p, pluginCmd)
if p.Error != nil {
return
Expand Down Expand Up @@ -186,21 +231,34 @@ func linkPluginCmd(rootCmd *cobra.Command, p *plugin.Plugin, pluginCmd plugin.Co
Short: pluginCmd.Short,
Long: pluginCmd.Long,
}
for _, f := range pluginCmd.Flags {
err := f.FeedFlagSet(newCmd.Flags())
if err != nil {
p.Error = err
return
}
}
cmd.AddCommand(newCmd)
if len(pluginCmd.Commands) == 0 {
// pluginCmd has no sub commands, so it's runnable
newCmd.RunE = func(cmd *cobra.Command, args []string) error {
// Pass config parameters
pluginCmd.With = p.With
// Pass cobra cmd
pluginCmd.CobraCmd = cmd
execCmd := plugin.ExecutedCommand{
Use: cmd.Use,
Path: cmd.CommandPath(),
Args: args,
With: p.With,
}
execCmd.SetFlags(cmd.Flags())
// Call the plugin Execute
err := p.Interface.Execute(pluginCmd, args)
err := p.Interface.Execute(execCmd)
// 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.
time.Sleep(100 * time.Millisecond)
return err
if err != nil {
return fmt.Errorf("plugin %q Execute() error : %w", p.Path, err)
}
return nil
}
} else {
for _, pluginCmd := range pluginCmd.Commands {
Expand Down Expand Up @@ -286,6 +344,9 @@ func NewPluginScaffold() *cobra.Command {
Short: "Scaffold a new plugin",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
session := cliui.New(cliui.StartSpinnerWithText(statusScaffolding))
defer session.End()

wd, err := os.Getwd()
if err != nil {
return err
Expand All @@ -307,11 +368,9 @@ func NewPluginScaffold() *cobra.Command {
plugins:
- path: %[2]s

👉 once the plugin is pushed to a repository, the config becomes:
plugins:
- path: %[1]s
👉 once the plugin is pushed to a repository, replace the local path by the repository path.
`
fmt.Printf(message, moduleName, path)
session.Printf(message, moduleName, path)
return nil
},
}
Expand Down
Loading