|
6 | 6 | "encoding/json" |
7 | 7 | "fmt" |
8 | 8 | "io" |
| 9 | + "io/ioutil" |
9 | 10 | "os" |
10 | 11 | "path/filepath" |
11 | 12 | "strings" |
@@ -37,28 +38,61 @@ func Module(path string, version string, outputDirectory string) error { |
37 | 38 |
|
38 | 39 | func getModuleName(path string) (string, error) { |
39 | 40 | moduleFilePath := filepath.Join(path, "go.mod") |
40 | | - |
41 | 41 | file, err := os.Open(moduleFilePath) |
42 | 42 | if err != nil { |
43 | 43 | return "", fmt.Errorf("unable to open module file: %w", err) |
44 | 44 | } |
45 | 45 | defer file.Close() |
46 | 46 |
|
47 | | - moduleFileReader := bufio.NewReader(file) |
48 | | - moduleHeader, err := moduleFileReader.ReadString('\n') |
49 | | - if err != nil { |
50 | | - return "", fmt.Errorf("unable to read module header: %w", err) |
51 | | - } |
| 47 | + moduleFileScanner := bufio.NewScanner(file) |
| 48 | + moduleFileScanner.Scan() |
52 | 49 |
|
53 | | - moduleHeaderParts := strings.Split(moduleHeader, " ") |
| 50 | + moduleHeaderParts := strings.Split(moduleFileScanner.Text(), " ") |
54 | 51 | if len(moduleHeaderParts) <= 1 { |
55 | 52 | return "", fmt.Errorf("unable to parse module header: %w", err) |
56 | 53 | } |
57 | 54 |
|
58 | 55 | return moduleHeaderParts[1], nil |
59 | 56 | } |
60 | 57 |
|
61 | | -func getFilesToArchive(path string) ([]string, error) { |
| 58 | +func createZipArchive(path string, moduleName string, version string, outputDirectory string) error { |
| 59 | + filePathsToArchive, err := getFilePathsToArchive(path) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("unable to get files to archive: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + outputPath := filepath.Join(outputDirectory, version+".zip") |
| 65 | + zipFile, err := os.Create(outputPath) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("unable to create zip file: %w", err) |
| 68 | + } |
| 69 | + defer zipFile.Close() |
| 70 | + |
| 71 | + zipWriter := zip.NewWriter(zipFile) |
| 72 | + defer zipWriter.Close() |
| 73 | + |
| 74 | + for _, filePath := range filePathsToArchive { |
| 75 | + fileToZip, err := os.Open(filePath) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("unable to open file: %w", err) |
| 78 | + } |
| 79 | + defer fileToZip.Close() |
| 80 | + |
| 81 | + zippedFilePath := getZipPath(path, filePath, moduleName, version) |
| 82 | + zippedFileWriter, err := zipWriter.Create(zippedFilePath) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("unable to add file to zip archive: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + if _, err := io.Copy(zippedFileWriter, fileToZip); err != nil { |
| 88 | + return fmt.Errorf("unable to copy file contents to zip archive: %w", err) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func getFilePathsToArchive(path string) ([]string, error) { |
62 | 96 | var files []string |
63 | 97 | err := filepath.Walk(path, func(currentFilePath string, fileInfo os.FileInfo, err error) error { |
64 | 98 | if err != nil { |
@@ -88,44 +122,6 @@ func getFilesToArchive(path string) ([]string, error) { |
88 | 122 | return files, nil |
89 | 123 | } |
90 | 124 |
|
91 | | -func createZipArchive(path string, moduleName string, version string, outputDirectory string) error { |
92 | | - outputPath := filepath.Join(outputDirectory, version+".zip") |
93 | | - |
94 | | - zipFile, err := os.Create(outputPath) |
95 | | - if err != nil { |
96 | | - return fmt.Errorf("unable to create zip file: %w", err) |
97 | | - } |
98 | | - defer zipFile.Close() |
99 | | - |
100 | | - zipWriter := zip.NewWriter(zipFile) |
101 | | - defer zipWriter.Close() |
102 | | - |
103 | | - filesToArchive, err := getFilesToArchive(path) |
104 | | - if err != nil { |
105 | | - return fmt.Errorf("unable to get files to archive: %w", err) |
106 | | - } |
107 | | - |
108 | | - for _, file := range filesToArchive { |
109 | | - zippedFilePath := getZipPath(path, file, moduleName, version) |
110 | | - zippedFileWriter, err := zipWriter.Create(zippedFilePath) |
111 | | - if err != nil { |
112 | | - return fmt.Errorf("unable to add file to zip archive: %w", err) |
113 | | - } |
114 | | - |
115 | | - fileToZip, err := os.Open(file) |
116 | | - if err != nil { |
117 | | - return fmt.Errorf("unable to open file: %w", err) |
118 | | - } |
119 | | - defer fileToZip.Close() |
120 | | - |
121 | | - if _, err := io.Copy(zippedFileWriter, fileToZip); err != nil { |
122 | | - return fmt.Errorf("unable to copy file contents to zip archive: %w", err) |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - return nil |
127 | | -} |
128 | | - |
129 | 125 | func getZipPath(path string, currentFilePath string, moduleName string, version string) string { |
130 | 126 | filePath := strings.TrimPrefix(currentFilePath, path) |
131 | 127 | return filepath.Join(fmt.Sprintf("%s@%s", moduleName, version), filePath) |
@@ -167,24 +163,25 @@ func getInfoFileFormattedTime(currentTime time.Time) string { |
167 | 163 | return currentTime.Format(infoFileTimeFormat) |
168 | 164 | } |
169 | 165 |
|
170 | | -func copyModuleFile(modulePath string, outputDirectory string) error { |
171 | | - sourcePath := filepath.Join(modulePath, "go.mod") |
| 166 | +func copyModuleFile(path string, outputDirectory string) error { |
| 167 | + if outputDirectory == "." { |
| 168 | + return nil |
| 169 | + } |
| 170 | + |
| 171 | + sourcePath := filepath.Join(path, "go.mod") |
172 | 172 | destinationPath := filepath.Join(outputDirectory, "go.mod") |
173 | 173 |
|
174 | | - sourceModule, err := os.Open(sourcePath) |
175 | | - if err != nil { |
176 | | - return fmt.Errorf("could not open source module file: %w", err) |
| 174 | + if sourcePath == destinationPath { |
| 175 | + return nil |
177 | 176 | } |
178 | | - defer sourceModule.Close() |
179 | 177 |
|
180 | | - destinationModule, err := os.Create(destinationPath) |
| 178 | + moduleContents, err := ioutil.ReadFile(sourcePath) |
181 | 179 | if err != nil { |
182 | | - return fmt.Errorf("could not create mod file: %w", err) |
| 180 | + return fmt.Errorf("unable to read module file: %w", err) |
183 | 181 | } |
184 | | - defer destinationModule.Close() |
185 | 182 |
|
186 | | - if _, err := io.Copy(destinationModule, sourceModule); err != nil { |
187 | | - return fmt.Errorf("could not copy module contents: %w", err) |
| 183 | + if err := ioutil.WriteFile(destinationPath, moduleContents, 0644); err != nil { |
| 184 | + return fmt.Errorf("unable to write module file: %w", err) |
188 | 185 | } |
189 | 186 |
|
190 | 187 | return nil |
|
0 commit comments