|
| 1 | +package pack |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// Module represents a Go module |
| 15 | +type Module struct { |
| 16 | + Path string |
| 17 | + Name string |
| 18 | + Version string |
| 19 | +} |
| 20 | + |
| 21 | +// PackageModule packs the module and outputs the result to the specified output path |
| 22 | +func (m Module) PackageModule(outputDirectory string) error { |
| 23 | + if err := m.createZipArchive(outputDirectory); err != nil { |
| 24 | + return fmt.Errorf("could not create zip archive: %w", err) |
| 25 | + } |
| 26 | + |
| 27 | + if err := m.createInfoFile(outputDirectory); err != nil { |
| 28 | + return fmt.Errorf("could not create info file: %w", err) |
| 29 | + } |
| 30 | + |
| 31 | + if err := m.copyModuleFile(outputDirectory); err != nil { |
| 32 | + return fmt.Errorf("could not copy module file: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +func (m Module) createZipArchive(outputDirectory string) error { |
| 39 | + outputPath := filepath.Join(outputDirectory, m.Version+".zip") |
| 40 | + |
| 41 | + zipFile, err := os.Create(outputPath) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("unable to create empty zip file: %w", err) |
| 44 | + } |
| 45 | + defer zipFile.Close() |
| 46 | + |
| 47 | + zipWriter := zip.NewWriter(zipFile) |
| 48 | + err = filepath.Walk(m.Path, func(currentFilePath string, fileInfo os.FileInfo, err error) error { |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("unable to walk path: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + if fileInfo.IsDir() && fileInfo.Name() == ".git" { |
| 54 | + return filepath.SkipDir |
| 55 | + } |
| 56 | + |
| 57 | + if fileInfo.IsDir() || filepath.Ext(currentFilePath) == ".zip" { |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + file, err := os.Open(currentFilePath) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("unable to open file: %w", err) |
| 64 | + } |
| 65 | + defer file.Close() |
| 66 | + |
| 67 | + zipPath := m.getZipPath(currentFilePath) |
| 68 | + zipFileWriter, err := zipWriter.Create(zipPath) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("unable to add file to zip archive: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + if _, err := io.Copy(zipFileWriter, file); err != nil { |
| 74 | + return fmt.Errorf("unable to copy file to zip archive: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("unable to zip all files: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + return zipWriter.Close() |
| 84 | +} |
| 85 | + |
| 86 | +func (m Module) getZipPath(currentFilePath string) string { |
| 87 | + fileName := strings.TrimPrefix(currentFilePath, m.Path) |
| 88 | + moduleName := fmt.Sprintf("%s@%s", m.Name, m.Version) |
| 89 | + |
| 90 | + return filepath.Join(moduleName, fileName) |
| 91 | +} |
| 92 | + |
| 93 | +func (m Module) createInfoFile(outputDirectory string) error { |
| 94 | + infoFilePath := filepath.Join(outputDirectory, m.Version+".info") |
| 95 | + file, err := os.Create(infoFilePath) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("could not create info file: %w", err) |
| 98 | + } |
| 99 | + defer file.Close() |
| 100 | + |
| 101 | + infoBytes, err := json.Marshal(struct { |
| 102 | + Version string |
| 103 | + Time string |
| 104 | + }{ |
| 105 | + Version: m.Version, |
| 106 | + Time: time.Now().Format("2006-01-02T15:04:05Z"), |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("could not marshal info file: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + if _, err := file.Write(infoBytes); err != nil { |
| 113 | + return fmt.Errorf("could not write info file: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func (m Module) copyModuleFile(outputDirectory string) error { |
| 120 | + sourcePath := filepath.Join(m.Path, "go.mod") |
| 121 | + destinationPath := filepath.Join(outputDirectory, "go.mod") |
| 122 | + |
| 123 | + sourceModule, err := os.Open(sourcePath) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("could not open source module file: %w", err) |
| 126 | + } |
| 127 | + defer sourceModule.Close() |
| 128 | + |
| 129 | + destinationModule, err := os.Create(destinationPath) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("could not create mod file: %w", err) |
| 132 | + } |
| 133 | + defer destinationModule.Close() |
| 134 | + |
| 135 | + if _, err := io.Copy(destinationModule, sourceModule); err != nil { |
| 136 | + return fmt.Errorf("could not copy module contents: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments