-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtypes.go
More file actions
167 lines (133 loc) · 5.17 KB
/
types.go
File metadata and controls
167 lines (133 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"encoding/json"
"fmt"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"os"
)
/*
type ocispec.Index struct {
// SchemaVersion is the image manifest schema that this image follows
SchemaVersion int `json:"schemaVersion"`
// MediaType specifies the type of this document data structure e.g. `application/vnd.oci.image.index.v1+json`
MediaType string `json:"mediaType,omitempty"`
// ArtifactType specifies the IANA media type of artifact when the manifest is used for an artifact.
ArtifactType string `json:"artifactType,omitempty"`
// Manifests references platform specific manifests.
Manifests []Descriptor `json:"manifests"`
// Subject is an optional link from the image manifest to another manifest forming an association between the image manifest and the other manifest.
Subject *Descriptor `json:"subject,omitempty"`
// Annotations contains arbitrary metadata for the image index.
Annotations map[string]string `json:"annotations,omitempty"`
}
type ocispec.Manifest struct {
// SchemaVersion is the image manifest schema that this image follows
SchemaVersion int `json:"schemaVersion"`
// MediaType specifies the type of this document data structure e.g. `application/vnd.oci.image.manifest.v1+json`
MediaType string `json:"mediaType,omitempty"`
// ArtifactType specifies the IANA media type of artifact when the manifest is used for an artifact.
ArtifactType string `json:"artifactType,omitempty"`
// Config references a configuration object for a container, by digest.
// The referenced configuration object is a JSON blob that the runtime uses to set up the container.
Config Descriptor `json:"config"`
// Layers is an indexed list of layers referenced by the manifest.
Layers []Descriptor `json:"layers"`
// Subject is an optional link from the image manifest to another manifest forming an association between the image manifest and the other manifest.
Subject *Descriptor `json:"subject,omitempty"`
// Annotations contains arbitrary metadata for the image manifest.
Annotations map[string]string `json:"annotations,omitempty"`
}
type ocispec.Descriptor struct {
// MediaType is the media type of the object this schema refers to.
MediaType string `json:"mediaType"`
// Digest is the digest of the targeted content.
Digest digest.Digest `json:"digest"`
// Size specifies the size in bytes of the blob.
Size int64 `json:"size"`
// URLs specifies a list of URLs from which this object MAY be downloaded
URLs []string `json:"urls,omitempty"`
// Annotations contains arbitrary metadata relating to the targeted content.
Annotations map[string]string `json:"annotations,omitempty"`
// Data is an embedding of the targeted content. This is encoded as a base64
// string when marshalled to JSON (automatically, by encoding/json). If
// present, Data can be used directly to avoid fetching the targeted content.
Data []byte `json:"data,omitempty"`
// Platform describes the platform which the image in the manifest runs on.
//
// This should only be used when referring to a manifest.
Platform *Platform `json:"platform,omitempty"`
// ArtifactType is the IANA media type of this artifact.
ArtifactType string `json:"artifactType,omitempty"`
}
*/
type MediaTyped struct {
// SchemaVersion is the image manifest schema that this image follows
SchemaVersion int `json:"schemaVersion"`
// MediaType specifies the type of this document data structure e.g. `application/vnd.oci.image.index.v1+json`
MediaType string `json:"mediaType"`
}
func ParseMediaTypedBytes(content []byte) (*MediaTyped, error) {
var mt MediaTyped
err := json.Unmarshal(content, &mt)
if err != nil {
return nil, fmt.Errorf("%w while parsing: %v", err, string(content))
}
return &mt, nil
}
func ParseMediaTypedFile(filename string) (*MediaTyped, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return ParseMediaTypedBytes(content)
}
func ParseIndexBytes(content []byte) (*ocispec.Index, error) {
var index ocispec.Index
err := json.Unmarshal(content, &index)
if err != nil {
return nil, fmt.Errorf("%w while parsing: %v", err, string(content))
}
return &index, nil
}
func ParseIndexFile(filename string) (*ocispec.Index, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return ParseIndexBytes(content)
}
func ParseManifestBytes(content []byte) (*ocispec.Manifest, error) {
var manifest ocispec.Manifest
err := json.Unmarshal(content, &manifest)
if err != nil {
return nil, fmt.Errorf("%w while parsing: %v", err, string(content))
}
return &manifest, nil
}
func ParseManifestFile(filename string) (*ocispec.Manifest, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return ParseManifestBytes(content)
}
func IsIndexType(mediaType string) bool {
switch mediaType {
case "application/vnd.oci.image.index.v1+json":
return true
case "application/vnd.docker.distribution.manifest.list.v2+json":
return true
default:
return false
}
}
func IsManifestType(mediaType string) bool {
switch mediaType {
case "application/vnd.oci.image.manifest.v1+json":
return true
case "application/vnd.docker.distribution.manifest.v2+json":
return true
default:
return false
}
}