diff --git a/file.go b/file.go index 4619e05..fce4072 100644 --- a/file.go +++ b/file.go @@ -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() @@ -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 { @@ -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 diff --git a/file_test.go b/file_test.go index f78a264..40e5f78 100644 --- a/file_test.go +++ b/file_test.go @@ -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 { @@ -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) diff --git a/main.go b/main.go index f7a2e58..bf399a8 100644 --- a/main.go +++ b/main.go @@ -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 %s to %s ...\n", sourcePath, destPath) - if err := extractFiles(localZipFilePath, sourcePath, destPath); err != nil { + fmt.Printf("Extracting files from %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.")