Skip to content

Commit 013cf22

Browse files
committed
Enable more golangci-lint
* Enable additional formatters. * Enable modernize linter. * Apply fixes. Signed-off-by: SuperQ <[email protected]>
1 parent cf90464 commit 013cf22

File tree

7 files changed

+38
-25
lines changed

7 files changed

+38
-25
lines changed

.golangci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@ version: "2"
22

33
formatters:
44
enable:
5+
- gci
56
- gofumpt
7+
- goimports
8+
settings:
9+
gci:
10+
sections:
11+
- standard
12+
- default
13+
- prefix(github.com/opencontainers/selinux)
14+
gofumpt:
15+
extra-rules: true
16+
goimports:
17+
local-prefixes:
18+
- github.com/opencontainers/selinux
619

720
linters:
821
enable:
@@ -12,6 +25,7 @@ linters:
1225
- gocritic # Metalinter; detects bugs, performance, and styling issues.
1326
- gosec # Detects security problems.
1427
- misspell # Detects commonly misspelled English words in comments.
28+
- modernize # Suggest simplifications to Go code.
1529
- nilerr # Detects code that returns nil even if it checks that the error is not nil.
1630
- nolintlint # Detects ill-formed or insufficient nolint directives.
1731
- prealloc # Detects slice declarations that could potentially be pre-allocated.

