Skip to content

Commit ff8a822

Browse files
committed
Close file after zip operation
1 parent b5127c3 commit ff8a822

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.DS_Store
1+
.DS_Store
2+
pacmod

internal/commands/pack.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ func NewPackCommand() *cobra.Command {
2929
func runPackCommand(args []string) error {
3030
path, err := os.Getwd()
3131
if err != nil {
32-
return fmt.Errorf("could not get working directory: %w", err)
32+
return fmt.Errorf("get working directory: %w", err)
3333
}
3434

3535
version := args[0]
3636

3737
path, err = filepath.Abs(path)
3838
if err != nil {
39-
return fmt.Errorf("could not get abs path of module path: %w", err)
39+
return fmt.Errorf("get abs path of module path: %w", err)
4040
}
4141

4242
outputDirectory, err := filepath.Abs(args[1])
4343
if err != nil {
44-
return fmt.Errorf("could not get abs path of output directory: %w", err)
44+
return fmt.Errorf("get abs path of output directory: %w", err)
4545
}
4646

4747
log.Printf("Packing module in path %s...", outputDirectory)
4848
if err := pack.Module(path, version, outputDirectory); err != nil {
49-
return fmt.Errorf("could not package module: %w", err)
49+
return fmt.Errorf("package module: %w", err)
5050
}
5151

5252
return nil

pack/pack.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ import (
1818
func Module(path string, version string, outputDirectory string) error {
1919
moduleName, err := getModuleName(path)
2020
if err != nil {
21-
return fmt.Errorf("could not get module name: %w", err)
21+
return fmt.Errorf("get module name: %w", err)
2222
}
2323

2424
if err := createZipArchive(path, moduleName, version, outputDirectory); err != nil {
25-
return fmt.Errorf("could not create zip archive: %w", err)
25+
return fmt.Errorf("create zip archive: %w", err)
2626
}
2727

2828
if err := createInfoFile(version, outputDirectory); err != nil {
29-
return fmt.Errorf("could not create info file: %w", err)
29+
return fmt.Errorf("create info file: %w", err)
3030
}
3131

3232
if err := copyModuleFile(path, outputDirectory); err != nil {
33-
return fmt.Errorf("could not copy module file: %w", err)
33+
return fmt.Errorf("copy module file: %w", err)
3434
}
3535

3636
return nil
@@ -49,7 +49,7 @@ func getModuleName(path string) (string, error) {
4949

5050
moduleHeaderParts := strings.Split(moduleFileScanner.Text(), " ")
5151
if len(moduleHeaderParts) <= 1 {
52-
return "", fmt.Errorf("unable to parse module header: %w", err)
52+
return "", fmt.Errorf("parse module header: %w", err)
5353
}
5454

5555
return moduleHeaderParts[1], nil
@@ -58,13 +58,13 @@ func getModuleName(path string) (string, error) {
5858
func createZipArchive(path string, moduleName string, version string, outputDirectory string) error {
5959
filePathsToArchive, err := getFilePathsToArchive(path)
6060
if err != nil {
61-
return fmt.Errorf("unable to get files to archive: %w", err)
61+
return fmt.Errorf("get files to archive: %w", err)
6262
}
6363

6464
outputPath := filepath.Join(outputDirectory, version+".zip")
6565
zipFile, err := os.Create(outputPath)
6666
if err != nil {
67-
return fmt.Errorf("unable to create zip file: %w", err)
67+
return fmt.Errorf("create zip file: %w", err)
6868
}
6969
defer zipFile.Close()
7070

@@ -74,19 +74,20 @@ func createZipArchive(path string, moduleName string, version string, outputDire
7474
for _, filePath := range filePathsToArchive {
7575
fileToZip, err := os.Open(filePath)
7676
if err != nil {
77-
return fmt.Errorf("unable to open file: %w", err)
77+
return fmt.Errorf("open file: %w", err)
7878
}
79-
defer fileToZip.Close()
8079

8180
zippedFilePath := getZipPath(path, filePath, moduleName, version)
8281
zippedFileWriter, err := zipWriter.Create(zippedFilePath)
8382
if err != nil {
84-
return fmt.Errorf("unable to add file to zip archive: %w", err)
83+
return fmt.Errorf("add file to zip archive: %w", err)
8584
}
8685

8786
if _, err := io.Copy(zippedFileWriter, fileToZip); err != nil {
88-
return fmt.Errorf("unable to copy file contents to zip archive: %w", err)
87+
return fmt.Errorf("copy file contents to zip archive: %w", err)
8988
}
89+
90+
fileToZip.Close()
9091
}
9192

9293
return nil
@@ -96,17 +97,13 @@ func getFilePathsToArchive(path string) ([]string, error) {
9697
var files []string
9798
err := filepath.Walk(path, func(currentFilePath string, fileInfo os.FileInfo, err error) error {
9899
if err != nil {
99-
return fmt.Errorf("unable to walk path: %w", err)
100+
return fmt.Errorf("walk path: %w", err)
100101
}
101102

102-
// We do not want to include the .git directory in the archived module
103-
// filepath.SkipDir tells the Walk() function to ignore everything inside of the directory
104103
if fileInfo.IsDir() && fileInfo.Name() == ".git" {
105104
return filepath.SkipDir
106105
}
107106

108-
// Do not process directories
109-
// returning nil tells the Walk() function to ignore this file
110107
if fileInfo.IsDir() {
111108
return nil
112109
}
@@ -131,7 +128,7 @@ func createInfoFile(version string, outputDirectory string) error {
131128
infoFilePath := filepath.Join(outputDirectory, version+".info")
132129
file, err := os.Create(infoFilePath)
133130
if err != nil {
134-
return fmt.Errorf("could not create info file: %w", err)
131+
return fmt.Errorf("create info file: %w", err)
135132
}
136133
defer file.Close()
137134

@@ -148,11 +145,11 @@ func createInfoFile(version string, outputDirectory string) error {
148145

149146
infoBytes, err := json.Marshal(info)
150147
if err != nil {
151-
return fmt.Errorf("could not marshal info file: %w", err)
148+
return fmt.Errorf("marshal info file: %w", err)
152149
}
153150

154151
if _, err := file.Write(infoBytes); err != nil {
155-
return fmt.Errorf("could not write info file: %w", err)
152+
return fmt.Errorf("write info file: %w", err)
156153
}
157154

158155
return nil
@@ -177,11 +174,11 @@ func copyModuleFile(path string, outputDirectory string) error {
177174

178175
moduleContents, err := ioutil.ReadFile(sourcePath)
179176
if err != nil {
180-
return fmt.Errorf("unable to read module file: %w", err)
177+
return fmt.Errorf("read module file: %w", err)
181178
}
182179

183180
if err := ioutil.WriteFile(destinationPath, moduleContents, 0644); err != nil {
184-
return fmt.Errorf("unable to write module file: %w", err)
181+
return fmt.Errorf("write module file: %w", err)
185182
}
186183

187184
return nil

0 commit comments

Comments
 (0)