From bc71b46bb1329e1690d9798d7c550aedf52ef3e5 Mon Sep 17 00:00:00 2001 From: Pavel Sturc Date: Tue, 26 Nov 2024 14:09:10 +0100 Subject: [PATCH] fix: remove dependency on oras CLI --- cmd/oci/download.go | 21 ++------------------- pkg/oci/artifact_scanner.go | 22 ++++++++++++++++------ pkg/utils/utils.go | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 pkg/utils/utils.go diff --git a/cmd/oci/download.go b/cmd/oci/download.go index c7b02e7..652ca01 100644 --- a/cmd/oci/download.go +++ b/cmd/oci/download.go @@ -9,6 +9,7 @@ import ( "time" "github.com/konflux-ci/qe-tools/pkg/oci" + "github.com/konflux-ci/qe-tools/pkg/utils" "github.com/spf13/cobra" ) @@ -119,7 +120,7 @@ Examples: // If repo is specified, call helper function to download from a single repository if opts.repo != "" { - repo, tag, err := parseRepoAndTag(opts.repo) + repo, tag, err := utils.ParseRepoAndTag(opts.repo) if err != nil { return err } @@ -192,24 +193,6 @@ Examples: }, } -// parseRepoAndTag extracts the repository and tag from the given repo flag. -func parseRepoAndTag(repoFlag string) (string, string, error) { - // Ensure the repoFlag starts with 'quay.io/' - if !strings.HasPrefix(repoFlag, "quay.io/") { - return "", "", fmt.Errorf("the repository must start with 'quay.io/'") - } - - // Remove 'quay.io/' prefix and split the repo and tag using the ':' character - repoFlag = strings.TrimPrefix(repoFlag, "quay.io/") - parts := strings.SplitN(repoFlag, ":", 2) - - if len(parts) != 2 { - return "", "", fmt.Errorf("tag is missing in the repo flag") - } - - return parts[0], parts[1], nil -} - // parseDuration handles the custom duration format func parseDuration(since string) (time.Duration, error) { if len(since) > 1 && since[len(since)-1] == 'd' { diff --git a/pkg/oci/artifact_scanner.go b/pkg/oci/artifact_scanner.go index d4e5009..bf1326d 100644 --- a/pkg/oci/artifact_scanner.go +++ b/pkg/oci/artifact_scanner.go @@ -4,10 +4,12 @@ import ( "fmt" "io" "os" - "os/exec" "path/filepath" "regexp" "slices" + "time" + + "github.com/konflux-ci/qe-tools/pkg/utils" ) // NewArtifactScanner creates a new instance of ArtifactScanner. @@ -40,11 +42,19 @@ func (as *ArtifactScanner) Run() error { } func (as *ArtifactScanner) pullAndExtractOciArtifact() error { - app := "oras" - args := []string{"pull", as.config.OciArtifactReference, "--output", as.artifactDirPath} - // #nosec G204 - cmd := exec.Command(app, args...) - if err := cmd.Run(); err != nil { + cacheDir, err := os.MkdirTemp("", "") + if err != nil { + return err + } + ctrl, err := NewController(as.artifactDirPath, cacheDir) + if err != nil { + return err + } + repo, tag, err := utils.ParseRepoAndTag(as.config.OciArtifactReference) + if err != nil { + return err + } + if err := ctrl.ProcessTag(repo, tag, time.Now().Format(time.RFC1123)); err != nil { return err } return nil diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go new file mode 100644 index 0000000..a41dbab --- /dev/null +++ b/pkg/utils/utils.go @@ -0,0 +1,24 @@ +package utils + +import ( + "fmt" + "strings" +) + +// ParseRepoAndTag extracts the quay.io repository and tag from the given repo flag. +func ParseRepoAndTag(repoFlag string) (string, string, error) { + // Ensure the repoFlag starts with 'quay.io/' + if !strings.HasPrefix(repoFlag, "quay.io/") { + return "", "", fmt.Errorf("the repository must start with 'quay.io/'") + } + + // Remove 'quay.io/' prefix and split the repo and tag using the ':' character + repoFlag = strings.TrimPrefix(repoFlag, "quay.io/") + parts := strings.SplitN(repoFlag, ":", 2) + + if len(parts) != 2 { + return "", "", fmt.Errorf("tag is missing in the repo flag") + } + + return parts[0], parts[1], nil +}