From 102a34eea2b68b4cec351a460088b92670286b31 Mon Sep 17 00:00:00 2001 From: Giuseppe De Palma Date: Thu, 4 Apr 2024 17:49:47 +0200 Subject: [PATCH] feat: pass params to invoke as k=v --- main.go | 28 ++++++++++++++++++++++++++++ main_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/main.go b/main.go index 9d2d8e4..62bcc6c 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,8 @@ import ( "os" "os/exec" "path/filepath" + "slices" + "strings" "time" "github.com/mitchellh/go-homedir" @@ -265,6 +267,13 @@ func main() { trace("wsk wrapper command") debug("extracted cmd", cmd) rest := args[2:] + debug("extracted args", rest) + + // if "invoke" is in the command, parse all a=b into -p a b + if (len(cmd) > 2 && cmd[2] == "invoke") || slices.Contains(rest, "invoke") { + rest = parseInvokeArgs(rest) + } + if err := tools.Wsk(cmd, rest...); err != nil { log.Fatalf("error: %s", err.Error()) } @@ -283,6 +292,25 @@ func main() { } } +// parse all a=b into -p a b +func parseInvokeArgs(rest []string) []string { + trace("parsing invoke args") + args := []string{} + + for _, arg := range rest { + if strings.Contains(arg, "=") { + kv := strings.Split(arg, "=") + p := []string{"-p", kv[0], kv[1]} + args = append(args, p...) + } else { + args = append(args, arg) + } + } + + debug("parsed invoke args", args) + return args +} + // getRootDirOrExit returns the olaris dir or exits (Fatal) if not found func getRootDirOrExit() string { dir, err := getNuvRoot() diff --git a/main_test.go b/main_test.go index c94860d..519dc3e 100644 --- a/main_test.go +++ b/main_test.go @@ -24,6 +24,7 @@ import ( "testing" "github.com/mitchellh/go-homedir" + "github.com/stretchr/testify/require" ) /// test utils @@ -78,3 +79,34 @@ func TestSetupNuvRootPlugin(t *testing.T) { t.Errorf("NUV_ROOT_PLUGIN not set correctly, expected /path/to/nuv/root but got %s", os.Getenv("NUV_ROOT_PLUGIN")) } } + +func TestParseInvokeArgs(t *testing.T) { + t.Run("Test case 1: No arguments with \"=\"", func(t *testing.T) { + input1 := []string{} + expected1 := []string{} + output1 := parseInvokeArgs(input1) + require.Equal(t, expected1, output1) + }) + + t.Run("Test case 2: Single argument with \"=\"", func(t *testing.T) { + input2 := []string{"key=value"} + expected2 := []string{"-p", "key", "value"} + output2 := parseInvokeArgs(input2) + require.Equal(t, expected2, output2) + }) + + t.Run("Test case 3: Multiple arguments with \"=\"", func(t *testing.T) { + + input3 := []string{"key1=value1", "key2=value2", "key3=value3"} + expected3 := []string{"-p", "key1", "value1", "-p", "key2", "value2", "-p", "key3", "value3"} + output3 := parseInvokeArgs(input3) + require.Equal(t, expected3, output3) + }) + + t.Run("Test case 4: Mixed arguments with \"=\" and without \"=\"", func(t *testing.T) { + input4 := []string{"key1=value1", "-p", "key2", "value2", "key3=value3"} + expected4 := []string{"-p", "key1", "value1", "-p", "key2", "value2", "-p", "key3", "value3"} + output4 := parseInvokeArgs(input4) + require.Equal(t, expected4, output4) + }) +}