Skip to content

Commit

Permalink
Merge pull request #1919 from robnester-rh/EC-806
Browse files Browse the repository at this point in the history
EC-806 Ensure ec uses go-gather by default
  • Loading branch information
robnester-rh authored Sep 5, 2024
2 parents d12edd1 + 92f3af4 commit a086d9d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 68 deletions.
41 changes: 1 addition & 40 deletions internal/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ import (
"fmt"
"regexp"
"strings"
"sync"

"github.com/enterprise-contract/go-gather/gather"
"github.com/enterprise-contract/go-gather/metadata"
"github.com/open-policy-agent/conftest/downloader"
log "github.com/sirupsen/logrus"

"github.com/enterprise-contract/ec-cli/internal/utils"
)

type key int
Expand All @@ -43,17 +39,12 @@ type downloadImpl interface {

var gatherFunc = gather.Gather

var dlMutex sync.Mutex

// WithDownloadImpl replaces the downloadImpl implementation used
func WithDownloadImpl(ctx context.Context, d downloadImpl) context.Context {
return context.WithValue(ctx, downloadImplKey, d)
}

// Download is used to download files from various sources.
//
// Note that it handles just one url at a time even though the equivalent
// Conftest function can take a list of source urls.
func Download(ctx context.Context, destDir string, sourceUrl string, showMsg bool) (metadata.Metadata, error) {
if !isSecure(sourceUrl) {
return nil, fmt.Errorf("attempting to download from insecure source: %s", sourceUrl)
Expand All @@ -65,40 +56,10 @@ func Download(ctx context.Context, destDir string, sourceUrl string, showMsg boo
fmt.Println(msg)
}

dl := func(ctx context.Context, sourceUrl, destDir string) (metadata.Metadata, error) {
// conftest's Download function leverages oras under the hood to fetch from OCI. It uses the
// global oras client and sets the user agent to "conftest". This is not a thread safe
// operation. Here we get around this limitation by ensuring a single download happens at a
// time.
dlMutex.Lock()
defer dlMutex.Unlock()
return nil, downloader.Download(ctx, destDir, []string{sourceUrl})
}

if utils.UseGoGather() {
dl = func(ctx context.Context, sourceUrl, destDir string) (metadata.Metadata, error) {
m, err := gatherFunc(ctx, sourceUrl, destDir)
if err != nil {
log.Debug("Download failed!")
}
return m, err
}
} else if d, ok := ctx.Value(downloadImplKey).(downloadImpl); ok {
dl = func(ctx context.Context, sourceUrl, destDir string) (metadata.Metadata, error) {
err := d.Download(ctx, destDir, []string{sourceUrl})
if err != nil {
log.Debug("Download failed!")
}
return nil, err
}
}

m, err := dl(ctx, sourceUrl, destDir)

m, err := gatherFunc(ctx, sourceUrl, destDir)
if err != nil {
log.Debug("Download failed!")
}

return m, err
}

Expand Down
26 changes: 2 additions & 24 deletions internal/downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package downloader
import (
"context"
"errors"
"os"
"testing"

"github.com/enterprise-contract/go-gather/metadata"
Expand All @@ -46,27 +45,12 @@ func TestDownloader_Download(t *testing.T) {
source string
errExpected bool
err error
useGoGather bool
}{
{
name: "Downloads",
dest: "dir",
source: "https://example.com/org/repo.git",
},
{
name: "Downloads with go-gather",
dest: "dir",
source: "https://example.com/org/repo.git",
useGoGather: true,
},
{
name: "Fails to download with go-gather",
dest: "dir",
source: "https://example.com/org/repo.git",
errExpected: true,
err: errors.New("expected error with go-gather"),
useGoGather: true,
},
{
name: "Fails to download",
dest: "dir",
Expand All @@ -93,14 +77,8 @@ func TestDownloader_Download(t *testing.T) {
gatherFunc = originalGatherFunction
}()

if tt.useGoGather {
t.Setenv("USEGOGATHER", "1")
gatherFunc = func(_ context.Context, _ string, _ string) (metadata.Metadata, error) {
return nil, tt.err
}
} else {
os.Unsetenv("USEGOGATHER")
d.On("Download", ctx, tt.dest, []string{tt.source}).Return(tt.err)
gatherFunc = func(_ context.Context, _ string, _ string) (metadata.Metadata, error) {
return nil, tt.err
}

_, err := Download(ctx, tt.dest, tt.source, false)
Expand Down
4 changes: 0 additions & 4 deletions internal/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ func Experimental() bool {
return os.Getenv("EC_EXPERIMENTAL") == "1"
}

func UseGoGather() bool {
return os.Getenv("USEGOGATHER") == "1"
}

// detect if the string is json
func IsJson(data string) bool {
var jsMsg json.RawMessage
Expand Down

0 comments on commit a086d9d

Please sign in to comment.