diff --git a/internal/fs.go b/internal/fs.go index 1028af3..81af84d 100644 --- a/internal/fs.go +++ b/internal/fs.go @@ -1,6 +1,7 @@ package internal import ( + "archive/zip" "context" "errors" "fmt" @@ -238,3 +239,70 @@ func mkdir(path string) (string, error) { return path, nil } + +func zipIt(dirPath, archivePath string) error { + archivePath, err := filepath.Abs(archivePath) + if err != nil { + return err + } + + archivePath = filepath.Clean(archivePath) + zipFile, err := os.Create(archivePath) + if err != nil { + return err + } + + defer func(zipFile *os.File) { + if err := zipFile.Close(); err != nil { + fmt.Println(err) + } + }(zipFile) + + zipWriter := zip.NewWriter(zipFile) + defer func(zipWriter *zip.Writer) { + if err := zipWriter.Close(); err != nil { + fmt.Println(err) + } + }(zipWriter) + + err = filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error { + filePath = filepath.Clean(filePath) + if err != nil { + return err + } + + if filePath == dirPath { + return nil + } + + relPath, err := filepath.Rel(dirPath, filePath) + if err != nil { + return err + } + + if info.IsDir() { + _, err := zipWriter.Create(relPath + "/") + return err + } + + fileInArchive, err := zipWriter.Create(relPath) + if err != nil { + return err + } + + srcFile, err := os.Open(filePath) + if err != nil { + return err + } + + defer func(srcFile *os.File) { + if err := srcFile.Close(); err != nil { + fmt.Println(err) + } + }(srcFile) + + _, err = io.Copy(fileInArchive, srcFile) + return err + }) + return err +} diff --git a/internal/module_builder.go b/internal/module_builder.go index 870f527..5875bb7 100644 --- a/internal/module_builder.go +++ b/internal/module_builder.go @@ -157,6 +157,12 @@ func (m *Module) Collect(log *zerolog.Logger) error { return fmt.Errorf("errors: %v", errs) } + zipPath := filepath.Join(m.BuildDirectory, fmt.Sprintf("%s.zip", m.Version)) + if err := zipIt(buildDirectory, zipPath); err != nil { + log.Error().Err(err).Msg("failed to zip build") + return err + } + return nil }