| 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 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 23 |
"oras.land/oras-go/v2" |
| 24 |
"oras.land/oras-go/v2/content" |
| 25 |
"oras.land/oras-go/v2/content/memory" |
| 26 |
) |
| 27 |
|
| 28 |
// ExampleImageV11 demonstrates packing an OCI Image Manifest as defined in |
| 29 |
// image-spec v1.1.1. |
| 30 |
func ExamplePackManifest_imageV11() { |
| 31 |
// 0. Create a storage |
| 32 |
store := memory.New() |
| 33 |
|
| 34 |
// 1. Set optional parameters |
| 35 |
opts := oras.PackManifestOptions{ |
| 36 |
ManifestAnnotations: map[string]string{ |
| 37 |
// this time stamp will be automatically generated if not specified |
| 38 |
// use a fixed value here to make the pack result reproducible |
| 39 |
ocispec.AnnotationCreated: "2000-01-01T00:00:00Z", |
| 40 |
}, |
| 41 |
} |
| 42 |
ctx := context.Background() |
| 43 |
|
| 44 |
// 2. Pack a manifest |
| 45 |
artifactType := "application/vnd.example+type" |
| 46 |
manifestDesc, err := oras.PackManifest(ctx, store, oras.PackManifestVersion1_1, artifactType, opts) |
| 47 |
if err != nil { |
| 48 |
panic(err) |
| 49 |
} |
| 50 |
fmt.Println("Manifest descriptor:", manifestDesc) |
| 51 |
|
| 52 |
// 3. Verify the packed manifest |
| 53 |
manifestData, err := content.FetchAll(ctx, store, manifestDesc) |
| 54 |
if err != nil { |
| 55 |
panic(err) |
| 56 |
} |
| 57 |
fmt.Println("Manifest content:", string(manifestData)) |
| 58 |
|
| 59 |
// Output: |
| 60 |
// Manifest descriptor: {application/vnd.oci.image.manifest.v1+json sha256:c259a195a48d8029d75449579c81269ca6225cd5b57d36073a7de6458afdfdbd 528 [] map[org.opencontainers.image.created:2000-01-01T00:00:00Z] [] <nil> application/vnd.example+type} |
| 61 |
// Manifest content: {"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","artifactType":"application/vnd.example+type","config":{"mediaType":"application/vnd.oci.empty.v1+json","digest":"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a","size":2,"data":"e30="},"layers":[{"mediaType":"application/vnd.oci.empty.v1+json","digest":"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a","size":2,"data":"e30="}],"annotations":{"org.opencontainers.image.created":"2000-01-01T00:00:00Z"}} |
| 62 |
} |
| 63 |
|
| 64 |
// ExampleImageV10 demonstrates packing an OCI Image Manifest as defined in |
| 65 |
// image-spec v1.0.2. |
| 66 |
func ExamplePackManifest_imageV10() { |
| 67 |
// 0. Create a storage |
| 68 |
store := memory.New() |
| 69 |
|
| 70 |
// 1. Set optional parameters |
| 71 |
opts := oras.PackManifestOptions{ |
| 72 |
ManifestAnnotations: map[string]string{ |
| 73 |
// this time stamp will be automatically generated if not specified |
| 74 |
// use a fixed value here to make the pack result reproducible |
| 75 |
ocispec.AnnotationCreated: "2000-01-01T00:00:00Z", |
| 76 |
}, |
| 77 |
} |
| 78 |
ctx := context.Background() |
| 79 |
|
| 80 |
// 2. Pack a manifest |
| 81 |
artifactType := "application/vnd.example+type" |
| 82 |
manifestDesc, err := oras.PackManifest(ctx, store, oras.PackManifestVersion1_0, artifactType, opts) |
| 83 |
if err != nil { |
| 84 |
panic(err) |
| 85 |
} |
| 86 |
fmt.Println("Manifest descriptor:", manifestDesc) |
| 87 |
|
| 88 |
// 3. Verify the packed manifest |
| 89 |
manifestData, err := content.FetchAll(ctx, store, manifestDesc) |
| 90 |
if err != nil { |
| 91 |
panic(err) |
| 92 |
} |
| 93 |
fmt.Println("Manifest content:", string(manifestData)) |
| 94 |
|
| 95 |
// Output: |
| 96 |
// Manifest descriptor: {application/vnd.oci.image.manifest.v1+json sha256:da221a11559704e4971c3dcf6564303707a333c8de8cb5475fc48b0072b36c19 308 [] map[org.opencontainers.image.created:2000-01-01T00:00:00Z] [] <nil> application/vnd.example+type} |
| 97 |
// Manifest content: {"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","config":{"mediaType":"application/vnd.example+type","digest":"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a","size":2},"layers":[],"annotations":{"org.opencontainers.image.created":"2000-01-01T00:00:00Z"}} |
| 98 |
} |