Skip to content

Commit 650b45a

Browse files
authored
Merge pull request #5915 from thaJeztah/remove_StringSliceReplaceAt
cli/command: remove StringSliceReplaceAt utility
2 parents bc57a03 + a5ec6c2 commit 650b45a

6 files changed

Lines changed: 69 additions & 61 deletions

File tree

cli/command/utils.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -218,37 +218,6 @@ func ValidateOutputPathFileMode(fileMode os.FileMode) error {
218218
return nil
219219
}
220220

221-
func stringSliceIndex(s, subs []string) int {
222-
j := 0
223-
if len(subs) > 0 {
224-
for i, x := range s {
225-
if j < len(subs) && subs[j] == x {
226-
j++
227-
} else {
228-
j = 0
229-
}
230-
if len(subs) == j {
231-
return i + 1 - j
232-
}
233-
}
234-
}
235-
return -1
236-
}
237-
238-
// StringSliceReplaceAt replaces the sub-slice find, with the sub-slice replace, in the string
239-
// slice s, returning a new slice and a boolean indicating if the replacement happened.
240-
// requireIdx is the index at which old needs to be found at (or -1 to disregard that).
241-
func StringSliceReplaceAt(s, find, replace []string, requireIndex int) ([]string, bool) {
242-
idx := stringSliceIndex(s, find)
243-
if (requireIndex != -1 && requireIndex != idx) || idx == -1 {
244-
return s, false
245-
}
246-
out := append([]string{}, s[:idx]...)
247-
out = append(out, replace...)
248-
out = append(out, s[idx+len(find):]...)
249-
return out, true
250-
}
251-
252221
// ValidateMountWithAPIVersion validates a mount with the server API version.
253222
func ValidateMountWithAPIVersion(m mounttypes.Mount, serverAPIVersion string) error {
254223
if m.BindOptions != nil {

cli/command/utils_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,6 @@ import (
2121
"gotest.tools/v3/assert"
2222
)
2323

24-
func TestStringSliceReplaceAt(t *testing.T) {
25-
out, ok := command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, -1)
26-
assert.Assert(t, ok)
27-
assert.DeepEqual(t, []string{"abc", "baz", "bax"}, out)
28-
29-
out, ok = command.StringSliceReplaceAt([]string{"foo"}, []string{"foo", "bar"}, []string{"baz"}, -1)
30-
assert.Assert(t, !ok)
31-
assert.DeepEqual(t, []string{"foo"}, out)
32-
33-
out, ok = command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
34-
assert.Assert(t, !ok)
35-
assert.DeepEqual(t, []string{"abc", "foo", "bar", "bax"}, out)
36-
37-
out, ok = command.StringSliceReplaceAt([]string{"foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
38-
assert.Assert(t, ok)
39-
assert.DeepEqual(t, []string{"baz", "bax"}, out)
40-
41-
out, ok = command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "baz"}, []string{"foo", "bar"}, nil, -1)
42-
assert.Assert(t, ok)
43-
assert.DeepEqual(t, []string{"abc", "baz"}, out)
44-
45-
out, ok = command.StringSliceReplaceAt([]string{"foo"}, nil, []string{"baz"}, -1)
46-
assert.Assert(t, !ok)
47-
assert.DeepEqual(t, []string{"foo"}, out)
48-
}
49-
5024
func TestValidateOutputPath(t *testing.T) {
5125
basedir := t.TempDir()
5226
dir := filepath.Join(basedir, "dir")

cmd/docker/aliases.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func processAliases(dockerCli command.Cli, cmd *cobra.Command, args, osArgs []st
4343

4444
for _, al := range aliases {
4545
var didChange bool
46-
args, didChange = command.StringSliceReplaceAt(args, al[0], al[1], 0)
46+
args, didChange = stringSliceReplaceAt(args, al[0], al[1], 0)
4747
if didChange {
48-
osArgs, _ = command.StringSliceReplaceAt(osArgs, al[0], al[1], -1)
48+
osArgs, _ = stringSliceReplaceAt(osArgs, al[0], al[1], -1)
4949
break
5050
}
5151
}

cmd/docker/aliases_utils.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
func stringSliceIndex(s, subs []string) int {
4+
j := 0
5+
if len(subs) > 0 {
6+
for i, x := range s {
7+
if j < len(subs) && subs[j] == x {
8+
j++
9+
} else {
10+
j = 0
11+
}
12+
if len(subs) == j {
13+
return i + 1 - j
14+
}
15+
}
16+
}
17+
return -1
18+
}
19+
20+
// stringSliceReplaceAt replaces the sub-slice find, with the sub-slice replace, in the string
21+
// slice s, returning a new slice and a boolean indicating if the replacement happened.
22+
// requireIdx is the index at which old needs to be found at (or -1 to disregard that).
23+
func stringSliceReplaceAt(s, find, replace []string, requireIndex int) ([]string, bool) {
24+
idx := stringSliceIndex(s, find)
25+
if (requireIndex != -1 && requireIndex != idx) || idx == -1 {
26+
return s, false
27+
}
28+
out := append([]string{}, s[:idx]...)
29+
out = append(out, replace...)
30+
out = append(out, s[idx+len(find):]...)
31+
return out, true
32+
}

cmd/docker/aliases_utils_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestStringSliceReplaceAt(t *testing.T) {
10+
out, ok := stringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, -1)
11+
assert.Assert(t, ok)
12+
assert.DeepEqual(t, []string{"abc", "baz", "bax"}, out)
13+
14+
out, ok = stringSliceReplaceAt([]string{"foo"}, []string{"foo", "bar"}, []string{"baz"}, -1)
15+
assert.Assert(t, !ok)
16+
assert.DeepEqual(t, []string{"foo"}, out)
17+
18+
out, ok = stringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
19+
assert.Assert(t, !ok)
20+
assert.DeepEqual(t, []string{"abc", "foo", "bar", "bax"}, out)
21+
22+
out, ok = stringSliceReplaceAt([]string{"foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
23+
assert.Assert(t, ok)
24+
assert.DeepEqual(t, []string{"baz", "bax"}, out)
25+
26+
out, ok = stringSliceReplaceAt([]string{"abc", "foo", "bar", "baz"}, []string{"foo", "bar"}, nil, -1)
27+
assert.Assert(t, ok)
28+
assert.DeepEqual(t, []string{"abc", "baz"}, out)
29+
30+
out, ok = stringSliceReplaceAt([]string{"foo"}, nil, []string{"baz"}, -1)
31+
assert.Assert(t, !ok)
32+
assert.DeepEqual(t, []string{"foo"}, out)
33+
}

cmd/docker/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ func forwardBuilder(alias string, args, osargs []string) ([]string, []string, []
152152
},
153153
}
154154
for _, al := range aliases {
155-
if fwargs, changed := command.StringSliceReplaceAt(args, al[0], al[1], 0); changed {
156-
fwosargs, _ := command.StringSliceReplaceAt(osargs, al[0], al[1], -1)
155+
if fwargs, changed := stringSliceReplaceAt(args, al[0], al[1], 0); changed {
156+
fwosargs, _ := stringSliceReplaceAt(osargs, al[0], al[1], -1)
157157
fwcmdpath := al[2]
158158
return fwargs, fwosargs, fwcmdpath, true
159159
}

0 commit comments

Comments
 (0)