Skip to content

Commit

Permalink
feature plugin custom flag (#251)
Browse files Browse the repository at this point in the history
* support plugin custom flags

* add short name

* remove log

* remove log
  • Loading branch information
songmeizi authored Dec 9, 2020
1 parent 67804a6 commit 84ab11a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tools/goctl/goctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
1 change: 0 additions & 1 deletion tools/goctl/plugin/demo/goctlplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
22 changes: 20 additions & 2 deletions tools/goctl/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
29 changes: 29 additions & 0 deletions tools/goctl/plugin/plugin_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 84ab11a

Please sign in to comment.