Skip to content

Commit

Permalink
added error checks
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
  • Loading branch information
Xiaoxuan Wang committed Sep 24, 2024
1 parent 9134a8f commit 5104cc9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmd/oras/root/manifest/index/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ func createIndex(cmd *cobra.Command, opts createOptions) error {
case "-":
err = opts.Output(os.Stdout, indexBytes)
default:
displayMetadata.OnCompleted(desc.Digest)
if err := displayMetadata.OnCompleted(desc.Digest); err != nil {
return err

Check warning on line 144 in cmd/oras/root/manifest/index/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/create.go#L144

Added line #L144 was not covered by tests
}
err = os.WriteFile(opts.outputPath, indexBytes, 0666)
}
return err
Expand Down
44 changes: 33 additions & 11 deletions cmd/oras/root/manifest/index/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,38 @@ func updateIndex(cmd *cobra.Command, opts updateOptions) error {
}
desc := content.NewDescriptorFromBytes(index.MediaType, indexBytes)

displayStatus.OnIndexUpdated(desc.Digest)
if err := displayStatus.OnIndexUpdated(desc.Digest); err != nil {
return err

Check warning on line 146 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L146

Added line #L146 was not covered by tests
}
path := getPushPath(opts.RawReference, opts.Type, opts.Reference, opts.Path)
switch opts.outputPath {
case "":
err = pushIndex(ctx, displayStatus.OnIndexPushed, displayMetadata.OnCompleted, displayMetadata.OnTagged, target, desc, indexBytes, opts.Reference, opts.tags, path)
case "-":
err = opts.Output(os.Stdout, indexBytes)
default:
displayMetadata.OnCompleted(desc.Digest)
if err := displayMetadata.OnCompleted(desc.Digest); err != nil {
return err

Check warning on line 156 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L156

Added line #L156 was not covered by tests
}
err = os.WriteFile(opts.outputPath, indexBytes, 0666)
}
return err
}

func fetchIndex(ctx context.Context, handler status.ManifestIndexUpdateHandler, target oras.ReadOnlyTarget, reference string) (ocispec.Index, error) {
handler.OnIndexFetching(reference)
if err := handler.OnIndexFetching(reference); err != nil {
return ocispec.Index{}, err

Check warning on line 165 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L165

Added line #L165 was not covered by tests
}
desc, content, err := oras.FetchBytes(ctx, target, reference, oras.DefaultFetchBytesOptions)
if err != nil {
return ocispec.Index{}, fmt.Errorf("could not find the index %s: %w", reference, err)
}
if !descriptor.IsIndex(desc) {
return ocispec.Index{}, fmt.Errorf("%s is not an index", reference)
}
handler.OnIndexFetched(reference, desc.Digest)
if err := handler.OnIndexFetched(reference, desc.Digest); err != nil {
return ocispec.Index{}, err

Check warning on line 175 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L175

Added line #L175 was not covered by tests
}
var index ocispec.Index
if err := json.Unmarshal(content, &index); err != nil {
return ocispec.Index{}, err
Expand All @@ -175,44 +183,56 @@ func fetchIndex(ctx context.Context, handler status.ManifestIndexUpdateHandler,

func addManifests(ctx context.Context, handler status.ManifestIndexUpdateHandler, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, addArguments []string) ([]ocispec.Descriptor, error) {
for _, manifestRef := range addArguments {
handler.OnManifestFetching(manifestRef)
if err := handler.OnManifestFetching(manifestRef); err != nil {
return nil, err

Check warning on line 187 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L187

Added line #L187 was not covered by tests
}
desc, content, err := oras.FetchBytes(ctx, target, manifestRef, oras.DefaultFetchBytesOptions)
if err != nil {
return nil, fmt.Errorf("could not find the manifest %s: %w", manifestRef, err)
}
if !descriptor.IsManifest(desc) {
return nil, fmt.Errorf("%s is not a manifest", manifestRef)
}
handler.OnManifestFetched(manifestRef, desc.Digest)
if err := handler.OnManifestFetched(manifestRef, desc.Digest); err != nil {
return nil, err

Check warning on line 197 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L197

Added line #L197 was not covered by tests
}
if descriptor.IsImageManifest(desc) {
desc.Platform, err = getPlatform(ctx, target, content)
if err != nil {
return nil, err
}
}
manifests = append(manifests, desc)
handler.OnManifestAdded(manifestRef, desc.Digest)
if err := handler.OnManifestAdded(manifestRef, desc.Digest); err != nil {
return nil, err

Check warning on line 207 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L207

Added line #L207 was not covered by tests
}
}
return manifests, nil
}

func mergeIndexes(ctx context.Context, handler status.ManifestIndexUpdateHandler, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, mergeArguments []string) ([]ocispec.Descriptor, error) {
for _, indexRef := range mergeArguments {
handler.OnIndexFetching(indexRef)
if err := handler.OnIndexFetching(indexRef); err != nil {
return nil, err

Check warning on line 216 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L216

Added line #L216 was not covered by tests
}
desc, content, err := oras.FetchBytes(ctx, target, indexRef, oras.DefaultFetchBytesOptions)
if err != nil {
return nil, fmt.Errorf("could not find the index %s: %w", indexRef, err)
}
if !descriptor.IsIndex(desc) {
return nil, fmt.Errorf("%s is not an index", indexRef)
}
handler.OnIndexFetched(indexRef, desc.Digest)
if err := handler.OnIndexFetched(indexRef, desc.Digest); err != nil {
return nil, err

Check warning on line 226 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L226

Added line #L226 was not covered by tests
}
var index ocispec.Index
if err := json.Unmarshal(content, &index); err != nil {
return nil, err
}
manifests = append(manifests, index.Manifests...)
handler.OnIndexMerged(indexRef, desc.Digest)
if err := handler.OnIndexMerged(indexRef, desc.Digest); err != nil {
return nil, err

Check warning on line 234 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L234

Added line #L234 was not covered by tests
}
}
return manifests, nil
}
Expand All @@ -239,7 +259,9 @@ func doRemoveManifests(originalManifests []ocispec.Descriptor, digestToRemove ma
if !removed {
return nil, fmt.Errorf("%s does not exist in the index %s", digest, indexRef)
}
handler.OnManifestRemoved(digest)
if err := handler.OnManifestRemoved(digest); err != nil {
return nil, err

Check warning on line 263 in cmd/oras/root/manifest/index/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/update.go#L263

Added line #L263 was not covered by tests
}
}
return manifests, nil
}
Expand Down

0 comments on commit 5104cc9

Please sign in to comment.