Skip to content

Commit

Permalink
Exclude OWNERS files when pulling devfile stack (#114)
Browse files Browse the repository at this point in the history
* Exclude OWNERS files when pulling devfile stack

Signed-off-by: Philippe Martin <phmartin@redhat.com>

* Doc

Signed-off-by: Philippe Martin <phmartin@redhat.com>

* Move constant to declaration blocks

Signed-off-by: Philippe Martin <phmartin@redhat.com>
  • Loading branch information
feloy committed May 4, 2022
1 parent 1821103 commit 21de537
Showing 1 changed file with 20 additions and 3 deletions.
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

0 comments on commit 21de537

Please sign in to comment.