From 84ab11ac09564f5a88daf93af51b308111e25410 Mon Sep 17 00:00:00 2001 From: songmeizi Date: Wed, 9 Dec 2020 18:08:17 +0800 Subject: [PATCH] feature plugin custom flag (#251) * support plugin custom flags * add short name * remove log * remove log --- tools/goctl/goctl.go | 2 +- tools/goctl/plugin/demo/goctlplugin.go | 1 - tools/goctl/plugin/plugin.go | 22 +++++++++++++++++-- tools/goctl/plugin/plugin_test.go | 29 ++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 tools/goctl/plugin/plugin_test.go diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 66b4690f..86c4b6f7 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -192,7 +192,7 @@ var ( Usage: "custom file generator", Flags: []cli.Flag{ cli.StringFlag{ - Name: "plugin", + Name: "plugin, p", Usage: "the plugin file", }, cli.StringFlag{ diff --git a/tools/goctl/plugin/demo/goctlplugin.go b/tools/goctl/plugin/demo/goctlplugin.go index 65028f4e..387f9842 100644 --- a/tools/goctl/plugin/demo/goctlplugin.go +++ b/tools/goctl/plugin/demo/goctlplugin.go @@ -15,6 +15,5 @@ func main() { if plugin.Api != nil { fmt.Printf("api: %+v \n", plugin.Api) } - fmt.Printf("dir: %s \n", plugin.Dir) fmt.Println("Enjoy anything you want.") } diff --git a/tools/goctl/plugin/plugin.go b/tools/goctl/plugin/plugin.go index 2470ea3e..0168dae4 100644 --- a/tools/goctl/plugin/plugin.go +++ b/tools/goctl/plugin/plugin.go @@ -46,17 +46,20 @@ func PluginCommand(c *cli.Context) error { return err } - bin, download, err := getCommand(plugin) + bin, args := getPluginAndArgs(plugin) + + bin, download, err := getCommand(bin) if err != nil { return err } + if download { defer func() { _ = os.Remove(bin) }() } - content, err := execx.Run(bin, filepath.Dir(ex), bytes.NewBuffer(transferData)) + content, err := execx.Run(bin+" "+args, filepath.Dir(ex), bytes.NewBuffer(transferData)) if err != nil { return err } @@ -164,3 +167,18 @@ func NewPlugin() (*Plugin, error) { } return &plugin, nil } + +func getPluginAndArgs(arg string) (string, string) { + i := strings.Index(arg, "=") + if i <= 0 { + return arg, "" + } + + return trimQuote(arg[:i]), trimQuote(arg[i+1:]) +} +func trimQuote(in string) string { + in = strings.Trim(in, `"`) + in = strings.Trim(in, `'`) + in = strings.Trim(in, "`") + return in +} diff --git a/tools/goctl/plugin/plugin_test.go b/tools/goctl/plugin/plugin_test.go new file mode 100644 index 00000000..d1be1b38 --- /dev/null +++ b/tools/goctl/plugin/plugin_test.go @@ -0,0 +1,29 @@ +package plugin + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetPluginAndArgs(t *testing.T) { + bin, args := getPluginAndArgs("android") + assert.Equal(t, "android", bin) + assert.Equal(t, "", args) + + bin, args = getPluginAndArgs("android=") + assert.Equal(t, "android", bin) + assert.Equal(t, "", args) + + bin, args = getPluginAndArgs("android=-javaPackage com.tal") + assert.Equal(t, "android", bin) + assert.Equal(t, "-javaPackage com.tal", args) + + bin, args = getPluginAndArgs("android=-javaPackage com.tal --lambda") + assert.Equal(t, "android", bin) + assert.Equal(t, "-javaPackage com.tal --lambda", args) + + bin, args = getPluginAndArgs(`https://test-xjy-file.obs.cn-east-2.myhuaweicloud.com/202012/8a7ab6e1-e639-49d1-89cf-2ae6127a1e90n=-v 1`) + assert.Equal(t, "https://test-xjy-file.obs.cn-east-2.myhuaweicloud.com/202012/8a7ab6e1-e639-49d1-89cf-2ae6127a1e90n", bin) + assert.Equal(t, "-v 1", args) +}