Skip to content

Commit 505acf4

Browse files
author
Pete Emerson
authored
Merge branch 'master' into pete/75/tags_without_semver
2 parents e7fa52a + 9554201 commit 505acf4

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

file.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ func shouldExtractPathInZip(pathPrefix string, zipPath *zip.File) bool {
8282
}
8383

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

8787
// Open the zip file for reading.
8888
r, err := zip.OpenReader(zipFilePath)
8989
if err != nil {
90-
return err
90+
return 0, err
9191
}
9292
defer r.Close()
9393

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

105+
// Count the number of files (not directories) unpacked
106+
fileCount := 0
107+
105108
// Iterate through the files in the archive,
106109
// printing some of their contents.
107110
for _, f := range r.File {
@@ -114,30 +117,31 @@ func extractFiles(zipFilePath, filesToExtractFromZipPath, localPath string) erro
114117
path := filepath.Join(localPath, strings.TrimPrefix(f.Name, pathPrefix))
115118
err = os.MkdirAll(path, 0777)
116119
if err != nil {
117-
return fmt.Errorf("Failed to create local directory %s: %s", path, err)
120+
return fileCount, fmt.Errorf("Failed to create local directory %s: %s", path, err)
118121
}
119122
} else {
120123
// Read the file into a byte array
121124
readCloser, err := f.Open()
122125
if err != nil {
123-
return fmt.Errorf("Failed to open file %s: %s", f.Name, err)
126+
return fileCount, fmt.Errorf("Failed to open file %s: %s", f.Name, err)
124127
}
125128

126129
byteArray, err := ioutil.ReadAll(readCloser)
127130
if err != nil {
128-
return fmt.Errorf("Failed to read file %s: %s", f.Name, err)
131+
return fileCount, fmt.Errorf("Failed to read file %s: %s", f.Name, err)
129132
}
130133

131134
// Write the file
132135
err = ioutil.WriteFile(filepath.Join(localPath, strings.TrimPrefix(f.Name, pathPrefix)), byteArray, 0644)
133136
if err != nil {
134-
return fmt.Errorf("Failed to write file: %s", err)
137+
return fileCount, fmt.Errorf("Failed to write file: %s", err)
135138
}
139+
fileCount++
136140
}
137141
}
138142
}
139143

140-
return nil
144+
return fileCount, nil
141145
}
142146

143147
// 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

file_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,15 @@ func TestExtractFiles(t *testing.T) {
307307
}
308308
defer os.RemoveAll(tempDir)
309309

310-
err = extractFiles(tc.localFilePath, tc.filePathToExtract, tempDir)
310+
fileCount, err := extractFiles(tc.localFilePath, tc.filePathToExtract, tempDir)
311311
if err != nil {
312312
t.Fatalf("Failed to extract files: %s", err)
313313
}
314314

315+
if fileCount != tc.expectedNumFiles {
316+
t.Fatalf("Expected to extract %d files, extracted %d instead", tc.expectedNumFiles, fileCount)
317+
}
318+
315319
// Count the number of files in the directory
316320
var numFiles int
317321
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
@@ -351,12 +355,18 @@ func TestExtractFilesExtractFile(t *testing.T) {
351355
zipFilePath := "test-fixtures/fetch-test-public-0.0.4.zip"
352356
filePathToExtract := "zzz.txt"
353357
localFileName := "/localzzz.txt"
358+
expectedFileCount := 1
354359
localPathName := filepath.Join(tempDir, localFileName)
355-
err = extractFiles(zipFilePath, filePathToExtract, localPathName)
360+
fileCount, err := extractFiles(zipFilePath, filePathToExtract, localPathName)
361+
356362
if err != nil {
357363
t.Fatalf("Failed to extract files: %s", err)
358364
}
359365

366+
if fileCount != expectedFileCount {
367+
t.Fatalf("Expected to extract %d files, extracted %d instead", expectedFileCount, fileCount)
368+
}
369+
360370
filepath.Walk(tempDir, func(path string, info os.FileInfo, err error) error {
361371
relativeFilename := strings.TrimPrefix(path, tempDir)
362372

main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,17 @@ func downloadSourcePaths(sourcePaths []string, destPath string, githubRepo GitHu
288288

289289
// Unzip and move the files we need to our destination
290290
for _, sourcePath := range sourcePaths {
291-
fmt.Printf("Extracting files from <repo>%s to %s ...\n", sourcePath, destPath)
292-
if err := extractFiles(localZipFilePath, sourcePath, destPath); err != nil {
291+
fmt.Printf("Extracting files from <repo>%s to %s ... ", sourcePath, destPath)
292+
fileCount, err := extractFiles(localZipFilePath, sourcePath, destPath)
293+
plural := ""
294+
if fileCount != 1 {
295+
plural = "s"
296+
}
297+
fmt.Printf("%d file%s extracted\n", fileCount, plural)
298+
if err != nil {
293299
return fmt.Errorf("Error occurred while extracting files from GitHub zip file: %s", err.Error())
294300
}
301+
295302
}
296303

297304
fmt.Println("Download and file extraction complete.")

0 commit comments

Comments
 (0)