Skip to content

Commit adb4e86

Browse files
Copilotericcurtin
andcommitted
Create pkg/utils package with SplitArgs function
Co-authored-by: ericcurtin <[email protected]>
1 parent d110242 commit adb4e86

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

main.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/docker/model-runner/pkg/inference/scheduling"
2222
"github.com/docker/model-runner/pkg/metrics"
2323
"github.com/docker/model-runner/pkg/routing"
24-
"github.com/mattn/go-shellwords"
24+
"github.com/docker/model-runner/pkg/utils"
2525
"github.com/sirupsen/logrus"
2626
)
2727

@@ -255,10 +255,7 @@ func createLlamaCppConfigFromEnv() config.BackendConfig {
255255
}
256256

257257
// Split the string by spaces, respecting quoted arguments
258-
args, err := shellwords.Parse(argsStr)
259-
if err != nil {
260-
log.Fatalf("Failed to parse LLAMA_ARGS: %v", err)
261-
}
258+
args := utils.SplitArgs(argsStr)
262259

263260
// Check for disallowed arguments
264261
disallowedArgs := []string{"--model", "--host", "--embeddings", "--mmproj"}

pkg/utils/args.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package utils
2+
3+
import (
4+
"github.com/mattn/go-shellwords"
5+
)
6+
7+
// SplitArgs splits a string into arguments, respecting quoted strings.
8+
// This is a wrapper around shellwords.Parse for convenience.
9+
func SplitArgs(s string) []string {
10+
args, err := shellwords.Parse(s)
11+
if err != nil {
12+
// If parsing fails, return empty slice
13+
// The caller can check for empty result
14+
return []string{}
15+
}
16+
return args
17+
}

pkg/utils/args_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package utils
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestSplitArgs(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input string
12+
expected []string
13+
}{
14+
{
15+
name: "simple arguments",
16+
input: "arg1 arg2 arg3",
17+
expected: []string{"arg1", "arg2", "arg3"},
18+
},
19+
{
20+
name: "quoted arguments",
21+
input: `arg1 "arg with spaces" arg3`,
22+
expected: []string{"arg1", "arg with spaces", "arg3"},
23+
},
24+
{
25+
name: "single quoted arguments",
26+
input: `arg1 'arg with spaces' arg3`,
27+
expected: []string{"arg1", "arg with spaces", "arg3"},
28+
},
29+
{
30+
name: "mixed quotes",
31+
input: `arg1 "double quoted" 'single quoted' arg4`,
32+
expected: []string{"arg1", "double quoted", "single quoted", "arg4"},
33+
},
34+
{
35+
name: "empty string",
36+
input: "",
37+
expected: []string{},
38+
},
39+
{
40+
name: "flags with values",
41+
input: "--flag1 value1 --flag2 value2",
42+
expected: []string{"--flag1", "value1", "--flag2", "value2"},
43+
},
44+
{
45+
name: "flags with quoted values",
46+
input: `--flag1 "value with spaces" --flag2 value2`,
47+
expected: []string{"--flag1", "value with spaces", "--flag2", "value2"},
48+
},
49+
}
50+
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
result := SplitArgs(tt.input)
54+
if !reflect.DeepEqual(result, tt.expected) {
55+
t.Errorf("SplitArgs(%q) = %v, expected %v", tt.input, result, tt.expected)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)