Skip to content

Commit

Permalink
fix: remove dependency on oras CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
psturc committed Nov 26, 2024
1 parent 788443a commit bc71b46
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
21 changes: 2 additions & 19 deletions cmd/oci/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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' {
Expand Down
22 changes: 16 additions & 6 deletions pkg/oci/artifact_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit bc71b46

Please sign in to comment.