Debian Codegraph @

example_test.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 oras_test
17
18 import (
19 "context"
20 "fmt"
21
22 v1 "github.com/opencontainers/image-spec/specs-go/v1"
23 oras "oras.land/oras-go/v2"
24 "oras.land/oras-go/v2/content/file"
25 "oras.land/oras-go/v2/content/oci"
26 "oras.land/oras-go/v2/registry/remote"
27 "oras.land/oras-go/v2/registry/remote/auth"
28 "oras.land/oras-go/v2/registry/remote/credentials"
29 "oras.land/oras-go/v2/registry/remote/retry"
30 )
31
32 // ExamplePullFilesFromRemoteRepository gives an example of pulling files from
33 // a remote repository to the local file system.
34 func Example_pullFilesFromRemoteRepository() {
35 // 0. Create a file store
36 fs, err := file.New("/tmp/")
37 if err != nil {
38 panic(err)
39 }
40 defer fs.Close()
41
42 // 1. Connect to a remote repository
43 ctx := context.Background()
44 reg := "myregistry.example.com"
45 repo, err := remote.NewRepository(reg + "/myrepo")
46 if err != nil {
47 panic(err)
48 }
49 // Note: The below code can be omitted if authentication is not required
50 repo.Client = &auth.Client{
51 Client: retry.DefaultClient,
52 Cache: auth.NewCache(),
53 Credential: auth.StaticCredential(reg, auth.Credential{
54 Username: "username",
55 Password: "password",
56 }),
57 }
58
59 // 2. Copy from the remote repository to the file store
60 tag := "latest"
61 manifestDescriptor, err := oras.Copy(ctx, repo, tag, fs, tag, oras.DefaultCopyOptions)
62 if err != nil {
63 panic(err)
64 }
65 fmt.Println("manifest descriptor:", manifestDescriptor)
66 }
67
68 // ExamplePullImageFromRemoteRepository gives an example of pulling an image
69 // from a remote repository to an OCI Image layout folder.
70 func Example_pullImageFromRemoteRepository() {
71 // 0. Create an OCI layout store
72 store, err := oci.New("/tmp/oci-layout-root")
73 if err != nil {
74 panic(err)
75 }
76
77 // 1. Connect to a remote repository
78 ctx := context.Background()
79 reg := "myregistry.example.com"
80 repo, err := remote.NewRepository(reg + "/myrepo")
81 if err != nil {
82 panic(err)
83 }
84 // Note: The below code can be omitted if authentication is not required
85 repo.Client = &auth.Client{
86 Client: retry.DefaultClient,
87 Cache: auth.NewCache(),
88 Credential: auth.StaticCredential(reg, auth.Credential{
89 Username: "username",
90 Password: "password",
91 }),
92 }
93
94 // 2. Copy from the remote repository to the OCI layout store
95 tag := "latest"
96 manifestDescriptor, err := oras.Copy(ctx, repo, tag, store, tag, oras.DefaultCopyOptions)
97 if err != nil {
98 panic(err)
99 }
100 fmt.Println("manifest descriptor:", manifestDescriptor)
101 }
102
103 // ExamplePullImageUsingDockerCredentials gives an example of pulling an image
104 // from a remote repository to an OCI Image layout folder using Docker
105 // credentials.
106 func Example_pullImageUsingDockerCredentials() {
107 // 0. Create an OCI layout store
108 store, err := oci.New("/tmp/oci-layout-root")
109 if err != nil {
110 panic(err)
111 }
112
113 // 1. Connect to a remote repository
114 ctx := context.Background()
115 reg := "docker.io"
116 repo, err := remote.NewRepository(reg + "/user/my-repo")
117 if err != nil {
118 panic(err)
119 }
120
121 // prepare authentication using Docker credentials
122 storeOpts := credentials.StoreOptions{}
123 credStore, err := credentials.NewStoreFromDocker(storeOpts)
124 if err != nil {
125 panic(err)
126 }
127 repo.Client = &auth.Client{
128 Client: retry.DefaultClient,
129 Cache: auth.NewCache(),
130 Credential: credentials.Credential(credStore), // Use the credentials store
131 }
132
133 // 2. Copy from the remote repository to the OCI layout store
134 tag := "latest"
135 manifestDescriptor, err := oras.Copy(ctx, repo, tag, store, tag, oras.DefaultCopyOptions)
136 if err != nil {
137 panic(err)
138 }
139
140 fmt.Println("manifest pulled:", manifestDescriptor.Digest, manifestDescriptor.MediaType)
141 }
142
143 // ExamplePushFilesToRemoteRepository gives an example of pushing local files
144 // to a remote repository.
145 func Example_pushFilesToRemoteRepository() {
146 // 0. Create a file store
147 fs, err := file.New("/tmp/")
148 if err != nil {
149 panic(err)
150 }
151 defer fs.Close()
152 ctx := context.Background()
153
154 // 1. Add files to the file store
155 mediaType := "application/vnd.test.file"
156 fileNames := []string{"/tmp/myfile"}
157 fileDescriptors := make([]v1.Descriptor, 0, len(fileNames))
158 for _, name := range fileNames {
159 fileDescriptor, err := fs.Add(ctx, name, mediaType, "")
160 if err != nil {
161 panic(err)
162 }
163 fileDescriptors = append(fileDescriptors, fileDescriptor)
164 fmt.Printf("file descriptor for %s: %v\n", name, fileDescriptor)
165 }
166
167 // 2. Pack the files and tag the packed manifest
168 artifactType := "application/vnd.test.artifact"
169 opts := oras.PackManifestOptions{
170 Layers: fileDescriptors,
171 }
172 manifestDescriptor, err := oras.PackManifest(ctx, fs, oras.PackManifestVersion1_1, artifactType, opts)
173 if err != nil {
174 panic(err)
175 }
176 fmt.Println("manifest descriptor:", manifestDescriptor)
177
178 tag := "latest"
179 if err = fs.Tag(ctx, manifestDescriptor, tag); err != nil {
180 panic(err)
181 }
182
183 // 3. Connect to a remote repository
184 reg := "myregistry.example.com"
185 repo, err := remote.NewRepository(reg + "/myrepo")
186 if err != nil {
187 panic(err)
188 }
189 // Note: The below code can be omitted if authentication is not required
190 repo.Client = &auth.Client{
191 Client: retry.DefaultClient,
192 Cache: auth.NewCache(),
193 Credential: auth.StaticCredential(reg, auth.Credential{
194 Username: "username",
195 Password: "password",
196 }),
197 }
198
199 // 4. Copy from the file store to the remote repository
200 _, err = oras.Copy(ctx, fs, tag, repo, tag, oras.DefaultCopyOptions)
201 if err != nil {
202 panic(err)
203 }
204 }
205
206 // ExampleAttachBlobToRemoteRepository gives an example of attaching a blob to an
207 // existing artifact in a remote repository. The blob is packed as a manifest whose
208 // subject is the existing artifact.
209 func Example_attachBlobToRemoteRepository() {
210 // 0. Connect to a remote repository with basic authentication
211 registry := "myregistry.example.com"
212 repository := "myrepo"
213 repo, err := remote.NewRepository(fmt.Sprintf("%s/%s", registry, repository))
214 if err != nil {
215 panic(err)
216 }
217 // Note: The below code can be omitted if authentication is not required.
218 repo.Client = &auth.Client{
219 Client: retry.DefaultClient,
220 Cache: auth.NewCache(),
221 Credential: auth.StaticCredential(registry, auth.Credential{
222 Username: "username",
223 Password: "password",
224 }),
225 }
226
227 // 1. Resolve the subject descriptor
228 ctx := context.Background()
229 subjectDescriptor, err := repo.Resolve(ctx, "sha256:f3a0356fe9f82b925c2f15106d3932252f36c1c56fd35be6c369d274f433d177")
230 if err != nil {
231 panic(err)
232 }
233
234 // 2. Prepare the blob to be attached
235 blob := []byte("example blob")
236
237 // 3. Push the blob to the repository
238 blobDescriptor, err := oras.PushBytes(ctx, repo, v1.MediaTypeImageLayer, blob)
239 if err != nil {
240 panic(err)
241 }
242 fmt.Println("pushed the blob to the repository")
243
244 // 4. Pack the blob as a manifest with version v1.1 and push it to the repository
245 packOpts := oras.PackManifestOptions{
246 Layers: []v1.Descriptor{blobDescriptor},
247 Subject: &subjectDescriptor,
248 }
249 artifactType := "application/vnd.example+type"
250 referrerDescriptor, err := oras.PackManifest(ctx, repo, oras.PackManifestVersion1_1, artifactType, packOpts)
251 if err != nil {
252 panic(err)
253 }
254 fmt.Printf("attached %s to %s\n", referrerDescriptor.Digest, subjectDescriptor.Digest)
255 }