Skip to content

Commit 617cda3

Browse files
authored
Merge pull request #142 from thaJeztah/use_strings_cut
update to go1.18, and use strings.Cut
2 parents 7af2bbe + b145b7c commit 617cda3

File tree

10 files changed

+34
-29
lines changed

10 files changed

+34
-29
lines changed

Diff for: .github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.17.x, 1.21.x, 1.22.x]
7+
go-version: [1.18.x, 1.21.x, 1.22.x]
88
platform: [ubuntu-20.04, ubuntu-22.04, ubuntu-24.04, windows-latest, macos-12, macos-14]
99
runs-on: ${{ matrix.platform }}
1010
steps:

Diff for: mount/flags_unix.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ func MergeTmpfsOptions(options []string) ([]string, error) {
100100
}
101101
continue
102102
}
103-
opt := strings.SplitN(option, "=", 2)
104-
if len(opt) != 2 || !validFlags[opt[0]] {
103+
opt, _, ok := strings.Cut(option, "=")
104+
if !ok || !validFlags[opt] {
105105
return nil, fmt.Errorf("invalid tmpfs option %q", opt)
106106
}
107-
if !dataCollisions[opt[0]] {
107+
if !dataCollisions[opt] {
108108
// We prepend the option and add to collision map
109109
newOptions = append([]string{option}, newOptions...)
110-
dataCollisions[opt[0]] = true
110+
dataCollisions[opt] = true
111111
}
112112
}
113113

Diff for: mount/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/moby/sys/mount
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/moby/sys/mountinfo v0.7.2

Diff for: mount/mounter_linux_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ func validateMount(t *testing.T, mnt string, opts, optional, vfs string) {
212212

213213
// clean strips off any value param after the colon
214214
func clean(v string) string {
215-
return strings.SplitN(v, ":", 2)[0]
215+
out, _, _ := strings.Cut(v, ":")
216+
return out
216217
}
217218

218219
// has returns true if key is a member of m

Diff for: mountinfo/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/moby/sys/mountinfo
22

3-
go 1.17
3+
go 1.18
44

55
require golang.org/x/sys v0.1.0

Diff for: mountinfo/mountinfo_linux.go

+21-17
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,20 @@ func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
7575
}
7676
}
7777

78-
p := &Info{}
78+
major, minor, ok := strings.Cut(fields[2], ":")
79+
if !ok {
80+
return nil, fmt.Errorf("parsing '%s' failed: unexpected major:minor pair %s", text, fields[2])
81+
}
82+
83+
p := &Info{
84+
ID: toInt(fields[0]),
85+
Parent: toInt(fields[1]),
86+
Major: toInt(major),
87+
Minor: toInt(minor),
88+
Options: fields[5],
89+
Optional: strings.Join(fields[6:sepIdx], " "), // zero or more optional fields
90+
VFSOptions: fields[sepIdx+3],
91+
}
7992

8093
p.Mountpoint, err = unescape(fields[4])
8194
if err != nil {
@@ -89,28 +102,12 @@ func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
89102
if err != nil {
90103
return nil, fmt.Errorf("parsing '%s' failed: source: %w", fields[sepIdx+2], err)
91104
}
92-
p.VFSOptions = fields[sepIdx+3]
93-
94-
// ignore any numbers parsing errors, as there should not be any
95-
p.ID, _ = strconv.Atoi(fields[0])
96-
p.Parent, _ = strconv.Atoi(fields[1])
97-
mm := strings.SplitN(fields[2], ":", 3)
98-
if len(mm) != 2 {
99-
return nil, fmt.Errorf("parsing '%s' failed: unexpected major:minor pair %s", text, mm)
100-
}
101-
p.Major, _ = strconv.Atoi(mm[0])
102-
p.Minor, _ = strconv.Atoi(mm[1])
103105

104106
p.Root, err = unescape(fields[3])
105107
if err != nil {
106108
return nil, fmt.Errorf("parsing '%s' failed: root: %w", fields[3], err)
107109
}
108110

109-
p.Options = fields[5]
110-
111-
// zero or more optional fields
112-
p.Optional = strings.Join(fields[6:sepIdx], " ")
113-
114111
// Run the filter after parsing all fields.
115112
var skip, stop bool
116113
if filter != nil {
@@ -248,3 +245,10 @@ func unescape(path string) (string, error) {
248245

249246
return string(buf[:bufLen]), nil
250247
}
248+
249+
// toInt converts a string to an int, and ignores any numbers parsing errors,
250+
// as there should not be any.
251+
func toInt(s string) int {
252+
i, _ := strconv.Atoi(s)
253+
return i
254+
}

Diff for: sequential/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/moby/sys/sequential
22

3-
go 1.17
3+
go 1.18
44

55
require golang.org/x/sys v0.1.0

Diff for: signal/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/moby/sys/signal
22

3-
go 1.17
3+
go 1.18
44

55
require golang.org/x/sys v0.1.0

Diff for: symlink/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/moby/sys/symlink
22

3-
go 1.17
3+
go 1.18
44

55
require golang.org/x/sys v0.1.0

Diff for: user/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/moby/sys/user
22

3-
go 1.17
3+
go 1.18
44

55
require golang.org/x/sys v0.1.0

0 commit comments

Comments
 (0)