diff --git a/pkg/registry/chart_test.go b/pkg/registry/chart_test.go index a67bc853a..77ccdaab7 100644 --- a/pkg/registry/chart_test.go +++ b/pkg/registry/chart_test.go @@ -236,12 +236,12 @@ func TestGenerateOCICreatedAnnotations(t *testing.T) { nowTime := time.Now() nowTimeString := nowTime.Format(time.RFC3339) - chart := &chart.Metadata{ + testChart := &chart.Metadata{ Name: "oci", Version: "0.0.1", } - result := generateOCIAnnotations(chart, nowTimeString) + result := generateOCIAnnotations(testChart, nowTimeString) // Check that created annotation exists if _, ok := result[ocispec.AnnotationCreated]; !ok { @@ -254,7 +254,7 @@ func TestGenerateOCICreatedAnnotations(t *testing.T) { } // Verify default creation time set - result = generateOCIAnnotations(chart, "") + result = generateOCIAnnotations(testChart, "") // Check that created annotation exists if _, ok := result[ocispec.AnnotationCreated]; !ok { diff --git a/pkg/registry/client.go b/pkg/registry/client.go index 1cb629657..9eb189216 100644 --- a/pkg/registry/client.go +++ b/pkg/registry/client.go @@ -255,7 +255,7 @@ func (c *Client) Login(host string, options ...LoginOption) error { return err } - fmt.Fprintln(c.out, "Login Succeeded") + _, _ = fmt.Fprintln(c.out, "Login Succeeded") return nil } @@ -383,7 +383,7 @@ func (c *Client) Logout(host string, opts ...LogoutOption) error { if err := credentials.Logout(context.Background(), c.credentialsStore, host); err != nil { return err } - fmt.Fprintf(c.out, "Removing login credentials for %s\n", host) + _, _ = fmt.Fprintf(c.out, "Removing login credentials for %s\n", host) return nil } @@ -453,7 +453,7 @@ func (c *Client) processChartPull(genericResult *GenericPullResult, operation *p provDescriptor = &d case LegacyChartLayerMediaType: chartDescriptor = &d - fmt.Fprintf(c.out, "Warning: chart media type %s is deprecated\n", LegacyChartLayerMediaType) + _, _ = fmt.Fprintf(c.out, "Warning: chart media type %s is deprecated\n", LegacyChartLayerMediaType) } } @@ -526,12 +526,12 @@ func (c *Client) processChartPull(genericResult *GenericPullResult, operation *p result.Prov.Size = provDescriptor.Size } - fmt.Fprintf(c.out, "Pulled: %s\n", result.Ref) - fmt.Fprintf(c.out, "Digest: %s\n", result.Manifest.Digest) + _, _ = fmt.Fprintf(c.out, "Pulled: %s\n", result.Ref) + _, _ = fmt.Fprintf(c.out, "Digest: %s\n", result.Manifest.Digest) if strings.Contains(result.Ref, "_") { - fmt.Fprintf(c.out, "%s contains an underscore.\n", result.Ref) - fmt.Fprint(c.out, registryUnderscoreMessage+"\n") + _, _ = fmt.Fprintf(c.out, "%s contains an underscore.\n", result.Ref) + _, _ = fmt.Fprint(c.out, registryUnderscoreMessage+"\n") } return result, nil @@ -728,11 +728,11 @@ func (c *Client) Push(data []byte, ref string, options ...PushOption) (*PushResu Size: provDescriptor.Size, } } - fmt.Fprintf(c.out, "Pushed: %s\n", result.Ref) - fmt.Fprintf(c.out, "Digest: %s\n", result.Manifest.Digest) + _, _ = fmt.Fprintf(c.out, "Pushed: %s\n", result.Ref) + _, _ = fmt.Fprintf(c.out, "Digest: %s\n", result.Manifest.Digest) if strings.Contains(parsedRef.orasReference.Reference, "_") { - fmt.Fprintf(c.out, "%s contains an underscore.\n", result.Ref) - fmt.Fprint(c.out, registryUnderscoreMessage+"\n") + _, _ = fmt.Fprintf(c.out, "%s contains an underscore.\n", result.Ref) + _, _ = fmt.Fprint(c.out, registryUnderscoreMessage+"\n") } return result, err diff --git a/pkg/registry/client_http_test.go b/pkg/registry/client_http_test.go index c4f950214..a2c3a1833 100644 --- a/pkg/registry/client_http_test.go +++ b/pkg/registry/client_http_test.go @@ -37,7 +37,7 @@ func (suite *HTTPRegistryClientTestSuite) SetupSuite() { func (suite *HTTPRegistryClientTestSuite) TearDownSuite() { teardown(&suite.TestRegistry) - os.RemoveAll(suite.WorkspaceDir) + _ = os.RemoveAll(suite.WorkspaceDir) } func (suite *HTTPRegistryClientTestSuite) Test_0_Login() { diff --git a/pkg/registry/client_insecure_tls_test.go b/pkg/registry/client_insecure_tls_test.go index e7f53c628..2774f5e6f 100644 --- a/pkg/registry/client_insecure_tls_test.go +++ b/pkg/registry/client_insecure_tls_test.go @@ -34,7 +34,7 @@ func (suite *InsecureTLSRegistryClientTestSuite) SetupSuite() { func (suite *InsecureTLSRegistryClientTestSuite) TearDownSuite() { teardown(&suite.TestRegistry) - os.RemoveAll(suite.WorkspaceDir) + _ = os.RemoveAll(suite.WorkspaceDir) } func (suite *InsecureTLSRegistryClientTestSuite) Test_0_Login() { diff --git a/pkg/registry/client_tls_test.go b/pkg/registry/client_tls_test.go index e7f00168b..ddeeb3b66 100644 --- a/pkg/registry/client_tls_test.go +++ b/pkg/registry/client_tls_test.go @@ -36,7 +36,7 @@ func (suite *TLSRegistryClientTestSuite) SetupSuite() { func (suite *TLSRegistryClientTestSuite) TearDownSuite() { teardown(&suite.TestRegistry) - os.RemoveAll(suite.WorkspaceDir) + _ = os.RemoveAll(suite.WorkspaceDir) } func (suite *TLSRegistryClientTestSuite) Test_0_Login() { diff --git a/pkg/registry/generic.go b/pkg/registry/generic.go index b82132338..fb7e80d10 100644 --- a/pkg/registry/generic.go +++ b/pkg/registry/generic.go @@ -92,7 +92,7 @@ func (c *GenericClient) PullGeneric(ref string, options GenericPullOptions) (*Ge memoryStore := memory.New() var descriptors []ocispec.Descriptor - // Set up repository with authentication and configuration + // Set up a repository with authentication and configuration repository, err := remote.NewRepository(parsedRef.String()) if err != nil { return nil, err @@ -114,7 +114,7 @@ func (c *GenericClient) PullGeneric(ref string, options GenericPullOptions) (*Ge manifest, err := oras.Copy(ctx, repository, parsedRef.String(), memoryStore, "", oras.CopyOptions{ CopyGraphOptions: oras.CopyGraphOptions{ PreCopy: func(ctx context.Context, desc ocispec.Descriptor) error { - // Apply custom PreCopy function if provided + // Apply a custom PreCopy function if provided if options.PreCopy != nil { if err := options.PreCopy(ctx, desc); err != nil { return err diff --git a/pkg/registry/plugin.go b/pkg/registry/plugin.go index 991bace76..e4b4afa24 100644 --- a/pkg/registry/plugin.go +++ b/pkg/registry/plugin.go @@ -147,15 +147,15 @@ func (c *Client) processPluginPull(genericResult *GenericPullResult, pluginName } } - fmt.Fprintf(c.out, "Pulled plugin: %s\n", result.Ref) - fmt.Fprintf(c.out, "Digest: %s\n", result.Manifest.Digest) + _, _ = fmt.Fprintf(c.out, "Pulled plugin: %s\n", result.Ref) + _, _ = fmt.Fprintf(c.out, "Digest: %s\n", result.Manifest.Digest) if result.Prov.Data != nil { - fmt.Fprintf(c.out, "Provenance: %s\n", foundProvenanceName) + _, _ = fmt.Fprintf(c.out, "Provenance: %s\n", foundProvenanceName) } if strings.Contains(result.Ref, "_") { - fmt.Fprintf(c.out, "%s contains an underscore.\n", result.Ref) - fmt.Fprint(c.out, registryUnderscoreMessage+"\n") + _, _ = fmt.Fprintf(c.out, "%s contains an underscore.\n", result.Ref) + _, _ = fmt.Fprint(c.out, registryUnderscoreMessage+"\n") } return result, nil diff --git a/pkg/registry/reference.go b/pkg/registry/reference.go index bd0974e69..9a98cf5c9 100644 --- a/pkg/registry/reference.go +++ b/pkg/registry/reference.go @@ -32,11 +32,11 @@ type reference struct { } // newReference will parse and validate the reference, and clean tags when -// applicable tags are only cleaned when plus (+) signs are present, and are +// applicable tags are only cleaned when plus (+) signs are present and are // converted to underscores (_) before pushing // See https://github.com/helm/helm/issues/10166 func newReference(raw string) (result reference, err error) { - // Remove oci:// prefix if it is there + // Remove the oci:// prefix if it is there raw = strings.TrimPrefix(raw, OCIScheme+"://") // The sole possible reference modification is replacing plus (+) signs diff --git a/pkg/registry/registry_test.go b/pkg/registry/registry_test.go index 4b0e72319..d4921c50b 100644 --- a/pkg/registry/registry_test.go +++ b/pkg/registry/registry_test.go @@ -35,6 +35,7 @@ import ( "github.com/distribution/distribution/v3/registry" _ "github.com/distribution/distribution/v3/registry/auth/htpasswd" _ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "golang.org/x/crypto/bcrypt" @@ -68,13 +69,13 @@ type TestRegistry struct { func setup(suite *TestRegistry, tlsEnabled, insecure bool) { suite.WorkspaceDir = testWorkspaceDir - os.RemoveAll(suite.WorkspaceDir) - os.Mkdir(suite.WorkspaceDir, 0700) + err := os.RemoveAll(suite.WorkspaceDir) + require.NoError(suite.T(), err, "no error removing test workspace dir") + err = os.Mkdir(suite.WorkspaceDir, 0700) + require.NoError(suite.T(), err, "no error creating test workspace dir") + + var out bytes.Buffer - var ( - out bytes.Buffer - err error - ) suite.Out = &out credentialsFile := filepath.Join(suite.WorkspaceDir, CredentialsFileBasename) @@ -124,7 +125,7 @@ func setup(suite *TestRegistry, tlsEnabled, insecure bool) { config := &configuration.Configuration{} ln, err := net.Listen("tcp", "127.0.0.1:0") suite.Nil(err, "no error finding free port for test registry") - defer ln.Close() + defer func() { _ = ln.Close() }() // Change the registry host to another host which is not localhost. // This is required because Docker enforces HTTP if the registry @@ -176,7 +177,7 @@ func initCompromisedRegistryTestServer() string { w.Header().Set("Content-Type", "application/vnd.oci.image.manifest.v1+json") w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, `{ "schemaVersion": 2, "config": { + _, _ = fmt.Fprintf(w, `{ "schemaVersion": 2, "config": { "mediaType": "%s", "digest": "sha256:a705ee2789ab50a5ba20930f246dbd5cc01ff9712825bb98f57ee8414377f133", "size": 181 @@ -192,13 +193,13 @@ func initCompromisedRegistryTestServer() string { } else if r.URL.Path == "/v2/testrepo/supposedlysafechart/blobs/sha256:a705ee2789ab50a5ba20930f246dbd5cc01ff9712825bb98f57ee8414377f133" { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write([]byte("{\"name\":\"mychart\",\"version\":\"0.1.0\",\"description\":\"A Helm chart for Kubernetes\\n" + + _, _ = w.Write([]byte("{\"name\":\"mychart\",\"version\":\"0.1.0\",\"description\":\"A Helm chart for Kubernetes\\n" + "an 'application' or a 'library' chart.\",\"apiVersion\":\"v2\",\"appVersion\":\"1.16.0\",\"type\":" + "\"application\"}")) } else if r.URL.Path == "/v2/testrepo/supposedlysafechart/blobs/sha256:ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb" { w.Header().Set("Content-Type", ChartLayerMediaType) w.WriteHeader(http.StatusOK) - w.Write([]byte("b")) + _, _ = w.Write([]byte("b")) } else { w.WriteHeader(http.StatusInternalServerError) } diff --git a/pkg/registry/tag.go b/pkg/registry/tag.go index 701701d7b..bfb4b1ef6 100644 --- a/pkg/registry/tag.go +++ b/pkg/registry/tag.go @@ -25,10 +25,10 @@ import ( func GetTagMatchingVersionOrConstraint(tags []string, versionString string) (string, error) { var constraint *semver.Constraints if versionString == "" { - // If string is empty, set wildcard constraint + // If the string is empty, set a wildcard constraint constraint, _ = semver.NewConstraint("*") } else { - // when customer inputs specific version, check whether there's an exact match first + // when customer inputs a specific version, check whether there's an exact match first for _, v := range tags { if versionString == v { return v, nil diff --git a/pkg/registry/transport.go b/pkg/registry/transport.go index 9d6a37326..f039a8159 100644 --- a/pkg/registry/transport.go +++ b/pkg/registry/transport.go @@ -97,7 +97,7 @@ func (t *LoggingTransport) RoundTrip(req *http.Request) (resp *http.Response, er // logHeader prints out the provided header keys and values, with auth header scrubbed. func logHeader(header http.Header) string { if len(header) > 0 { - headers := []string{} + var headers []string for k, v := range header { for _, h := range toScrub { if strings.EqualFold(k, h) {