From e07c2c5401d4be7651d78a2dcb19b3976a164e9e Mon Sep 17 00:00:00 2001 From: Anurag <81210977+kranurag7@users.noreply.github.com> Date: Tue, 11 Jul 2023 18:35:14 +0530 Subject: [PATCH] chore: fix lint warning (#1001) Signed-off-by: Anurag Signed-off-by: Billy Zha --- cmd/oras/internal/display/print.go | 5 +++-- cmd/oras/internal/option/applier.go | 2 +- cmd/oras/internal/option/packer_test.go | 16 ++++++++++------ cmd/oras/internal/option/remote.go | 4 +++- cmd/oras/internal/option/remote_test.go | 9 +++++++-- cmd/oras/root/attach.go | 2 +- cmd/oras/root/discover.go | 9 ++++++--- internal/cache/target_test.go | 4 +++- internal/graph/graph_test.go | 19 ++++++++++++------- 9 files changed, 46 insertions(+), 24 deletions(-) diff --git a/cmd/oras/internal/display/print.go b/cmd/oras/internal/display/print.go index 35de4ec5a..48374a327 100644 --- a/cmd/oras/internal/display/print.go +++ b/cmd/oras/internal/display/print.go @@ -116,7 +116,7 @@ func (p *tagManifestStatusForRepo) PushReference(ctx context.Context, expected o if p.printHint != nil { p.printHint.Do(func() { ref := p.refPrefix + "@" + expected.Digest.String() - Print("Tagging", ref) + _ = Print("Tagging", ref) }) } if err := p.Repository.PushReference(ctx, expected, content, reference); err != nil { @@ -136,9 +136,10 @@ func (p *tagManifestStatusForTarget) Tag(ctx context.Context, desc ocispec.Descr if p.printHint != nil { p.printHint.Do(func() { ref := p.refPrefix + "@" + desc.Digest.String() - Print("Tagging", ref) + _ = Print("Tagging", ref) }) } + if err := p.Target.Tag(ctx, desc, reference); err != nil { return err } diff --git a/cmd/oras/internal/option/applier.go b/cmd/oras/internal/option/applier.go index f47aba14b..1d177e96e 100644 --- a/cmd/oras/internal/option/applier.go +++ b/cmd/oras/internal/option/applier.go @@ -29,7 +29,7 @@ type FlagApplier interface { // NOTE: The option argument need to be a pointer to the options, so its value // becomes addressable. func ApplyFlags(optsPtr interface{}, target *pflag.FlagSet) { - rangeFields(optsPtr, func(fa FlagApplier) error { + _ = rangeFields(optsPtr, func(fa FlagApplier) error { fa.ApplyFlags(target) return nil }) diff --git a/cmd/oras/internal/option/packer_test.go b/cmd/oras/internal/option/packer_test.go index 5c7470ee5..e0cd51339 100644 --- a/cmd/oras/internal/option/packer_test.go +++ b/cmd/oras/internal/option/packer_test.go @@ -70,7 +70,10 @@ func TestPacker_LoadManifestAnnotations_err(t *testing.T) { func TestPacker_LoadManifestAnnotations_annotationFile(t *testing.T) { testFile := filepath.Join(t.TempDir(), "testAnnotationFile") - os.WriteFile(testFile, []byte(testContent), fs.ModePerm) + err := os.WriteFile(testFile, []byte(testContent), fs.ModePerm) + if err != nil { + t.Fatalf("Error writing %s: %v", testFile, err) + } opts := Packer{AnnotationFilePath: testFile} anno, err := opts.LoadManifestAnnotations() @@ -120,11 +123,12 @@ func TestPacker_LoadManifestAnnotations_annotationFlag(t *testing.T) { t.Fatalf("unexpected error: failed when looking for '$manifest' in annotations") } if !reflect.DeepEqual(annotations, - map[string]map[string]string{"$manifest": { - "Key0": "", - "Key1": "Val", - "Key2": "${env:USERNAME}", - }, + map[string]map[string]string{ + "$manifest": { + "Key0": "", + "Key1": "Val", + "Key2": "${env:USERNAME}", + }, }) { t.Fatalf("unexpected error: %v", errors.New("content not match")) } diff --git a/cmd/oras/internal/option/remote.go b/cmd/oras/internal/option/remote.go index c4261a880..efd4e054a 100644 --- a/cmd/oras/internal/option/remote.go +++ b/cmd/oras/internal/option/remote.go @@ -273,7 +273,9 @@ func (opts *Remote) NewRepository(reference string, common Common) (repo *remote return nil, err } if opts.distributionSpec.referrersAPI != nil { - repo.SetReferrersCapability(*opts.distributionSpec.referrersAPI) + if err := repo.SetReferrersCapability(*opts.distributionSpec.referrersAPI); err != nil { + return nil, err + } } return } diff --git a/cmd/oras/internal/option/remote_test.go b/cmd/oras/internal/option/remote_test.go index efca51cc2..abd1c3029 100644 --- a/cmd/oras/internal/option/remote_test.go +++ b/cmd/oras/internal/option/remote_test.go @@ -51,7 +51,9 @@ func TestMain(m *testing.M) { case p == "/v2/" && m == "GET": w.WriteHeader(http.StatusOK) case p == fmt.Sprintf("/v2/%s/tags/list", testRepo) && m == "GET": - json.NewEncoder(w).Encode(testTagList) + if err := json.NewEncoder(w).Encode(testTagList); err != nil { + http.Error(w, "error encoding", http.StatusBadRequest) + } } })) defer ts.Close() @@ -233,7 +235,10 @@ func TestRemote_NewRepository_Retry(t *testing.T) { http.Error(w, "error", http.StatusTooManyRequests) return } - json.NewEncoder(w).Encode(testTagList) + err := json.NewEncoder(w).Encode(testTagList) + if err != nil { + http.Error(w, "error encoding", http.StatusBadRequest) + } })) defer ts.Close() opts := struct { diff --git a/cmd/oras/root/attach.go b/cmd/oras/root/attach.go index 99ed8ba14..d0a84dd13 100644 --- a/cmd/oras/root/attach.go +++ b/cmd/oras/root/attach.go @@ -94,7 +94,7 @@ Example - Attach file to the manifest tagged 'v1' in an OCI image layout folder cmd.Flags().StringVarP(&opts.artifactType, "artifact-type", "", "", "artifact type") cmd.Flags().IntVarP(&opts.concurrency, "concurrency", "", 5, "concurrency level") - cmd.MarkFlagRequired("artifact-type") + _ = cmd.MarkFlagRequired("artifact-type") opts.EnableDistributionSpecFlag() option.ApplyFlags(&opts, cmd.Flags()) return cmd diff --git a/cmd/oras/root/discover.go b/cmd/oras/root/discover.go index d853e77d2..b7dd372ae 100644 --- a/cmd/oras/root/discover.go +++ b/cmd/oras/root/discover.go @@ -133,7 +133,7 @@ func runDiscover(ctx context.Context, opts discoverOptions) error { fmt.Println("Digest:", desc.Digest) if len(refs) > 0 { fmt.Println() - printDiscoveredReferrersTable(refs, opts.Verbose) + _ = printDiscoveredReferrersTable(refs, opts.Verbose) } return nil } @@ -171,7 +171,7 @@ func fetchAllReferrers(ctx context.Context, repo oras.ReadOnlyGraphTarget, desc return nil } -func printDiscoveredReferrersTable(refs []ocispec.Descriptor, verbose bool) { +func printDiscoveredReferrersTable(refs []ocispec.Descriptor, verbose bool) error { typeNameTitle := "Artifact Type" typeNameLength := len(typeNameTitle) for _, ref := range refs { @@ -188,9 +188,12 @@ func printDiscoveredReferrersTable(refs []ocispec.Descriptor, verbose bool) { for _, ref := range refs { print(ref.ArtifactType, ref.Digest) if verbose { - printJSON(ref) + if err := printJSON(ref); err != nil { + return fmt.Errorf("Error printing JSON: %w", err) + } } } + return nil } // printDiscoveredReferrersJSON prints referrer list in JSON equivalent to the diff --git a/internal/cache/target_test.go b/internal/cache/target_test.go index cbc7cd0b0..508116774 100644 --- a/internal/cache/target_test.go +++ b/internal/cache/target_test.go @@ -177,7 +177,9 @@ func TestProxy_fetchReference(t *testing.T) { w.WriteHeader(http.StatusOK) // write data to the response if this is the first request if requestCount == 1 { - w.Write(blob) + if _, err := w.Write(blob); err != nil { + t.Errorf("Error writing blobs: %v", err) + } } atomic.AddInt64(&successCount, 1) return diff --git a/internal/graph/graph_test.go b/internal/graph/graph_test.go index 50eb0e8e0..599307fa2 100644 --- a/internal/graph/graph_test.go +++ b/internal/graph/graph_test.go @@ -121,7 +121,7 @@ func TestReferrers(t *testing.T) { index ) anno := map[string]string{"test": "foo"} - appendBlob(ocispec.MediaTypeArtifactManifest, []byte("subject content")) + appendBlob(ocispec.MediaTypeArtifactManifest, []byte(`{"name":"subject content"}`)) imageType := "test.image" appendBlob(imageType, []byte("config content")) generateImage(nil, nil, descs[imgConfig], descs[blob]) @@ -139,7 +139,9 @@ func TestReferrers(t *testing.T) { referrers := []ocispec.Descriptor{descs[image], descs[image]} memory := memory.New() for i := range descs { - memory.Push(ctx, descs[i], bytes.NewReader(blobs[i])) + if err := memory.Push(ctx, descs[i], bytes.NewReader(blobs[i])); err != nil { + t.Errorf("Error pushing %v\n", err) + } } finder := &predecessorFinder{Store: memory} @@ -244,7 +246,7 @@ func TestSuccessors(t *testing.T) { artifact index ) - appendBlob(ocispec.MediaTypeArtifactManifest, []byte("subject content")) + appendBlob(ocispec.MediaTypeArtifactManifest, []byte(`{"name":"subject content"}`)) imageType := "test.image" appendBlob(imageType, []byte("config content")) generateImage(&descs[subject], ocispec.MediaTypeImageManifest, descs[config]) @@ -255,7 +257,9 @@ func TestSuccessors(t *testing.T) { memory := memory.New() ctx := context.Background() for i := range descs { - memory.Push(ctx, descs[i], bytes.NewReader(blobs[i])) + if err := memory.Push(ctx, descs[i], bytes.NewReader(blobs[i])); err != nil { + t.Errorf("Error pushing %v\n", err) + } } fetcher := &fetcher{Fetcher: memory} @@ -341,7 +345,7 @@ func TestFindReferrerPredecessors(t *testing.T) { image ) var anno map[string]string - appendBlob(ocispec.MediaTypeArtifactManifest, []byte("subject content")) + appendBlob(ocispec.MediaTypeArtifactManifest, []byte(`{"name":"subject content"}`)) imageType := "test.image" appendBlob(imageType, []byte("config content")) generateImage(&descs[subject], anno, descs[imgConfig]) @@ -353,7 +357,9 @@ func TestFindReferrerPredecessors(t *testing.T) { referrers := []ocispec.Descriptor{descs[image], descs[image]} memory := memory.New() for i := range descs { - memory.Push(ctx, descs[i], bytes.NewReader(blobs[i])) + if err := memory.Push(ctx, descs[i], bytes.NewReader(blobs[i])); err != nil { + t.Errorf("Error pushing %v\n", err) + } } finder := &predecessorFinder{Store: memory} type args struct { @@ -367,7 +373,6 @@ func TestFindReferrerPredecessors(t *testing.T) { want []ocispec.Descriptor wantErr bool }{ - {"should failed to get referrers", args{ctx, &errLister{}, ocispec.Descriptor{}}, nil, true}, {"should failed to get predecessor", args{ctx, &errFinder{}, ocispec.Descriptor{}}, nil, true}, {"should return referrers when target is a referrer lister", args{ctx, &refLister{referrers: referrers}, ocispec.Descriptor{}}, referrers, false},