go-selinux/label/label_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var ErrIncompatibleLabel = errors.New("bad SELinux option: z and Z can not be us
2626
// guaranteed to be unique.
2727
// If the disabled flag is passed in, the process label will not be set, but the mount label will be set
2828
// to the container_file label with the maximum category. This label is not usable by any confined label.
29-
func InitLabels(options []string) (plabel string, mlabel string, retErr error) {
29+
func InitLabels(options []string) (plabel, mlabel string, retErr error) {
3030
if !selinux.GetEnabled() {
3131
return "", "", nil
3232
}
@@ -80,7 +80,7 @@ func InitLabels(options []string) (plabel string, mlabel string, retErr error) {
8080
}
8181

8282
// SetFileLabel modifies the "path" label to the specified file label
83-
func SetFileLabel(path string, fileLabel string) error {
83+
func SetFileLabel(path, fileLabel string) error {
8484
if !selinux.GetEnabled() || fileLabel == "" {
8585
return nil
8686
}
@@ -100,7 +100,7 @@ func SetFileCreateLabel(fileLabel string) error {
100100
// This will allow all containers to share the content.
101101
//
102102
// The path itself is guaranteed to be relabeled last.
103-
func Relabel(path string, fileLabel string, shared bool) error {
103+
func Relabel(path, fileLabel string, shared bool) error {
104104
if !selinux.GetEnabled() || fileLabel == "" {
105105
return nil
106106
}

go-selinux/selinux.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ func ClassIndex(class string) (int, error) {
7272

7373
// SetFileLabel sets the SELinux label for this path, following symlinks,
7474
// or returns an error.
75-
func SetFileLabel(fpath string, label string) error {
75+
func SetFileLabel(fpath, label string) error {
7676
return setFileLabel(fpath, label)
7777
}
7878

7979
// LsetFileLabel sets the SELinux label for this path, not following symlinks,
8080
// or returns an error.
81-
func LsetFileLabel(fpath string, label string) error {
81+
func LsetFileLabel(fpath, label string) error {
8282
return lSetFileLabel(fpath, label)
8383
}
8484

@@ -135,7 +135,7 @@ func CanonicalizeContext(val string) (string, error) {
135135

136136
// ComputeCreateContext requests the type transition from source to target for
137137
// class from the kernel.
138-
func ComputeCreateContext(source string, target string, class string) (string, error) {
138+
func ComputeCreateContext(source, target, class string) (string, error) {
139139
return computeCreateContext(source, target, class)
140140
}
141141

@@ -269,7 +269,7 @@ func InitContainerLabels() (string, string) {
269269

270270
// ContainerLabels returns an allocated processLabel and fileLabel to be used for
271271
// container labeling by the calling process.
272-
func ContainerLabels() (processLabel string, fileLabel string) {
272+
func ContainerLabels() (processLabel, fileLabel string) {
273273
return containerLabels()
274274
}
275275

@@ -289,7 +289,7 @@ func CopyLevel(src, dest string) (string, error) {
289289
// directory tree setting the label.
290290
//
291291
// The fpath itself is guaranteed to be relabeled last.
292-
func Chcon(fpath string, label string, recurse bool) error {
292+
func Chcon(fpath, label string, recurse bool) error {
293293
return chcon(fpath, label, recurse)
294294
}
295295

go-selinux/selinux_linux.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func classIndex(class string) (int, error) {
454454

455455
// lSetFileLabel sets the SELinux label for this path, not following symlinks,
456456
// or returns an error.
457-
func lSetFileLabel(fpath string, label string) error {
457+
func lSetFileLabel(fpath, label string) error {
458458
if fpath == "" {
459459
return ErrEmptyPath
460460
}
@@ -473,7 +473,7 @@ func lSetFileLabel(fpath string, label string) error {
473473

474474
// setFileLabel sets the SELinux label for this path, following symlinks,
475475
// or returns an error.
476-
func setFileLabel(fpath string, label string) error {
476+
func setFileLabel(fpath, label string) error {
477477
if fpath == "" {
478478
return ErrEmptyPath
479479
}
@@ -566,7 +566,7 @@ func canonicalizeContext(val string) (string, error) {
566566

567567
// computeCreateContext requests the type transition from source to target for
568568
// class from the kernel.
569-
func computeCreateContext(source string, target string, class string) (string, error) {
569+
func computeCreateContext(source, target, class string) (string, error) {
570570
classidx, err := classIndex(class)
571571
if err != nil {
572572
return "", err
@@ -579,8 +579,7 @@ func computeCreateContext(source string, target string, class string) (string, e
579579
func catsToBitset(cats string) (*big.Int, error) {
580580
bitset := new(big.Int)
581581

582-
catlist := strings.Split(cats, ",")
583-
for _, r := range catlist {
582+
for r := range strings.SplitSeq(cats, ",") {
584583
ranges := strings.SplitN(r, ".", 2)
585584
if len(ranges) > 1 {
586585
catstart, err := parseLevelItem(ranges[0], category)
@@ -788,7 +787,7 @@ func calculateGlbLub(sourceRange, targetRange string) (string, error) {
788787
return outrange.String(), nil
789788
}
790789

791-
func readWriteCon(fpath string, val string) (string, error) {
790+
func readWriteCon(fpath, val string) (string, error) {
792791
if fpath == "" {
793792
return "", ErrEmptyPath
794793
}
@@ -1088,7 +1087,7 @@ func initContainerLabels() (string, string) {
10881087

10891088
// containerLabels returns an allocated processLabel and fileLabel to be used for
10901089
// container labeling by the calling process.
1091-
func containerLabels() (processLabel string, fileLabel string) {
1090+
func containerLabels() (processLabel, fileLabel string) {
10921091
if !getEnabled() {
10931092
return "", ""
10941093
}
@@ -1155,7 +1154,7 @@ func copyLevel(src, dest string) (string, error) {
11551154
// chcon changes the fpath file object to the SELinux label.
11561155
// If fpath is a directory and recurse is true, then chcon walks the
11571156
// directory tree setting the label.
1158-
func chcon(fpath string, label string, recurse bool) error {
1157+
func chcon(fpath, label string, recurse bool) error {
11591158
if fpath == "" {
11601159
return ErrEmptyPath
11611160
}

go-selinux/selinux_linux_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func BenchmarkContextGet(b *testing.B) {
243243
b.Fatal(err)
244244
}
245245
str := ""
246-
for i := 0; i < b.N; i++ {
246+
for b.Loop() {
247247
str = ctx.get()
248248
}
249249
b.Log(str)
@@ -674,8 +674,8 @@ func BenchmarkChcon(b *testing.B) {
674674
b.Fatalf("FileLabel(%q): %v", file, err)
675675
}
676676
b.Logf("Chcon(%q, %q)", dir, con)
677-
b.ResetTimer()
678-
for n := 0; n < b.N; n++ {
677+
678+
for b.Loop() {
679679
if err := Chcon(dir, con, true); err != nil {
680680
b.Fatal(err)
681681
}
@@ -687,7 +687,7 @@ func BenchmarkCurrentLabel(b *testing.B) {
687687
l string
688688
err error
689689
)
690-
for n := 0; n < b.N; n++ {
690+
for b.Loop() {
691691
l, err = CurrentLabel()
692692
if err != nil {
693693
b.Fatal(err)
@@ -698,14 +698,14 @@ func BenchmarkCurrentLabel(b *testing.B) {
698698

699699
func BenchmarkReadConfig(b *testing.B) {
700700
str := ""
701-
for n := 0; n < b.N; n++ {
701+
for b.Loop() {
702702
str = readConfig(selinuxTypeTag)
703703
}
704704
b.Log(str)
705705
}
706706

707707
func BenchmarkLoadLabels(b *testing.B) {
708-
for n := 0; n < b.N; n++ {
708+
for b.Loop() {
709709
loadLabels()
710710
}
711711
}

pkg/pwalk/pwalk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func WalkN(root string, walkFn WalkFunc, num int) error {
100100
}()
101101

102102
wg.Add(num)
103-
for i := 0; i < num; i++ {
103+
for range num {
104104
go func() {
105105
for file := range files {
106106
if e := walkFn(file.path, *file.info, nil); e != nil {

pkg/pwalk/pwalk_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ func TestWalkDirManyErrors(t *testing.T) {
9393
}
9494

9595
func makeManyDirs(prefix string, levels, dirs, files int) (count uint32, err error) {
96-
for d := 0; d < dirs; d++ {
96+
for range dirs {
9797
var dir string
9898
dir, err = os.MkdirTemp(prefix, "d-")
9999
if err != nil {
100100
return count, err
101101
}
102102
count++
103-
for f := 0; f < files; f++ {
103+
for range files {
104104
var fi *os.File
105105
fi, err = os.CreateTemp(dir, "f-")
106106
if err != nil {

0 commit comments

Comments
 (0)