Skip to content

Commit

Permalink
Fix arg passing to plugin processes (#996)
Browse files Browse the repository at this point in the history
* Fix arg passing to plugin processes

* Document helper function

* Add back sigterm cancel
  • Loading branch information
vcheung-stripe committed Nov 3, 2022
1 parent 7e58ee7 commit 3a4972b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
32 changes: 29 additions & 3 deletions pkg/cmd/plugin_cmds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -32,7 +33,9 @@ func newPluginTemplateCmd(config *config.Config, plugin *plugins.Plugin) *plugin
Use: plugin.Shortname,
Short: plugin.Shortdesc,
RunE: func(cmd *cobra.Command, args []string) error {
return ptc.runPluginCmd(cmd, os.Args[2:])
// "stripe [host_flags...] plugin_name [plugin_subcommands...] [plugin_flags...]" => "[plugin_subcommands...] [plugin_flags...]"
pluginArgs := subsliceAfter(os.Args, cmd.Name())
return ptc.runPluginCmd(cmd, pluginArgs)
},
Annotations: map[string]string{"scope": "plugin"},
FParseErrWhitelist: cobra.FParseErrWhitelist{
Expand All @@ -42,7 +45,17 @@ func newPluginTemplateCmd(config *config.Config, plugin *plugins.Plugin) *plugin

// override the CLI's help command and let the plugin supply the help text instead
ptc.cmd.SetHelpFunc(func(c *cobra.Command, s []string) {
ptc.runPluginCmd(c, s[1:])
var args []string
if len(s) == 0 {
// "stripe help plugin_name [plugin_subcommands...]" => "[plugin_subcommands...] --help"
args = subsliceAfter(os.Args, c.Name())
args = append(args, "--help")
c.SetContext(context.Background())
} else {
// "stripe plugin_name [plugin_subcommands...] --help" => "[plugin_subcommands...] --help"
args = subsliceAfter(s, c.Name())
}
ptc.runPluginCmd(c, args)
})

return ptc
Expand All @@ -59,7 +72,7 @@ func (ptc *pluginTemplateCmd) runPluginCmd(cmd *cobra.Command, args []string) er
ptc.ParsedArgs = args

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

if err != nil {
return err
Expand Down Expand Up @@ -88,3 +101,16 @@ func (ptc *pluginTemplateCmd) runPluginCmd(cmd *cobra.Command, args []string) er

return nil
}

// Return a copy of sl strictly after the first occurrence of str, or empty slice if not found.
func subsliceAfter(sl []string, str string) []string {
for i, s := range sl {
if s == str {
subsl := sl[i+1:]
res := make([]string, len(subsl))
copy(res, subsl)
return res
}
}
return make([]string, 0)
}
23 changes: 23 additions & 0 deletions pkg/cmd/plugin_cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stripe/stripe-cli/pkg/plugins"
Expand Down Expand Up @@ -50,3 +51,25 @@ func TestFlagsArePassedAsArgs(t *testing.T) {
require.Equal(t, 2, len(pluginCmd.ParsedArgs))
require.Equal(t, "testarg --log-level=info", strings.Join(pluginCmd.ParsedArgs, " "))
}

func TestSubsliceAfter(t *testing.T) {
tests := []struct {
name string
expected []string
sl []string
str string
}{
{"empty slice", []string{}, []string{}, "foo"},
{"empty string", []string{}, []string{""}, ""},
{"not found", []string{}, []string{"bar"}, "foo"},
{"found at beginning", []string{"bar"}, []string{"foo", "bar"}, "foo"},
{"found at middle", []string{"baz", "qux"}, []string{"foo", "bar", "baz", "qux"}, "bar"},
{"found at end", []string{}, []string{"foo", "bar"}, "bar"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, subsliceAfter(tt.sl, tt.str))
})
}
}

0 comments on commit 3a4972b

Please sign in to comment.