github.com/dtroyer-salad/og2/v2@v2.0.0-20240412154159-c47231610877/example_test.go (about) 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 }