pkg/mount: use strings.Cut

In some places, using strings.Cut (available since Go 1.18) instead of
strings.SplitN allows for simpler more readable code.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2024-09-19 16:05:04 -07:00
parent bb27ad1764
commit d074e3c622
3 changed files with 11 additions and 14 deletions

View File

@ -97,14 +97,14 @@ func MergeTmpfsOptions(options []string) ([]string, error) {
}
continue
}
opt := strings.SplitN(option, "=", 2)
if len(opt) != 2 || !validFlags[opt[0]] {
opt, _, ok := strings.Cut(option, "=")
if !ok || !validFlags[opt] {
return nil, fmt.Errorf("invalid tmpfs option %q", opt)
}
if !dataCollisions[opt[0]] {
if !dataCollisions[opt] {
// We prepend the option and add to collision map
newOptions = append([]string{option}, newOptions...)
dataCollisions[opt[0]] = true
dataCollisions[opt] = true
}
}
@ -140,8 +140,8 @@ func ParseOptions(options string) (int, string) {
func ParseTmpfsOptions(options string) (int, string, error) {
flags, data := ParseOptions(options)
for _, o := range strings.Split(data, ",") {
opt := strings.SplitN(o, "=", 2)
if !validFlags[opt[0]] {
opt, _, _ := strings.Cut(o, "=")
if !validFlags[opt] {
return 0, "", fmt.Errorf("invalid tmpfs option %q", opt)
}
}

View File

@ -40,13 +40,9 @@ func mount(device, target, mType string, flag uintptr, data string) error {
isNullFS = true
continue
}
opt := strings.SplitN(x, "=", 2)
options = append(options, opt[0])
if len(opt) == 2 {
options = append(options, opt[1])
} else {
options = append(options, "")
}
name, val, _ := strings.Cut(x, "=")
options = append(options, name)
options = append(options, val)
}
}

View File

@ -209,7 +209,8 @@ func validateMount(t *testing.T, mnt string, opts, optional, vfs string) {
// clean strips off any value param after the colon
func clean(v string) string {
return strings.SplitN(v, ":", 2)[0]
ret, _, _ := strings.Cut(v, ":")
return ret
}
// has returns true if key is a member of m