Skip to content

Commit a5ec6c2

Browse files
committed
cli/command: remove StringSliceReplaceAt utility
It was only used internally in cmd/docker and has no known external consumers. Move it to cmd/docker and un-export it. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 2eec746 commit a5ec6c2

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
@@ -240,37 +240,6 @@ func ValidateOutputPathFileMode(fileMode os.FileMode) error {
240240
return nil
241241
}
242242

243-
func stringSliceIndex(s, subs []string) int {
244-
j := 0
245-
if len(subs) > 0 {
246-
for i, x := range s {
247-
if j < len(subs) && subs[j] == x {
248-
j++
249-
} else {
250-
j = 0
251-
}
252-
if len(subs) == j {
253-
return i + 1 - j
254-
}
255-
}
256-
}
257-
return -1
258-
}
259-
260-
// StringSliceReplaceAt replaces the sub-slice find, with the sub-slice replace, in the string
261-
// slice s, returning a new slice and a boolean indicating if the replacement happened.
262-
// requireIdx is the index at which old needs to be found at (or -1 to disregard that).
263-
func StringSliceReplaceAt(s, find, replace []string, requireIndex int) ([]string, bool) {
264-
idx := stringSliceIndex(s, find)
265-
if (requireIndex != -1 && requireIndex != idx) || idx == -1 {
266-
return s, false
267-
}
268-
out := append([]string{}, s[:idx]...)
269-
out = append(out, replace...)
270-
out = append(out, s[idx+len(find):]...)
271-
return out, true
272-
}
273-
274243
// ValidateMountWithAPIVersion validates a mount with the server API version.
275244
func ValidateMountWithAPIVersion(m mounttypes.Mount, serverAPIVersion string) error {
276245
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)