From fa2b7290c36116bd21233f5771d1b3ecd3bdee56 Mon Sep 17 00:00:00 2001 From: Sajay Antony Date: Thu, 4 Jan 2024 23:27:44 +0000 Subject: [PATCH] doc: add example for digest and parseReference Signed-off-by: Sajay Antony --- registry/example_parse_test.go | 32 ++++++++++++++++++++++++++++++++ registry/reference.go | 2 ++ 2 files changed, 34 insertions(+) create mode 100644 registry/example_parse_test.go diff --git a/registry/example_parse_test.go b/registry/example_parse_test.go new file mode 100644 index 00000000..75e24f6d --- /dev/null +++ b/registry/example_parse_test.go @@ -0,0 +1,32 @@ +package registry_test + +import ( + _ "crypto/sha256" + "fmt" + "log" + + "oras.land/oras-go/v2/registry" +) + +var img = "ghcr.io/oras-project/oras@sha256:601d05a48832e7946dab8f49b14953549bebf42e42f4d7973b1a5a287d77ab76" + +func ExampleParseReference() { + + ref, err := registry.ParseReference(img) + if err != nil { + log.Fatalf("%s", err) + } + + fmt.Printf("Registry: %s\n", ref.Registry) + fmt.Printf("Repository: %s\n", ref.Repository) + digest, err := ref.Digest() + if err != nil { + log.Fatalf("%s", err) + } + fmt.Printf("Digest: %v\n", digest) + + // Output: + // Registry: ghcr.io + // Repository: oras-project/oras + // Digest: sha256:601d05a48832e7946dab8f49b14953549bebf42e42f4d7973b1a5a287d77ab76 +} diff --git a/registry/reference.go b/registry/reference.go index a4d2003d..c73ed09b 100644 --- a/registry/reference.go +++ b/registry/reference.go @@ -250,6 +250,8 @@ func (r Reference) ReferenceOrDefault() string { } // Digest returns the reference as a digest. +// Make sure you import the appropriate hash implementations as described +// in https://pkg.go.dev/github.com/opencontainers/go-digest#readme-usage func (r Reference) Digest() (digest.Digest, error) { return digest.Parse(r.Reference) }