Skip to content

Exclude OWNERS files when pulling devfile stack #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions registry-library/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ const (
DevfilePNGLogoMediaType = "image/png"
DevfileArchiveMediaType = "application/x-tar"

OwnersFile = "OWNERS"

httpRequestTimeout = 30 * time.Second // httpRequestTimeout configures timeout of all HTTP requests
responseHeaderTimeout = 30 * time.Second // responseHeaderTimeout is the timeout to retrieve the server's response headers
)

var (
DevfileMediaTypeList = []string{DevfileMediaType}
DevfileAllMediaTypesList = []string{DevfileMediaType, DevfilePNGLogoMediaType, DevfileSVGLogoMediaType, DevfileVSXMediaType, DevfileArchiveMediaType}
ExcludedFiles = []string{OwnersFile}
)

type Registry struct {
Expand Down Expand Up @@ -237,7 +240,8 @@ func PrintRegistry(registryURLs string, devfileType string, options RegistryOpti
return nil
}

// PullStackByMediaTypesFromRegistry pulls a specified stack with allowed media types from a given registry URL to the destination directory
// PullStackByMediaTypesFromRegistry pulls a specified stack with allowed media types from a given registry URL to the destination directory.
// OWNERS files present in the registry will be excluded
func PullStackByMediaTypesFromRegistry(registry string, stack string, allowedMediaTypes []string, destDir string, options RegistryOptions) error {
var requestVersion string
if strings.Contains(stack, ":") {
Expand Down Expand Up @@ -329,7 +333,7 @@ func PullStackByMediaTypesFromRegistry(registry string, stack string, allowedMed
// Decompress archive.tar
archivePath := filepath.Join(destDir, "archive.tar")
if _, err := os.Stat(archivePath); err == nil {
err := decompress(destDir, archivePath)
err := decompress(destDir, archivePath, ExcludedFiles)
if err != nil {
return err
}
Expand All @@ -349,7 +353,7 @@ func PullStackFromRegistry(registry string, stack string, destDir string, option
}

// decompress extracts the archive file
func decompress(targetDir string, tarFile string) error {
func decompress(targetDir string, tarFile string, excludeFiles []string) error {
reader, err := os.Open(tarFile)
if err != nil {
return err
Expand All @@ -370,6 +374,9 @@ func decompress(targetDir string, tarFile string) error {
} else if err != nil {
return err
}
if isExcluded(header.Name, excludeFiles) {
continue
}

target := path.Join(targetDir, header.Name)
switch header.Typeflag {
Expand All @@ -396,6 +403,16 @@ func decompress(targetDir string, tarFile string) error {
return nil
}

func isExcluded(name string, excludeFiles []string) bool {
basename := filepath.Base(name)
for _, excludeFile := range excludeFiles {
if basename == excludeFile {
return true
}
}
return false
}

//setHeaders sets the request headers
func setHeaders(headers *http.Header, options RegistryOptions) {
t := options.Telemetry
Expand Down