Skip to content

Commit

Permalink
chore: fix lint warning (oras-project#1001)
Browse files Browse the repository at this point in the history
Signed-off-by: Anurag <[email protected]>
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
kranurag7 authored and qweeah committed Jul 21, 2023
1 parent fc309cf commit e07c2c5
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 24 deletions.
5 changes: 3 additions & 2 deletions cmd/oras/internal/display/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/internal/option/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
16 changes: 10 additions & 6 deletions cmd/oras/internal/option/packer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"))
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/oras/internal/option/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/oras/internal/option/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions cmd/oras/root/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion internal/cache/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions internal/graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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}

Expand Down Expand Up @@ -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])
Expand All @@ -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}

Expand Down Expand Up @@ -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])
Expand All @@ -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 {
Expand All @@ -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},
Expand Down

0 comments on commit e07c2c5

Please sign in to comment.