Skip to content

Commit 87f462a

Browse files
authored
Merge pull request #5943 from koba1t/chore/drop_shlex_dependency
drop shlex dependency
2 parents f9ab532 + 3866a30 commit 87f462a

File tree

63 files changed

+247
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+247
-95
lines changed

api/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.22.7
44

55
require (
66
github.com/blang/semver/v4 v4.0.0
7-
github.com/carapace-sh/carapace-shlex v1.0.1
87
github.com/go-errors/errors v1.4.2
98
github.com/stretchr/testify v1.10.0
109
go.uber.org/goleak v1.3.0

api/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
22
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
3-
github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c=
4-
github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
53
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
64
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
75
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

api/internal/plugins/execplugin/execplugin.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"runtime"
1313
"strings"
1414

15-
shlex "github.com/carapace-sh/carapace-shlex"
16-
1715
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
1816
"sigs.k8s.io/kustomize/api/resmap"
1917
"sigs.k8s.io/kustomize/kyaml/errors"
@@ -95,11 +93,11 @@ func (p *ExecPlugin) processOptionalArgsFields() error {
9593
return err
9694
}
9795
if c.ArgsOneLiner != "" {
98-
argsTolenSlice, err := shlex.Split(c.ArgsOneLiner)
96+
argsTolenSlice, err := ShlexSplit(c.ArgsOneLiner)
9997
if err != nil {
10098
return fmt.Errorf("failed to parse argsOneLiner: %w", err)
10199
}
102-
p.args = argsTolenSlice.Strings()
100+
p.args = argsTolenSlice
103101
}
104102
if c.ArgsFromFile != "" {
105103
content, err := p.h.Loader().Load(c.ArgsFromFile)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2019 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package execplugin
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
"unicode"
10+
)
11+
12+
// ShlexSplit splits a string into a slice of strings using shell-style rules for quoting and commenting
13+
// Similar to Python's shlex.split with comments enabled
14+
func ShlexSplit(s string) ([]string, error) {
15+
return shlexSplit(s)
16+
}
17+
18+
func shlexSplit(s string) ([]string, error) {
19+
result := []string{}
20+
21+
// noQuote is used to track if we are not in a quoted
22+
const noQuote = 0
23+
24+
var current strings.Builder
25+
var quote rune = noQuote
26+
var escaped bool
27+
28+
for _, r := range s {
29+
switch {
30+
case escaped:
31+
current.WriteRune(r)
32+
escaped = false
33+
case r == '\\' && quote != '\'':
34+
escaped = true
35+
case (r == '\'' || r == '"') && quote == noQuote:
36+
quote = r
37+
case r == quote:
38+
quote = noQuote
39+
case r == '#' && quote == noQuote:
40+
// Comment starts, ignore the rest of the line
41+
if current.Len() > 0 {
42+
result = append(result, current.String())
43+
}
44+
return result, nil
45+
case unicode.IsSpace(r) && quote == noQuote:
46+
if current.Len() > 0 {
47+
result = append(result, current.String())
48+
current.Reset()
49+
}
50+
default:
51+
current.WriteRune(r)
52+
}
53+
}
54+
55+
if quote != noQuote {
56+
return nil, fmt.Errorf("unclosed quote in string")
57+
}
58+
if current.Len() > 0 {
59+
result = append(result, current.String())
60+
}
61+
return result, nil
62+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright 2019 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package execplugin
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestShlexSplit(t *testing.T) {
13+
testCases := []struct {
14+
name string
15+
input string
16+
expected []string
17+
wantErr bool
18+
}{
19+
{
20+
name: "basic space separation",
21+
input: `hello world`,
22+
expected: []string{"hello", "world"},
23+
wantErr: false,
24+
},
25+
{
26+
name: "double quoted string",
27+
input: `"hello world"`,
28+
expected: []string{"hello world"},
29+
wantErr: false,
30+
},
31+
{
32+
name: "single quoted string",
33+
input: `'hello world'`,
34+
expected: []string{"hello world"},
35+
wantErr: false,
36+
},
37+
{
38+
name: "mixed quotes and words",
39+
input: `hello "world test"`,
40+
expected: []string{"hello", "world test"},
41+
wantErr: false,
42+
},
43+
{
44+
name: "single quotes with spaces",
45+
input: `hello 'world test'`,
46+
expected: []string{"hello", "world test"},
47+
wantErr: false,
48+
},
49+
{
50+
name: "nested quotes - single in double",
51+
input: `"hello 'nested' world"`,
52+
expected: []string{"hello 'nested' world"},
53+
wantErr: false,
54+
},
55+
{
56+
name: "nested quotes - double in single",
57+
input: `'hello "nested" world'`,
58+
expected: []string{"hello \"nested\" world"},
59+
wantErr: false,
60+
},
61+
{
62+
name: "escaped space",
63+
input: `hello\ world`,
64+
expected: []string{"hello world"},
65+
wantErr: false,
66+
},
67+
{
68+
name: "escaped quotes in double quotes",
69+
input: `"hello \"world\""`,
70+
expected: []string{"hello \"world\""},
71+
wantErr: false,
72+
},
73+
{
74+
name: "single quote in single quotes",
75+
input: `'can'\''t'`,
76+
expected: []string{"can't"},
77+
wantErr: false,
78+
},
79+
{
80+
name: "complex argument list",
81+
input: `arg1 "arg 2" 'arg 3' arg4`,
82+
expected: []string{"arg1", "arg 2", "arg 3", "arg4"},
83+
wantErr: false,
84+
},
85+
{
86+
name: "echo command with escaped quotes",
87+
input: `echo "Hello, \"World!\""`,
88+
expected: []string{"echo", "Hello, \"World!\""},
89+
wantErr: false,
90+
},
91+
{
92+
name: "grep command with quoted search term",
93+
input: `grep -r "search term" /path/to/dir`,
94+
expected: []string{"grep", "-r", "search term", "/path/to/dir"},
95+
wantErr: false,
96+
},
97+
{
98+
name: "ls command with quoted filename",
99+
input: `ls -la "file with spaces.txt"`,
100+
expected: []string{"ls", "-la", "file with spaces.txt"},
101+
wantErr: false,
102+
},
103+
{
104+
name: "empty string",
105+
input: ``,
106+
expected: []string{},
107+
wantErr: false,
108+
},
109+
{
110+
name: "multiple spaces",
111+
input: ` multiple spaces `,
112+
expected: []string{"multiple", "spaces"},
113+
wantErr: false,
114+
},
115+
{
116+
name: "with comment string",
117+
input: `echo "Hello, W#orld!" ${USER} # This is a comment`,
118+
expected: []string{"echo", "Hello, W#orld!", "${USER}"},
119+
wantErr: false,
120+
},
121+
{
122+
name: "comment only",
123+
input: `# this line is just a comment`,
124+
expected: []string{},
125+
wantErr: false,
126+
},
127+
// may cause an error in shlex at python3
128+
{
129+
name: "unclosed double quote",
130+
input: `"unclosed quote`,
131+
expected: nil,
132+
wantErr: true,
133+
},
134+
{
135+
name: "unclosed single quote",
136+
input: `'unclosed quote`,
137+
expected: nil,
138+
wantErr: true,
139+
},
140+
{
141+
name: "mixed unclosed quotes",
142+
input: `"mixed 'quotes`,
143+
expected: nil,
144+
wantErr: true,
145+
},
146+
{
147+
name: "single quote closed with double quote",
148+
input: `"hello world'`,
149+
expected: nil,
150+
wantErr: true,
151+
},
152+
{
153+
name: "double quote closed with single quote",
154+
input: `'hello world"`,
155+
expected: nil,
156+
wantErr: true,
157+
},
158+
}
159+
160+
// execute each test case
161+
for _, tc := range testCases {
162+
t.Run(tc.name, func(t *testing.T) {
163+
// call the ShlexSplit function
164+
result, err := ShlexSplit(tc.input)
165+
166+
// check for expected error
167+
if tc.wantErr {
168+
if err == nil {
169+
t.Errorf("FAIL: Expected error but got none, Expected: %q\n", tc.expected)
170+
}
171+
return
172+
}
173+
174+
if assert.NoError(t, err, "FAIL: Unexpected error %q for input %q", err, tc.input) {
175+
// check if the result matches the expected output
176+
assert.Equal(t, tc.expected, result,
177+
"FAIL: Result mismatch,Input %q, Expected %q, Got: %q\n",
178+
tc.input, tc.expected, result,
179+
)
180+
}
181+
})
182+
}
183+
}

cmd/pluginator/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
22
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
3-
github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c=
4-
github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
53
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
64
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
75
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

kustomize/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ require (
1616

1717
require (
1818
github.com/blang/semver/v4 v4.0.0 // indirect
19-
github.com/carapace-sh/carapace-shlex v1.0.1 // indirect
2019
github.com/davecgh/go-spew v1.1.1 // indirect
2120
github.com/go-errors/errors v1.4.2 // indirect
2221
github.com/go-openapi/jsonpointer v0.21.0 // indirect

kustomize/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
22
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
3-
github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c=
4-
github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
53
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
64
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
75
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

plugin/builtin/annotationstransformer/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99

1010
require (
1111
github.com/blang/semver/v4 v4.0.0 // indirect
12-
github.com/carapace-sh/carapace-shlex v1.0.1 // indirect
1312
github.com/davecgh/go-spew v1.1.1 // indirect
1413
github.com/go-errors/errors v1.4.2 // indirect
1514
github.com/go-openapi/jsonpointer v0.21.0 // indirect

plugin/builtin/annotationstransformer/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
22
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
3-
github.com/carapace-sh/carapace-shlex v1.0.1 h1:ww0JCgWpOVuqWG7k3724pJ18Lq8gh5pHQs9j3ojUs1c=
4-
github.com/carapace-sh/carapace-shlex v1.0.1/go.mod h1:lJ4ZsdxytE0wHJ8Ta9S7Qq0XpjgjU0mdfCqiI2FHx7M=
53
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
64
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
75
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

0 commit comments

Comments
 (0)