Skip to content

Commit

Permalink
do not parse flags separately from args in plugin cmd (#846)
Browse files Browse the repository at this point in the history
* do not parse flags separately from args in plugin cmd

* remove print from plugin cmd test

* goimports-ed with -local
  • Loading branch information
suz-stripe committed Mar 31, 2022
1 parent 0638c6a commit 88ac6a3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pkg/cmd/plugin_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
)

type pluginTemplateCmd struct {
cfg *config.Config
cmd *cobra.Command
fs afero.Fs
cfg *config.Config
cmd *cobra.Command
fs afero.Fs
ParsedArgs []string
}

// newPluginTemplateCmd is a generic plugin command template to dynamically use
Expand All @@ -27,10 +28,11 @@ func newPluginTemplateCmd(config *config.Config, plugin *plugins.Plugin) *plugin
ptc.cfg = config

ptc.cmd = &cobra.Command{
Use: plugin.Shortname,
Short: plugin.Shortdesc,
RunE: ptc.runPluginCmd,
Annotations: map[string]string{"scope": "plugin"},
Use: plugin.Shortname,
Short: plugin.Shortdesc,
RunE: ptc.runPluginCmd,
Annotations: map[string]string{"scope": "plugin"},
DisableFlagParsing: true,
}

// override the CLI's help command and let the plugin supply the help text instead
Expand All @@ -50,6 +52,7 @@ func (ptc *pluginTemplateCmd) runPluginCmd(cmd *cobra.Command, args []string) er
}).Debug("Ctrl+C received, cleaning up...")
})

ptc.ParsedArgs = args
fs := afero.NewOsFs()
plugin, err := plugins.LookUpPlugin(ctx, ptc.cfg, fs, cmd.CalledAs())

Expand Down
41 changes: 41 additions & 0 deletions pkg/cmd/plugin_cmds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/stripe/stripe-cli/pkg/plugins"
)

func createPluginCmd() *pluginTemplateCmd {
plugin := plugins.Plugin{
Shortname: "test",
Shortdesc: "test your stuff",
Binary: "stripe-cli-test",
MagicCookieValue: "magic",
Releases: []plugins.Release{{
Arch: "amd64",
OS: "darwin",
Version: "0.0.1",
Sum: "c53a98c3fa63563227eb8b5601acedb5e0e70fed2e1d52a5918a17ac755f17f7",
}},
}

pluginCmd := newPluginTemplateCmd(&Config, &plugin)

return pluginCmd
}

func TestFlagsArePassedAsArgs(t *testing.T) {
Execute(context.Background())

pluginCmd := createPluginCmd()
rootCmd.AddCommand(pluginCmd.cmd)
executeCommandC(rootCmd, "test", "testarg", "--testflag")

require.Equal(t, len(pluginCmd.ParsedArgs), 2)
require.Equal(t, strings.Join(pluginCmd.ParsedArgs, " "), "testarg --testflag")
}

0 comments on commit 88ac6a3

Please sign in to comment.