Skip to content

Commit

Permalink
Merge branch 'master' into pete/75/tags_without_semver
Browse files Browse the repository at this point in the history
  • Loading branch information
Pete Emerson authored Oct 27, 2020
2 parents e7fa52a + 9554201 commit 505acf4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
18 changes: 11 additions & 7 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func shouldExtractPathInZip(pathPrefix string, zipPath *zip.File) bool {
}

// Decompress the file at zipFileAbsPath and move only those files under filesToExtractFromZipPath to localPath
func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) error {
func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) (int, error) {

// Open the zip file for reading.
r, err := zip.OpenReader(zipFilePath)
if err != nil {
return err
return 0, err
}
defer r.Close()

Expand All @@ -102,6 +102,9 @@ func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) erro
// Add the path from which we will extract files to the path prefix so we can exclude the appropriate files
pathPrefix = filepath.Join(pathPrefix, filesToExtractFromZipPath)

// Count the number of files (not directories) unpacked
fileCount := 0

// Iterate through the files in the archive,
// printing some of their contents.
for _, f := range r.File {
Expand All @@ -114,30 +117,31 @@ func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) erro
path := filepath.Join(localPath, strings.TrimPrefix(f.Name, pathPrefix))
err = os.MkdirAll(path, 0777)
if err != nil {
return fmt.Errorf("Failed to create local directory %s: %s", path, err)
return fileCount, fmt.Errorf("Failed to create local directory %s: %s", path, err)
}
} else {
// Read the file into a byte array
readCloser, err := f.Open()
if err != nil {
return fmt.Errorf("Failed to open file %s: %s", f.Name, err)
return fileCount, fmt.Errorf("Failed to open file %s: %s", f.Name, err)
}

byteArray, err := ioutil.ReadAll(readCloser)
if err != nil {
return fmt.Errorf("Failed to read file %s: %s", f.Name, err)
return fileCount, fmt.Errorf("Failed to read file %s: %s", f.Name, err)
}

// Write the file
err = ioutil.WriteFile(filepath.Join(localPath, strings.TrimPrefix(f.Name, pathPrefix)), byteArray, 0644)
if err != nil {
return fmt.Errorf("Failed to write file: %s", err)
return fileCount, fmt.Errorf("Failed to write file: %s", err)
}
fileCount++
}
}
}

return nil
return fileCount, nil
}

// Return an HTTP request that will fetch the given GitHub repo's zip file for the given tag, possibly with the gitHubOAuthToken in the header
Expand Down
14 changes: 12 additions & 2 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,15 @@ func TestExtractFiles(t *testing.T) {
}
defer os.RemoveAll(tempDir)

err = extractFiles(tc.localFilePath, tc.filePathToExtract, tempDir)
fileCount, err := extractFiles(tc.localFilePath, tc.filePathToExtract, tempDir)
if err != nil {
t.Fatalf("Failed to extract files: %s", err)
}

if fileCount != tc.expectedNumFiles {
t.Fatalf("Expected to extract %d files, extracted %d instead", tc.expectedNumFiles, fileCount)
}

// Count the number of files in the directory
var numFiles int
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
Expand Down Expand Up @@ -351,12 +355,18 @@ func TestExtractFilesExtractFile(t *testing.T) {
zipFilePath := "test-fixtures/fetch-test-public-0.0.4.zip"
filePathToExtract := "zzz.txt"
localFileName := "/localzzz.txt"
expectedFileCount := 1
localPathName := filepath.Join(tempDir, localFileName)
err = extractFiles(zipFilePath, filePathToExtract, localPathName)
fileCount, err := extractFiles(zipFilePath, filePathToExtract, localPathName)

if err != nil {
t.Fatalf("Failed to extract files: %s", err)
}

if fileCount != expectedFileCount {
t.Fatalf("Expected to extract %d files, extracted %d instead", expectedFileCount, fileCount)
}

filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
relativeFilename := strings.TrimPrefix(path, tempDir)

Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,17 @@ func downloadSourcePaths(sourcePaths []string, destPath string, githubRepo GitHu

// Unzip and move the files we need to our destination
for _, sourcePath := range sourcePaths {
fmt.Printf("Extracting files from <repo>%s to %s ...\n", sourcePath, destPath)
if err := extractFiles(localZipFilePath, sourcePath, destPath); err != nil {
fmt.Printf("Extracting files from <repo>%s to %s ... ", sourcePath, destPath)
fileCount, err := extractFiles(localZipFilePath, sourcePath, destPath)
plural := ""
if fileCount != 1 {
plural = "s"
}
fmt.Printf("%d file%s extracted\n", fileCount, plural)
if err != nil {
return fmt.Errorf("Error occurred while extracting files from GitHub zip file: %s", err.Error())
}

}

fmt.Println("Download and file extraction complete.")
Expand Down

0 comments on commit 505acf4

Please sign in to comment.