-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xiaoxuan Wang <[email protected]>
- Loading branch information
1 parent
6ec43e7
commit af853b2
Showing
6 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package oras_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"oras.land/oras-go/v2" | ||
"oras.land/oras-go/v2/content/file" | ||
"oras.land/oras-go/v2/registry/remote" | ||
"oras.land/oras-go/v2/registry/remote/auth" | ||
"oras.land/oras-go/v2/registry/remote/retry" | ||
) | ||
|
||
func pullFiles() error { | ||
// 0. Create a file store | ||
fs, err := file.New("/tmp/") | ||
if err != nil { | ||
return err | ||
} | ||
defer fs.Close() | ||
|
||
// 1. Connect to a remote repository | ||
ctx := context.Background() | ||
reg := "myregistry.example.com" | ||
repo, err := remote.NewRepository(reg + "/myrepo") | ||
if err != nil { | ||
return err | ||
} | ||
// Note: The below code can be omitted if authentication is not required | ||
repo.Client = &auth.Client{ | ||
Client: retry.DefaultClient, | ||
Cache: auth.DefaultCache, | ||
Credential: auth.StaticCredential(reg, auth.Credential{ | ||
Username: "username", | ||
Password: "password", | ||
}), | ||
} | ||
|
||
// 2. Copy from the remote repository to the file store | ||
tag := "latest" | ||
manifestDescriptor, err := oras.Copy(ctx, repo, tag, fs, tag, oras.DefaultCopyOptions) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("manifest descriptor:", manifestDescriptor) | ||
return nil | ||
} | ||
|
||
func Example_pullFilesFromRemoteRepository() { | ||
if err := pullFiles(); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package oras_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"oras.land/oras-go/v2" | ||
"oras.land/oras-go/v2/content" | ||
"oras.land/oras-go/v2/content/oci" | ||
"oras.land/oras-go/v2/registry/remote" | ||
"oras.land/oras-go/v2/registry/remote/auth" | ||
"oras.land/oras-go/v2/registry/remote/retry" | ||
) | ||
|
||
func pullImageFromRemote() error { | ||
// 0. Create an OCI layout store | ||
store, err := oci.New("/tmp/oci-layout-root") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 1. Connect to a remote repository | ||
ctx := context.Background() | ||
reg := "myregistry.example.com" | ||
repo, err := remote.NewRepository(reg + "/myrepo") | ||
if err != nil { | ||
return err | ||
} | ||
// Note: The below code can be omitted if authentication is not required | ||
repo.Client = &auth.Client{ | ||
Client: retry.DefaultClient, | ||
Cache: auth.DefaultCache, | ||
Credential: auth.StaticCredential(reg, auth.Credential{ | ||
Username: "username", | ||
Password: "password", | ||
}), | ||
} | ||
|
||
// 2. Copy from the remote repository to the OCI layout store | ||
tag := "latest" | ||
manifestDescriptor, err := oras.Copy(ctx, repo, tag, store, tag, oras.DefaultCopyOptions) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("manifest pulled:", manifestDescriptor.Digest, manifestDescriptor.MediaType) | ||
|
||
// 3. Fetch from OCI layout store to verify | ||
fetched, err := content.FetchAll(ctx, store, manifestDescriptor) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("manifest content:\n%s", fetched) | ||
return nil | ||
} | ||
|
||
func Example_pullImageFromRemoteRepository() { | ||
if err := pullImageFromRemote(); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package oras_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
credentials "github.com/oras-project/oras-credentials-go" | ||
"oras.land/oras-go/v2" | ||
"oras.land/oras-go/v2/content" | ||
"oras.land/oras-go/v2/content/oci" | ||
"oras.land/oras-go/v2/registry/remote" | ||
"oras.land/oras-go/v2/registry/remote/auth" | ||
"oras.land/oras-go/v2/registry/remote/retry" | ||
) | ||
|
||
func pullImageUsingDockerCredentialStore() error { | ||
|
||
// 0. Create an OCI layout store | ||
store, err := oci.New("/tmp/oci-layout-root") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 1. Connect to a remote repository | ||
ctx := context.Background() | ||
reg := "docker.io" | ||
repo, err := remote.NewRepository(reg + "/user/my-repo") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 2. Get credentials from the docker credential store | ||
storeOpts := credentials.StoreOptions{} | ||
credStore, err := credentials.NewStoreFromDocker(storeOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Prepare the auth client for the registry and credential store | ||
repo.Client = &auth.Client{ | ||
Client: retry.DefaultClient, | ||
Cache: auth.DefaultCache, | ||
Credential: credentials.Credential(credStore), // Use the credential store | ||
} | ||
|
||
// 3. Copy from the remote repository to the OCI layout store | ||
tag := "latest" | ||
manifestDescriptor, err := oras.Copy(ctx, repo, tag, store, tag, oras.DefaultCopyOptions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("manifest pulled:", manifestDescriptor.Digest, manifestDescriptor.MediaType) | ||
|
||
// 3. Fetch from OCI layout store to verify | ||
fetched, err := content.FetchAll(ctx, store, manifestDescriptor) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("manifest content:\n%s", fetched) | ||
return nil | ||
} | ||
|
||
func Example_pullImageUsingDockerCredentialStore() { | ||
if err := pullImageUsingDockerCredentialStore(); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package oras_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras-go/v2" | ||
"oras.land/oras-go/v2/content/file" | ||
"oras.land/oras-go/v2/registry/remote" | ||
"oras.land/oras-go/v2/registry/remote/auth" | ||
"oras.land/oras-go/v2/registry/remote/retry" | ||
) | ||
|
||
func pushFiles() error { | ||
// 0. Create a file store | ||
fs, err := file.New("/tmp/") | ||
if err != nil { | ||
return err | ||
} | ||
defer fs.Close() | ||
ctx := context.Background() | ||
|
||
// 1. Add files to a file store | ||
mediaType := "example/file" | ||
fileNames := []string{"/tmp/myfile"} | ||
fileDescriptors := make([]v1.Descriptor, 0, len(fileNames)) | ||
for _, name := range fileNames { | ||
fileDescriptor, err := fs.Add(ctx, name, mediaType, "") | ||
if err != nil { | ||
return err | ||
} | ||
fileDescriptors = append(fileDescriptors, fileDescriptor) | ||
fmt.Printf("file descriptor for %s: %v\n", name, fileDescriptor) | ||
} | ||
|
||
// 2. Pack the files and tag the packed manifest | ||
artifactType := "example/files" | ||
manifestDescriptor, err := oras.Pack(ctx, fs, artifactType, fileDescriptors, oras.PackOptions{ | ||
PackImageManifest: true, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("manifest descriptor:", manifestDescriptor) | ||
|
||
tag := "latest" | ||
if err = fs.Tag(ctx, manifestDescriptor, tag); err != nil { | ||
return err | ||
} | ||
|
||
// 3. Connect to a remote repository | ||
reg := "myregistry.example.com" | ||
repo, err := remote.NewRepository(reg + "/myrepo") | ||
if err != nil { | ||
panic(err) | ||
} | ||
// Note: The below code can be omitted if authentication is not required | ||
repo.Client = &auth.Client{ | ||
Client: retry.DefaultClient, | ||
Cache: auth.DefaultCache, | ||
Credential: auth.StaticCredential(reg, auth.Credential{ | ||
Username: "username", | ||
Password: "password", | ||
}), | ||
} | ||
|
||
// 3. Copy from the file store to the remote repository | ||
_, err = oras.Copy(ctx, fs, tag, repo, tag, oras.DefaultCopyOptions) | ||
return err | ||
} | ||
|
||
func Example_pushFilesToRemoteRepository() { | ||
if err := pushFiles(); err != nil { | ||
panic(err) | ||
} | ||
} |