Skip to content

Commit

Permalink
added the unit test and updated the function for remote url
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 15, 2024
1 parent 7b48327 commit f372be8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
23 changes: 23 additions & 0 deletions uriget/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package uriget
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"testing"
)

func ExampleGetFile_local() {
Expand Down Expand Up @@ -51,3 +54,23 @@ func ExampleWithHttpClient() {
fmt.Println(err)
// Output: failed to make get request: Get "https://example.com": no proxy
}

func TestGetOci(t *testing.T) {
logger := log.New(os.Stdout, "TEST: ", log.LstdFlags)
o := &options{
tempDir: t.TempDir(),
logger: logger,
}

testUrl := "oci://ghcr.io/mathieu-benoit/policies:0.1.0"

u, err := url.Parse(testUrl)
if err != nil {
t.Fatalf("failed to parse URL: %v", err)
}

_, err = o.getOci(context.Background(), u)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
14 changes: 10 additions & 4 deletions uriget/uriget.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,13 @@ func (o *options) getOci(ctx context.Context, u *url.URL) ([]byte, error) {
return nil, fmt.Errorf("invalid OCI URL format")
}
registry := parts[0]
repo := strings.Join(parts[1:], "/")
repo := strings.Join(parts[1:len(parts)-1], "/")
tag := "latest"
if u.Fragment != "" {
tag = u.Fragment
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]
}
store, err := oci.New(o.tempDir)
if err != nil {
Expand All @@ -260,10 +263,13 @@ func (o *options) getOci(ctx context.Context, u *url.URL) ([]byte, error) {
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
}
manifestDescriptor, err := oras.Copy(ctx, remoteRepo, tag, store, tag, oras.DefaultCopyOptions)
if err != nil {
return nil, fmt.Errorf("failed to pull OCI image: %w", err)
}
o.logger.Printf("Pulled OCI image: %s with manifest descriptor : %v", u.String(), manifestDescriptor.Digest)
o.logger.Printf("Pulled OCI image: %s with manifest descriptor: %v", u.String(), manifestDescriptor.Digest)
return []byte(manifestDescriptor.Digest), nil
}

0 comments on commit f372be8

Please sign in to comment.