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>
This commit is contained in:
parent
2eec74659e
commit
a5ec6c2963
@ -240,37 +240,6 @@ func ValidateOutputPathFileMode(fileMode os.FileMode) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringSliceIndex(s, subs []string) int {
|
|
||||||
j := 0
|
|
||||||
if len(subs) > 0 {
|
|
||||||
for i, x := range s {
|
|
||||||
if j < len(subs) && subs[j] == x {
|
|
||||||
j++
|
|
||||||
} else {
|
|
||||||
j = 0
|
|
||||||
}
|
|
||||||
if len(subs) == j {
|
|
||||||
return i + 1 - j
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringSliceReplaceAt replaces the sub-slice find, with the sub-slice replace, in the string
|
|
||||||
// slice s, returning a new slice and a boolean indicating if the replacement happened.
|
|
||||||
// requireIdx is the index at which old needs to be found at (or -1 to disregard that).
|
|
||||||
func StringSliceReplaceAt(s, find, replace []string, requireIndex int) ([]string, bool) {
|
|
||||||
idx := stringSliceIndex(s, find)
|
|
||||||
if (requireIndex != -1 && requireIndex != idx) || idx == -1 {
|
|
||||||
return s, false
|
|
||||||
}
|
|
||||||
out := append([]string{}, s[:idx]...)
|
|
||||||
out = append(out, replace...)
|
|
||||||
out = append(out, s[idx+len(find):]...)
|
|
||||||
return out, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateMountWithAPIVersion validates a mount with the server API version.
|
// ValidateMountWithAPIVersion validates a mount with the server API version.
|
||||||
func ValidateMountWithAPIVersion(m mounttypes.Mount, serverAPIVersion string) error {
|
func ValidateMountWithAPIVersion(m mounttypes.Mount, serverAPIVersion string) error {
|
||||||
if m.BindOptions != nil {
|
if m.BindOptions != nil {
|
||||||
|
@ -21,32 +21,6 @@ import (
|
|||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStringSliceReplaceAt(t *testing.T) {
|
|
||||||
out, ok := command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, -1)
|
|
||||||
assert.Assert(t, ok)
|
|
||||||
assert.DeepEqual(t, []string{"abc", "baz", "bax"}, out)
|
|
||||||
|
|
||||||
out, ok = command.StringSliceReplaceAt([]string{"foo"}, []string{"foo", "bar"}, []string{"baz"}, -1)
|
|
||||||
assert.Assert(t, !ok)
|
|
||||||
assert.DeepEqual(t, []string{"foo"}, out)
|
|
||||||
|
|
||||||
out, ok = command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
|
|
||||||
assert.Assert(t, !ok)
|
|
||||||
assert.DeepEqual(t, []string{"abc", "foo", "bar", "bax"}, out)
|
|
||||||
|
|
||||||
out, ok = command.StringSliceReplaceAt([]string{"foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
|
|
||||||
assert.Assert(t, ok)
|
|
||||||
assert.DeepEqual(t, []string{"baz", "bax"}, out)
|
|
||||||
|
|
||||||
out, ok = command.StringSliceReplaceAt([]string{"abc", "foo", "bar", "baz"}, []string{"foo", "bar"}, nil, -1)
|
|
||||||
assert.Assert(t, ok)
|
|
||||||
assert.DeepEqual(t, []string{"abc", "baz"}, out)
|
|
||||||
|
|
||||||
out, ok = command.StringSliceReplaceAt([]string{"foo"}, nil, []string{"baz"}, -1)
|
|
||||||
assert.Assert(t, !ok)
|
|
||||||
assert.DeepEqual(t, []string{"foo"}, out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidateOutputPath(t *testing.T) {
|
func TestValidateOutputPath(t *testing.T) {
|
||||||
basedir := t.TempDir()
|
basedir := t.TempDir()
|
||||||
dir := filepath.Join(basedir, "dir")
|
dir := filepath.Join(basedir, "dir")
|
||||||
|
@ -43,9 +43,9 @@ func processAliases(dockerCli command.Cli, cmd *cobra.Command, args, osArgs []st
|
|||||||
|
|
||||||
for _, al := range aliases {
|
for _, al := range aliases {
|
||||||
var didChange bool
|
var didChange bool
|
||||||
args, didChange = command.StringSliceReplaceAt(args, al[0], al[1], 0)
|
args, didChange = stringSliceReplaceAt(args, al[0], al[1], 0)
|
||||||
if didChange {
|
if didChange {
|
||||||
osArgs, _ = command.StringSliceReplaceAt(osArgs, al[0], al[1], -1)
|
osArgs, _ = stringSliceReplaceAt(osArgs, al[0], al[1], -1)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
32
cmd/docker/aliases_utils.go
Normal file
32
cmd/docker/aliases_utils.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func stringSliceIndex(s, subs []string) int {
|
||||||
|
j := 0
|
||||||
|
if len(subs) > 0 {
|
||||||
|
for i, x := range s {
|
||||||
|
if j < len(subs) && subs[j] == x {
|
||||||
|
j++
|
||||||
|
} else {
|
||||||
|
j = 0
|
||||||
|
}
|
||||||
|
if len(subs) == j {
|
||||||
|
return i + 1 - j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// stringSliceReplaceAt replaces the sub-slice find, with the sub-slice replace, in the string
|
||||||
|
// slice s, returning a new slice and a boolean indicating if the replacement happened.
|
||||||
|
// requireIdx is the index at which old needs to be found at (or -1 to disregard that).
|
||||||
|
func stringSliceReplaceAt(s, find, replace []string, requireIndex int) ([]string, bool) {
|
||||||
|
idx := stringSliceIndex(s, find)
|
||||||
|
if (requireIndex != -1 && requireIndex != idx) || idx == -1 {
|
||||||
|
return s, false
|
||||||
|
}
|
||||||
|
out := append([]string{}, s[:idx]...)
|
||||||
|
out = append(out, replace...)
|
||||||
|
out = append(out, s[idx+len(find):]...)
|
||||||
|
return out, true
|
||||||
|
}
|
33
cmd/docker/aliases_utils_test.go
Normal file
33
cmd/docker/aliases_utils_test.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStringSliceReplaceAt(t *testing.T) {
|
||||||
|
out, ok := stringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, -1)
|
||||||
|
assert.Assert(t, ok)
|
||||||
|
assert.DeepEqual(t, []string{"abc", "baz", "bax"}, out)
|
||||||
|
|
||||||
|
out, ok = stringSliceReplaceAt([]string{"foo"}, []string{"foo", "bar"}, []string{"baz"}, -1)
|
||||||
|
assert.Assert(t, !ok)
|
||||||
|
assert.DeepEqual(t, []string{"foo"}, out)
|
||||||
|
|
||||||
|
out, ok = stringSliceReplaceAt([]string{"abc", "foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
|
||||||
|
assert.Assert(t, !ok)
|
||||||
|
assert.DeepEqual(t, []string{"abc", "foo", "bar", "bax"}, out)
|
||||||
|
|
||||||
|
out, ok = stringSliceReplaceAt([]string{"foo", "bar", "bax"}, []string{"foo", "bar"}, []string{"baz"}, 0)
|
||||||
|
assert.Assert(t, ok)
|
||||||
|
assert.DeepEqual(t, []string{"baz", "bax"}, out)
|
||||||
|
|
||||||
|
out, ok = stringSliceReplaceAt([]string{"abc", "foo", "bar", "baz"}, []string{"foo", "bar"}, nil, -1)
|
||||||
|
assert.Assert(t, ok)
|
||||||
|
assert.DeepEqual(t, []string{"abc", "baz"}, out)
|
||||||
|
|
||||||
|
out, ok = stringSliceReplaceAt([]string{"foo"}, nil, []string{"baz"}, -1)
|
||||||
|
assert.Assert(t, !ok)
|
||||||
|
assert.DeepEqual(t, []string{"foo"}, out)
|
||||||
|
}
|
@ -152,8 +152,8 @@ func forwardBuilder(alias string, args, osargs []string) ([]string, []string, []
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, al := range aliases {
|
for _, al := range aliases {
|
||||||
if fwargs, changed := command.StringSliceReplaceAt(args, al[0], al[1], 0); changed {
|
if fwargs, changed := stringSliceReplaceAt(args, al[0], al[1], 0); changed {
|
||||||
fwosargs, _ := command.StringSliceReplaceAt(osargs, al[0], al[1], -1)
|
fwosargs, _ := stringSliceReplaceAt(osargs, al[0], al[1], -1)
|
||||||
fwcmdpath := al[2]
|
fwcmdpath := al[2]
|
||||||
return fwargs, fwosargs, fwcmdpath, true
|
return fwargs, fwosargs, fwcmdpath, true
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user