Skip to content

Commit

Permalink
Added the changes and rest of the testcases
Browse files Browse the repository at this point in the history
Signed-off-by: 7h3-3mp7y-m4n <[email protected]>
  • Loading branch information
7h3-3mp7y-m4n committed Oct 17, 2024
1 parent 0c8b008 commit b417b1e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
28 changes: 26 additions & 2 deletions uriget/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,31 @@ func ExampleGetFile_oci() {
fmt.Println("failed to pull OCI image:", err)
return
}
fmt.Println(len(buff) > 0, err)
fmt.Println(len(buff) > 0)
// Output:
// true <nil>
// true
}

func ExampleGetFile_ociNoTag() {
testUrl := "oci://ghcr.io/score-spec/score-compose"
buff, err := GetFile(context.Background(), testUrl)
if err != nil {
fmt.Println("failed to pull OCI image:", err)
return
}
fmt.Println(len(buff) > 0)
// Output:
// true
}

func ExampleGetFile_ociWithDigest() {
testUrl := "oci://ghcr.io/score-spec/score-compose@sha256:f3d8d5485a751cbdc91e073df1b6fbcde83f85a86ee3bc7d53e05b00452baedd"
buff, err := GetFile(context.Background(), testUrl)
if err != nil {
fmt.Println("failed to pull OCI image:", err)
return
}
fmt.Println(len(buff) > 0)
// Output:
// true
}
24 changes: 8 additions & 16 deletions uriget/uriget.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/oci"
"oras.land/oras-go/v2/registry"
"oras.land/oras-go/v2/registry/remote"
)

Expand Down Expand Up @@ -241,30 +242,21 @@ func (o *options) getGit(ctx context.Context, u *url.URL) ([]byte, error) {
}

func (o *options) getOci(ctx context.Context, u *url.URL) ([]byte, error) {
parts := strings.Split(u.Host+u.Path, "/")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid OCI URL format")
}
registry := parts[0]
repo := strings.Join(parts[1:len(parts)-1], "/")
tag := "latest"
lastPart := parts[len(parts)-1]
if strings.Contains(lastPart, ":") {
split := strings.Split(lastPart, ":")
repo = strings.Join(parts[1:len(parts)-1], "/") + "/" + split[0]
tag = split[1]
ref, err := registry.ParseReference(u.Host + u.Path)
if err != nil {
return nil, fmt.Errorf("can't parse artifact URL into a valid reference: %w", err)
}
store, err := oci.New(o.tempDir)
if err != nil {
return nil, fmt.Errorf("failed to create OCI layout store: %w", err)
}
repoUrl := fmt.Sprintf("%s/%s", registry, repo)
remoteRepo, err := remote.NewRepository(repoUrl)
remoteRepo, err := remote.NewRepository(ref.String())
if err != nil {
return nil, fmt.Errorf("failed to connect to remote repository: %w", err)
}
if strings.HasPrefix(repoUrl, "localhost:") || strings.HasPrefix(repoUrl, "127.0.0.1:") {
remoteRepo.PlainHTTP = true
tag := "latest"
if ref.Reference != "" {
tag = ref.Reference
}
manifestDescriptor, err := oras.Copy(ctx, remoteRepo, tag, store, tag, oras.DefaultCopyOptions)
if err != nil {
Expand Down

0 comments on commit b417b1e

Please sign in to comment.