chore: update golangci-lint and fix all issues (#3575)

* chore: Update golangci-lint

Signed-off-by: Lars Francke <git@lars-francke.de>

* chore: fix all golangci-lint issues

- Remove deprecated `// +build` tags
- Fix godoclint, modernize, wsl_v5, govet, lll, gci, noctx issues
- Update linter configuration
- Modernize code to use Go 1.22+ features (for range N, slices.Contains, etc.)
- Update make check lint the privileged tests

Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>

---------

Signed-off-by: Lars Francke <git@lars-francke.de>
Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
Co-authored-by: Lars Francke <git@lars-francke.de>
This commit is contained in:
Andrei Aaron
2025-11-22 23:36:48 +02:00
committed by GitHub
parent 566286ae42
commit da426850e7
242 changed files with 811 additions and 1010 deletions
+15 -38
View File
@@ -9,6 +9,7 @@ import (
"net"
"os"
"regexp"
"slices"
"strings"
"syscall"
"time"
@@ -26,7 +27,8 @@ const (
CosignSignature = "cosign"
CosignSigKey = "dev.cosignproject.cosign/signature"
NotationSignature = "notation"
// same value as github.com/notaryproject/notation-go/registry.ArtifactTypeNotation (assert by internal test).
// ArtifactTypeNotation is the same value as github.com/notaryproject/notation-go/registry.ArtifactTypeNotation
// (assert by internal test).
// reason used: to reduce zot minimal binary size (otherwise adds oras.land/oras-go/v2 deps).
ArtifactTypeNotation = "application/vnd.cncf.notary.signature"
ArtifactTypeCosign = "application/vnd.dev.cosign.artifact.sig.v1+json"
@@ -51,28 +53,7 @@ func IsCosignTag(tag string) bool {
return IsCosignSignature(tag) || IsCosignSBOM(tag)
}
func Contains[T comparable](elems []T, v T) bool {
for _, s := range elems {
if v == s {
return true
}
}
return false
}
// first match of item in [].
func Index(slice []string, item string) int {
for k, v := range slice {
if item == v {
return k
}
}
return -1
}
// remove matches of item in [].
// RemoveFrom removes matches of item in [].
func RemoveFrom(inputSlice []string, item string) []string {
var newSlice []string
@@ -85,7 +66,7 @@ func RemoveFrom(inputSlice []string, item string) []string {
return newSlice
}
func TypeOf(v interface{}) string {
func TypeOf(v any) string {
return fmt.Sprintf("%T", v)
}
@@ -113,8 +94,8 @@ func DirExists(d string) bool {
return true
}
// Used to filter a json fields by using an intermediate struct.
func MarshalThroughStruct(obj interface{}, throughStruct interface{}) ([]byte, error) {
// MarshalThroughStruct is used to filter a json fields by using an intermediate struct.
func MarshalThroughStruct(obj any, throughStruct any) ([]byte, error) {
toJSON, err := json.Marshal(obj)
if err != nil {
return []byte{}, err
@@ -134,16 +115,12 @@ func MarshalThroughStruct(obj interface{}, throughStruct interface{}) ([]byte, e
}
func ContainsStringIgnoreCase(strSlice []string, str string) bool {
for _, val := range strSlice {
if strings.EqualFold(val, str) {
return true
}
}
return false
return slices.ContainsFunc(strSlice, func(val string) bool {
return strings.EqualFold(val, str)
})
}
// this function will check if tag is a referrers tag
// IsReferrersTag checks if tag is a referrers tag
// (https://github.com/opencontainers/distribution-spec/blob/main/spec.md#referrers-tag-schema).
func IsReferrersTag(tag string) bool {
referrersTagRule := regexp.MustCompile(`sha256\-[A-Za-z0-9]*$`)
@@ -160,7 +137,7 @@ func IsContextDone(ctx context.Context) bool {
}
}
// get a list of IP addresses configured on the host's
// GetLocalIPs gets a list of IP addresses configured on the host's
// interfaces.
func GetLocalIPs() ([]string, error) {
var localIPs []string
@@ -186,7 +163,7 @@ func GetLocalIPs() ([]string, error) {
return localIPs, nil
}
// get a list of listening sockets on the host (IP:port).
// GetLocalSockets gets a list of listening sockets on the host (IP:port).
// IPv6 is returned as [host]:port.
func GetLocalSockets(port string) ([]string, error) {
localIPs, err := GetLocalIPs()
@@ -205,7 +182,7 @@ func GetLocalSockets(port string) ([]string, error) {
}
func GetIPFromHostName(host string) ([]string, error) {
addrs, err := net.LookupIP(host)
addrs, err := net.LookupIP(host) //nolint: noctx
if err != nil {
return []string{}, err
}
@@ -219,7 +196,7 @@ func GetIPFromHostName(host string) ([]string, error) {
return ips, nil
}
// checks if 2 sockets are equal at the host port level.
// AreSocketsEqual checks if 2 sockets are equal at the host port level.
func AreSocketsEqual(socketA string, socketB string) (bool, error) {
hostA, portA, err := net.SplitHostPort(socketA)
if err != nil {
+4 -8
View File
@@ -3,6 +3,7 @@ package common_test
import (
"os"
"path"
"slices"
"strings"
"testing"
@@ -16,9 +17,9 @@ import (
func TestCommon(t *testing.T) {
Convey("test Contains()", t, func() {
first := []string{"apple", "biscuit"}
So(common.Contains(first, "apple"), ShouldBeTrue)
So(common.Contains(first, "peach"), ShouldBeFalse)
So(common.Contains([]string{}, "apple"), ShouldBeFalse)
So(slices.Contains(first, "apple"), ShouldBeTrue)
So(slices.Contains(first, "peach"), ShouldBeFalse)
So(slices.Contains([]string{}, "apple"), ShouldBeFalse)
})
Convey("test MarshalThroughStruct()", t, func() {
@@ -56,11 +57,6 @@ func TestCommon(t *testing.T) {
So(isDir, ShouldBeFalse)
})
Convey("Index func", t, func() {
So(common.Index([]string{"a", "b"}, "b"), ShouldEqual, 1)
So(common.Index([]string{"a", "b"}, "c"), ShouldEqual, -1)
})
Convey("Test ArtifactTypeNotation const has same value as in notaryproject", t, func() {
So(common.ArtifactTypeNotation, ShouldEqual, notreg.ArtifactTypeNotation)
})
+2 -2
View File
@@ -56,14 +56,14 @@ func loadPerHostCerts(caCertPool *x509.CertPool, host string) *tls.Config {
return nil
}
// Holds certificate options for an HTTP client.
// HTTPClientCertOptions holds certificate options for an HTTP client.
type HTTPClientCertOptions struct {
ClientCertFile string // Holds the path to the client certificate file. Mandatory if ClientKeyFile is present.
ClientKeyFile string // Holds the path to the client key file. Mandatory if ClientCertFile is present.
RootCaCertFile string // Optional. Holds the path to the custom Root CA cert file.
}
// Holds client options for creating an HTTP client.
// HTTPClientOptions holds client options for creating an HTTP client.
type HTTPClientOptions struct {
// Results in a client with TLS config if true.
TLSEnabled bool
+1 -1
View File
@@ -129,7 +129,7 @@ func AuthzFail(w http.ResponseWriter, r *http.Request, identity, realm string, d
}
}
func WriteJSON(response http.ResponseWriter, status int, data interface{}) {
func WriteJSON(response http.ResponseWriter, status int, data any) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
body, err := json.Marshal(data)
+26 -13
View File
@@ -137,8 +137,9 @@ type Annotation struct {
}
type ImageListWithCVEFixedResponse struct {
Errors []ErrorGQL `json:"errors"`
ImageListWithCVEFixed `json:"data"`
Errors []ErrorGQL `json:"errors"`
}
type ImageListWithCVEFixed struct {
@@ -146,8 +147,9 @@ type ImageListWithCVEFixed struct {
}
type ImagesForCve struct {
Errors []ErrorGQL `json:"errors"`
ImagesForCVEList `json:"data"`
Errors []ErrorGQL `json:"errors"`
}
type ImagesForCVEList struct {
@@ -155,8 +157,9 @@ type ImagesForCVEList struct {
}
type ImagesForDigest struct {
Errors []ErrorGQL `json:"errors"`
ImagesForDigestList `json:"data"`
Errors []ErrorGQL `json:"errors"`
}
type ImagesForDigestList struct {
@@ -165,17 +168,20 @@ type ImagesForDigestList struct {
type RepoWithNewestImageResponse struct {
RepoListWithNewestImage `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type DerivedImageListResponse struct {
DerivedImageList `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type BaseImageListResponse struct {
BaseImageList `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type DerivedImageList struct {
@@ -188,7 +194,8 @@ type BaseImageList struct {
type ImageListResponse struct {
ImageList `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type ImageList struct {
@@ -197,12 +204,14 @@ type ImageList struct {
type ExpandedRepoInfoResp struct {
ExpandedRepoInfo `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type ReferrersResp struct {
ReferrersResult `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type ReferrersResult struct {
@@ -210,7 +219,8 @@ type ReferrersResult struct {
}
type GlobalSearchResultResp struct {
GlobalSearchResult `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type GlobalSearchResult struct {
@@ -248,7 +258,8 @@ type SingleImageSummary struct {
}
type ImageSummaryResult struct {
SingleImageSummary `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
//nolint:tagliatelle // graphQL schema
@@ -263,12 +274,14 @@ type BookmarkedRepos struct {
type StarredReposResponse struct {
StarredRepos `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type BookmarkedReposResponse struct {
BookmarkedRepos `json:"data"`
Errors []ErrorGQL `json:"errors"`
Errors []ErrorGQL `json:"errors"`
}
type ImageTags struct {