Skip to content

Commit f7a6126

Browse files
docs: add more practical examples (#940)
Fixes: #817 Signed-off-by: Xiaoxuan Wang <[email protected]>
1 parent 0c12035 commit f7a6126

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

example_copy_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
"oras.land/oras-go/v2/content/oci"
3737
"oras.land/oras-go/v2/internal/spec"
3838
"oras.land/oras-go/v2/registry/remote"
39+
"oras.land/oras-go/v2/registry/remote/auth"
40+
"oras.land/oras-go/v2/registry/remote/retry"
3941
)
4042

4143
var exampleMemoryStore oras.Target
@@ -408,3 +410,57 @@ func Example_extendedCopyArtifactAndReferrersFromRepository() {
408410
// Output:
409411
// sha256:f396bc4d300934a39ca28ab0d5ac8a3573336d7d63c654d783a68cd1e2057662
410412
}
413+
414+
// ExampleExtendedCopyArtifactAndReferrersToRepository is an example of pushing
415+
// an artifact and its referrer to a remote repository.
416+
func Example_extendedCopyArtifactAndReferrersToRepository() {
417+
// 0. Assemble the referenced manifest in memory with tag "v1"
418+
ctx := context.Background()
419+
src := memory.New()
420+
manifestDescriptor, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "application/vnd.example+type", oras.PackManifestOptions{})
421+
if err != nil {
422+
panic(err)
423+
}
424+
fmt.Println("created manifest: ", manifestDescriptor)
425+
426+
tag := "v1"
427+
err = src.Tag(ctx, manifestDescriptor, tag)
428+
if err != nil {
429+
panic(err)
430+
}
431+
fmt.Println("tagged manifest: ", manifestDescriptor.Digest)
432+
433+
// 1. Assemble the referrer manifest in memory
434+
referrerPackOpts := oras.PackManifestOptions{
435+
Subject: &manifestDescriptor,
436+
}
437+
referrerDescriptor, err := oras.PackManifest(ctx, src, oras.PackManifestVersion1_1, "sbom/example", referrerPackOpts)
438+
if err != nil {
439+
panic(err)
440+
}
441+
fmt.Println("created referrer: ", referrerDescriptor)
442+
443+
// 2. Connect to a remote repository with basic authentication
444+
registry := "myregistry.example.com"
445+
repository := "myrepo"
446+
repo, err := remote.NewRepository(fmt.Sprintf("%s/%s", registry, repository))
447+
if err != nil {
448+
panic(err)
449+
}
450+
// Note: The below code can be omitted if authentication is not required.
451+
repo.Client = &auth.Client{
452+
Client: retry.DefaultClient,
453+
Cache: auth.NewCache(),
454+
Credential: auth.StaticCredential(registry, auth.Credential{
455+
Username: "username",
456+
Password: "password",
457+
}),
458+
}
459+
460+
// 3. Push the manifest and its referrer to the remote repository
461+
desc, err := oras.ExtendedCopy(ctx, src, tag, repo, "", oras.DefaultExtendedCopyOptions)
462+
if err != nil {
463+
panic(err)
464+
}
465+
fmt.Println("pushed: ", desc.Digest)
466+
}

example_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,54 @@ func Example_pushFilesToRemoteRepository() {
202202
panic(err)
203203
}
204204
}
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+
}

0 commit comments

Comments
 (0)