mirror of
https://github.com/distribution/distribution.git
synced 2026-10-28 10:15:27 +08:00
update golangci-lint to v2.9 and fix linting issues
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
25 files changed
+76
-65
No files matched your search
+24
-19
@@ -1,27 +1,32 @@
|
||||
linters:
|
||||
version: "2"
|
||||
|
||||
formatters:
|
||||
enable:
|
||||
- staticcheck
|
||||
- unconvert
|
||||
- gofmt
|
||||
- goimports
|
||||
- revive
|
||||
- ineffassign
|
||||
- govet
|
||||
- unused
|
||||
- misspell
|
||||
|
||||
linters:
|
||||
enable:
|
||||
- bodyclose
|
||||
- misspell
|
||||
- prealloc
|
||||
- errcheck
|
||||
- revive
|
||||
- staticcheck
|
||||
- tparallel
|
||||
- unconvert
|
||||
|
||||
linters-settings:
|
||||
revive:
|
||||
rules:
|
||||
# TODO(thaJeztah): temporarily disabled the "unused-parameter" check.
|
||||
# It produces many warnings, and some of those may need to be looked at.
|
||||
- name: unused-parameter
|
||||
disabled: true
|
||||
disable:
|
||||
- errcheck
|
||||
|
||||
issues:
|
||||
exclude-dirs:
|
||||
- vendor
|
||||
settings:
|
||||
revive:
|
||||
rules:
|
||||
# TODO(thaJeztah): temporarily disabled the "unused-parameter" check.
|
||||
# It produces many warnings, and some of those may need to be looked at.
|
||||
- name: unused-parameter
|
||||
disabled: true
|
||||
staticcheck:
|
||||
checks:
|
||||
- all
|
||||
- -QF1008 # Omit embedded fields from selector expression; https://staticcheck.dev/docs/checks/#QF1008
|
||||
- -ST1000 # Incorrect or missing package comment; https://staticcheck.dev/docs/checks/#ST1000
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
ARG GO_VERSION=1.23.7
|
||||
ARG ALPINE_VERSION=3.21
|
||||
ARG GOLANGCI_LINT_VERSION=v1.64.8
|
||||
# GOLANGCI_LINT_VERSION sets the version of the golangci/golangci-lint image to use.
|
||||
ARG GOLANGCI_LINT_VERSION=v2.9
|
||||
ARG BUILDTAGS=""
|
||||
|
||||
FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint
|
||||
|
||||
@@ -440,7 +440,8 @@ func TestBlobUploadChunked(t *testing.T) {
|
||||
b1[513:1024],
|
||||
}
|
||||
repo, _ := reference.WithName("test.example.com/uploadrepo")
|
||||
uuids := []string{uuid.NewString()}
|
||||
uuids := make([]string, 0, len(chunks)+1)
|
||||
uuids = append(uuids, uuid.NewString())
|
||||
m = append(m, testutil.RequestResponseMapping{
|
||||
Request: testutil.Request{
|
||||
Method: http.MethodPost,
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Returns a new V7 UUID string. V7 UUIDs are time-ordered for better database performance.
|
||||
// NewString returns a new V7 UUID string. V7 UUIDs are time-ordered for better database performance.
|
||||
// Panics on error to maintain compatibility with google/uuid's NewString() method.
|
||||
func NewString() string {
|
||||
return uuid.Must(uuid.NewV7()).String()
|
||||
|
||||
@@ -44,6 +44,10 @@ func init() {
|
||||
defer endpoints.mu.Unlock()
|
||||
|
||||
var names []interface{}
|
||||
if len(endpoints.registered) == 0 {
|
||||
return names
|
||||
}
|
||||
names = make([]interface{}, 0, len(endpoints.registered))
|
||||
for _, v := range endpoints.registered {
|
||||
var epjson struct {
|
||||
Name string `json:"name"`
|
||||
|
||||
@@ -26,7 +26,7 @@ func (ec ErrorCode) ErrorCode() ErrorCode {
|
||||
// Error returns the ID/Value
|
||||
func (ec ErrorCode) Error() string {
|
||||
// NOTE(stevvooe): Cannot use message here since it may have unpopulated args.
|
||||
return strings.ToLower(strings.Replace(ec.String(), "_", " ", -1))
|
||||
return strings.ToLower(strings.ReplaceAll(ec.String(), "_", " "))
|
||||
}
|
||||
|
||||
// Descriptor returns the descriptor for the error code.
|
||||
|
||||
@@ -85,7 +85,7 @@ func TestErrorCodes(t *testing.T) {
|
||||
t.Fatalf("unexpected error code during error code marshal/unmarshal: %v != %v", ecUnmarshaled, ec)
|
||||
}
|
||||
|
||||
expectedErrorString := strings.ToLower(strings.Replace(ec.Descriptor().Value, "_", " ", -1))
|
||||
expectedErrorString := strings.ToLower(strings.ReplaceAll(ec.Descriptor().Value, "_", " "))
|
||||
if ec.Error() != expectedErrorString {
|
||||
t.Fatalf("unexpected return from %v.Error(): %q != %q", ec, ec.Error(), expectedErrorString)
|
||||
}
|
||||
|
||||
@@ -268,8 +268,7 @@ func (a byValue) Less(i, j int) bool { return a[i].Value < a[j].Value }
|
||||
|
||||
// GetGroupNames returns the list of Error group names that are registered
|
||||
func GetGroupNames() []string {
|
||||
keys := []string{}
|
||||
|
||||
keys := make([]string, 0, len(groupToDescriptors))
|
||||
for k := range groupToDescriptors {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
@@ -287,9 +286,9 @@ func GetErrorCodeGroup(name string) []ErrorDescriptor {
|
||||
// GetErrorAllDescriptors returns a slice of all ErrorDescriptors that are
|
||||
// registered, irrespective of what group they're in
|
||||
func GetErrorAllDescriptors() []ErrorDescriptor {
|
||||
result := []ErrorDescriptor{}
|
||||
|
||||
for _, group := range GetGroupNames() {
|
||||
groups := GetGroupNames()
|
||||
result := make([]ErrorDescriptor, 0, len(groups))
|
||||
for _, group := range groups {
|
||||
result = append(result, GetErrorCodeGroup(group)...)
|
||||
}
|
||||
sort.Sort(byValue(result))
|
||||
|
||||
@@ -147,7 +147,7 @@ func createHtpasswdFile(path string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := f.Write([]byte(fmt.Sprintf("docker:%s", string(encryptedPass[:])))); err != nil {
|
||||
if _, err := fmt.Fprintf(f, "docker:%s", string(encryptedPass[:])); err != nil {
|
||||
return err
|
||||
}
|
||||
dcontext.GetLoggerWithFields(context.Background(), map[interface{}]interface{}{
|
||||
|
||||
@@ -57,7 +57,7 @@ func (ac *accessController) Authorized(req *http.Request, accessRecords ...auth.
|
||||
}
|
||||
|
||||
if len(accessRecords) > 0 {
|
||||
var scopes []string
|
||||
scopes := make([]string, 0, len(accessRecords))
|
||||
for _, access := range accessRecords {
|
||||
scopes = append(scopes, fmt.Sprintf("%s:%s:%s", access.Type, access.Resource.Name, access.Action))
|
||||
}
|
||||
|
||||
@@ -135,9 +135,10 @@ func (ac authChallenge) challengeParams(r *http.Request) string {
|
||||
str = fmt.Sprintf("%s,scope=%q", str, scope)
|
||||
}
|
||||
|
||||
if ac.err == ErrInvalidToken || ac.err == ErrMalformedToken {
|
||||
switch ac.err {
|
||||
case ErrInvalidToken, ErrMalformedToken:
|
||||
str = fmt.Sprintf("%s,error=%q", str, "invalid_token")
|
||||
} else if ac.err == ErrInsufficientScope {
|
||||
case ErrInsufficientScope:
|
||||
str = fmt.Sprintf("%s,error=%q", str, "insufficient_scope")
|
||||
}
|
||||
|
||||
|
||||
@@ -77,9 +77,9 @@ func GetJWKThumbprint(publickey crypto.PublicKey) string {
|
||||
func getJWKThumbprint(publickey crypto.PublicKey, skipED25519 bool) string {
|
||||
switch pubkey := publickey.(type) {
|
||||
case *rsa.PublicKey:
|
||||
e_big := big.NewInt(int64(pubkey.E)).Bytes()
|
||||
eBig := big.NewInt(int64(pubkey.E)).Bytes()
|
||||
|
||||
e := base64.RawURLEncoding.EncodeToString(e_big)
|
||||
e := base64.RawURLEncoding.EncodeToString(eBig)
|
||||
n := base64.RawURLEncoding.EncodeToString(pubkey.N.Bytes())
|
||||
|
||||
return hashAndEncode(fmt.Sprintf(`{"e":"%s","kty":"RSA","n":"%s"}`, e, n))
|
||||
|
||||
@@ -464,7 +464,7 @@ func (app *App) register(routeName string, dispatch dispatchFunc) {
|
||||
// Chain the handler with prometheus instrumented handler
|
||||
if app.Config.HTTP.Debug.Prometheus.Enabled {
|
||||
namespace := metrics.NewNamespace(prometheus.NamespacePrefix, "http", nil)
|
||||
httpMetrics := namespace.NewDefaultHttpMetrics(strings.Replace(routeName, "-", "_", -1))
|
||||
httpMetrics := namespace.NewDefaultHttpMetrics(strings.ReplaceAll(routeName, "-", "_"))
|
||||
metrics.Register(namespace)
|
||||
handler = metrics.InstrumentHandler(httpMetrics, handler)
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func (hook *logHook) Fire(entry *logrus.Entry) error {
|
||||
|
||||
// Levels contains hook levels to be catched
|
||||
func (hook *logHook) Levels() []logrus.Level {
|
||||
levels := []logrus.Level{}
|
||||
levels := make([]logrus.Level, 0, len(hook.LevelsParam))
|
||||
for _, v := range hook.LevelsParam {
|
||||
lv, _ := logrus.ParseLevel(v)
|
||||
levels = append(levels, lv)
|
||||
|
||||
@@ -153,9 +153,10 @@ func (imh *manifestHandler) GetManifest(w http.ResponseWriter, r *http.Request)
|
||||
if _, isOCImanifest := manifest.(*ocischema.DeserializedManifest); isOCImanifest {
|
||||
manifestType = ociSchema
|
||||
} else if isManifestList {
|
||||
if manifestList.MediaType == manifestlist.MediaTypeManifestList {
|
||||
switch manifestList.MediaType {
|
||||
case manifestlist.MediaTypeManifestList:
|
||||
manifestType = manifestlistSchema
|
||||
} else if manifestList.MediaType == v1.MediaTypeImageIndex {
|
||||
case v1.MediaTypeImageIndex:
|
||||
manifestType = ociImageIndexSchema
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
|
||||
func NewListener(net, laddr string) (net.Listener, error) {
|
||||
listeners, err := activation.Listeners()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Socket activation failed: %v", err)
|
||||
return nil, fmt.Errorf("socket activation failed: %v", err)
|
||||
}
|
||||
switch len(listeners) {
|
||||
case 0:
|
||||
@@ -57,7 +57,7 @@ func NewListener(net, laddr string) (net.Listener, error) {
|
||||
log.Info("Using systemd socket activation instead of any configured network listeners")
|
||||
return listeners[0], nil
|
||||
default:
|
||||
return nil, fmt.Errorf("Found %d socket-activation listeners, only expected 1", len(listeners))
|
||||
return nil, fmt.Errorf("found %d socket-activation listeners, only expected 1", len(listeners))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ func (reg *registry) Repositories(ctx context.Context, repos []string, last stri
|
||||
foundRepos := 0
|
||||
|
||||
if len(repos) == 0 {
|
||||
return 0, errors.New("Attempted to list 0 repositories")
|
||||
return 0, errors.New("attempted to list 0 repositories")
|
||||
}
|
||||
|
||||
root, err := pathFor(repositoriesRootPathSpec{})
|
||||
|
||||
@@ -87,7 +87,7 @@ func makeRepo(ctx context.Context, t *testing.T, name string, reg distribution.N
|
||||
t.Fatalf("failed to upload layers: %v", err)
|
||||
}
|
||||
|
||||
digests := []digest.Digest{}
|
||||
digests := make([]digest.Digest, 0, len(layers))
|
||||
for digest := range layers {
|
||||
digests = append(digests, digest)
|
||||
}
|
||||
|
||||
@@ -661,7 +661,7 @@ func (bw *blockWriter) Write(p []byte) (int, error) {
|
||||
}
|
||||
appendposFailed := bloberror.HasCode(err, bloberror.AppendPositionConditionNotMet)
|
||||
etagFailed := bloberror.HasCode(err, bloberror.ConditionNotMet)
|
||||
if !(appendposFailed || etagFailed) || !timeoutFromCtx {
|
||||
if (!appendposFailed && !etagFailed) || !timeoutFromCtx {
|
||||
// Error was not caused by an operation timeout, abort!
|
||||
return n, fmt.Errorf("appending blob: %w", err)
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ func TestErrorFormat(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expJson := `{"driver":"foo","detail":"unexpected error"}`
|
||||
if gotJson := string(b); gotJson != expJson {
|
||||
t.Fatalf("expected JSON: %s,\n got: %s", expJson, gotJson)
|
||||
expJSON := `{"driver":"foo","detail":"unexpected error"}`
|
||||
if gotJSON := string(b); gotJSON != expJSON {
|
||||
t.Fatalf("expected JSON: %s,\n got: %s", expJSON, gotJSON)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,25 +40,25 @@ func TestErrors(t *testing.T) {
|
||||
name string
|
||||
errs Errors
|
||||
exp string
|
||||
expJson string
|
||||
expJSON string
|
||||
}{
|
||||
{
|
||||
name: "no details",
|
||||
errs: Errors{DriverName: drvName},
|
||||
exp: fmt.Sprintf("%s: <nil>", drvName),
|
||||
expJson: `{"driver":"foo","details":[]}`,
|
||||
expJSON: `{"driver":"foo","details":[]}`,
|
||||
},
|
||||
{
|
||||
name: "single detail",
|
||||
errs: Errors{DriverName: drvName, Errs: []error{errors.New("err msg")}},
|
||||
exp: fmt.Sprintf("%s: err msg", drvName),
|
||||
expJson: `{"driver":"foo","details":["err msg"]}`,
|
||||
expJSON: `{"driver":"foo","details":["err msg"]}`,
|
||||
},
|
||||
{
|
||||
name: "multiple details",
|
||||
errs: Errors{DriverName: drvName, Errs: []error{errors.New("err msg1"), errors.New("err msg2")}},
|
||||
exp: fmt.Sprintf("%s: errors:\nerr msg1\nerr msg2\n", drvName),
|
||||
expJson: `{"driver":"foo","details":["err msg1","err msg2"]}`,
|
||||
expJSON: `{"driver":"foo","details":["err msg1","err msg2"]}`,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -72,8 +72,8 @@ func TestErrors(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gotJson := string(b); gotJson != tc.expJson {
|
||||
t.Errorf("expected JSON: %s,\n got: %s", tc.expJson, gotJson)
|
||||
if gotJSON := string(b); gotJSON != tc.expJSON {
|
||||
t.Errorf("expected JSON: %s,\n got: %s", tc.expJSON, gotJSON)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ type baseEmbed struct {
|
||||
func FromParameters(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
||||
bucket, ok := parameters["bucket"]
|
||||
if !ok || fmt.Sprint(bucket) == "" {
|
||||
return nil, fmt.Errorf("No bucket parameter provided")
|
||||
return nil, fmt.Errorf("no bucket parameter provided")
|
||||
}
|
||||
|
||||
rootDirectory, ok := parameters["rootdirectory"]
|
||||
@@ -171,21 +171,21 @@ func FromParameters(ctx context.Context, parameters map[string]interface{}) (sto
|
||||
} else if credentials, ok := parameters["credentials"]; ok {
|
||||
credentialMap, ok := credentials.(map[interface{}]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("The credentials were not specified in the correct format")
|
||||
return nil, fmt.Errorf("the credentials were not specified in the correct format")
|
||||
}
|
||||
|
||||
stringMap := map[string]interface{}{}
|
||||
for k, v := range credentialMap {
|
||||
key, ok := k.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("One of the credential keys was not a string: %s", fmt.Sprint(k))
|
||||
return nil, fmt.Errorf("one of the credential keys was not a string: %s", fmt.Sprint(k))
|
||||
}
|
||||
stringMap[key] = v
|
||||
}
|
||||
|
||||
data, err := json.Marshal(stringMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to marshal gcs credentials to json")
|
||||
return nil, fmt.Errorf("failed to marshal gcs credentials to json")
|
||||
}
|
||||
|
||||
jwtConf, err = google.JWTConfigFromJSON(data, storage.ScopeFullControl)
|
||||
@@ -242,7 +242,7 @@ func New(ctx context.Context, params driverParameters) (storagedriver.StorageDri
|
||||
rootDirectory += "/"
|
||||
}
|
||||
if params.chunkSize <= 0 || params.chunkSize%minChunkSize != 0 {
|
||||
return nil, fmt.Errorf("Invalid chunksize: %d is not a positive multiple of %d", params.chunkSize, minChunkSize)
|
||||
return nil, fmt.Errorf("invalid chunksize: %d is not a positive multiple of %d", params.chunkSize, minChunkSize)
|
||||
}
|
||||
d := &driver{
|
||||
bucket: params.gcs.Bucket(params.bucket),
|
||||
|
||||
@@ -501,7 +501,7 @@ func TestDelete(t *testing.T) {
|
||||
"/folder1-v2/subfolder1/file1",
|
||||
}
|
||||
|
||||
tcs := []testCase{
|
||||
tcs := []testCase{ //nolint:prealloc
|
||||
{
|
||||
name: "delete folder1",
|
||||
delete: "/folder1",
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/distribution/distribution/v3/internal/dcontext"
|
||||
"github.com/distribution/distribution/v3/manifest/ocischema"
|
||||
"github.com/distribution/distribution/v3/registry/storage/driver"
|
||||
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
||||
"github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
|
||||
"github.com/distribution/distribution/v3/testutil"
|
||||
"github.com/distribution/reference"
|
||||
@@ -114,7 +113,7 @@ func uploadRandomSchema2Image(t *testing.T, repository distribution.Repository)
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
digests := []digest.Digest{}
|
||||
digests := make([]digest.Digest, 0, len(randomLayers))
|
||||
for digest := range randomLayers {
|
||||
digests = append(digests, digest)
|
||||
}
|
||||
@@ -138,7 +137,7 @@ func uploadRandomOCIImage(t *testing.T, repository distribution.Repository) imag
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
digests := []digest.Digest{}
|
||||
digests := make([]digest.Digest, 0, len(randomLayers))
|
||||
for digest := range randomLayers {
|
||||
digests = append(digests, digest)
|
||||
}
|
||||
@@ -453,7 +452,7 @@ func TestGCWithUnusedLayerLinkPath(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = d.Stat(ctx, layerLinkPath)
|
||||
if _, ok := err.(storagedriver.PathNotFoundError); !ok {
|
||||
if _, ok := err.(driver.PathNotFoundError); !ok {
|
||||
t.Fatalf("layer link path %s should be not found", layerLinkPath)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ func TestPurgeOnlyUploads(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
nonUploadPath := strings.Replace(dataPath, "_upload", "_important", -1)
|
||||
nonUploadPath := strings.ReplaceAll(dataPath, "_upload", "_important")
|
||||
if strings.Contains(nonUploadPath, "_upload") {
|
||||
t.Fatal("Non-upload path not created correctly")
|
||||
}
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ func (r Request) String() string {
|
||||
}
|
||||
var headers []string
|
||||
if len(r.Headers) > 0 {
|
||||
var headerKeys []string
|
||||
headerKeys := make([]string, 0, len(r.Headers))
|
||||
for k := range r.Headers {
|
||||
headerKeys = append(headerKeys, k)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user