@@ -18,19 +18,19 @@ import (
1818func 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) {
5858func 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