Skip to content

Commit

Permalink
feat: pass params to invoke as k=v
Browse files Browse the repository at this point in the history
  • Loading branch information
giusdp committed Apr 4, 2024
1 parent 3cf407a commit 102a34e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"time"

"github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -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())
}
Expand All @@ -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()
Expand Down
32 changes: 32 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/mitchellh/go-homedir"
"github.com/stretchr/testify/require"
)

/// test utils
Expand Down Expand Up @@ -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)
})
}

0 comments on commit 102a34e

Please sign in to comment.