Debian Codegraph @

internal/manifestutil/parser.go

raw
1 /*
2 Copyright The ORAS Authors.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15
16 package manifestutil
17
18 import (
19 "context"
20 "encoding/json"
21
22 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
23 "oras.land/oras-go/v2/content"
24 "oras.land/oras-go/v2/internal/docker"
25 "oras.land/oras-go/v2/internal/spec"
26 )
27
28 // Config returns the config of desc, if present.
29 func Config(ctx context.Context, fetcher content.Fetcher, desc ocispec.Descriptor) (*ocispec.Descriptor, error) {
30 switch desc.MediaType {
31 case docker.MediaTypeManifest, ocispec.MediaTypeImageManifest:
32 content, err := content.FetchAll(ctx, fetcher, desc)
33 if err != nil {
34 return nil, err
35 }
36 // OCI manifest schema can be used to marshal docker manifest
37 var manifest ocispec.Manifest
38 if err := json.Unmarshal(content, &manifest); err != nil {
39 return nil, err
40 }
41 return &manifest.Config, nil
42 default:
43 return nil, nil
44 }
45 }
46
47 // Manifest returns the manifests of desc, if present.
48 func Manifests(ctx context.Context, fetcher content.Fetcher, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
49 switch desc.MediaType {
50 case docker.MediaTypeManifestList, ocispec.MediaTypeImageIndex:
51 content, err := content.FetchAll(ctx, fetcher, desc)
52 if err != nil {
53 return nil, err
54 }
55 // OCI manifest index schema can be used to marshal docker manifest list
56 var index ocispec.Index
57 if err := json.Unmarshal(content, &index); err != nil {
58 return nil, err
59 }
60 return index.Manifests, nil
61 default:
62 return nil, nil
63 }
64 }
65
66 // Subject returns the subject of desc, if present.
67 func Subject(ctx context.Context, fetcher content.Fetcher, desc ocispec.Descriptor) (*ocispec.Descriptor, error) {
68 switch desc.MediaType {
69 case ocispec.MediaTypeImageManifest, ocispec.MediaTypeImageIndex, spec.MediaTypeArtifactManifest:
70 content, err := content.FetchAll(ctx, fetcher, desc)
71 if err != nil {
72 return nil, err
73 }
74 var manifest struct {
75 Subject *ocispec.Descriptor `json:"subject,omitempty"`
76 }
77 if err := json.Unmarshal(content, &manifest); err != nil {
78 return nil, err
79 }
80 return manifest.Subject, nil
81 default:
82 return nil, nil
83 }
84 }