feature plugin custom flag (#251)

* support plugin custom flags

* add short name

* remove log

* remove log
master
songmeizi 4 years ago committed by GitHub
parent 67804a6bb2
commit 84ab11ac09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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{

@ -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.")
}

@ -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
}

@ -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)
}
Loading…
Cancel
Save