Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: module archiving has been implemented #23

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions internal/fs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"archive/zip"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions internal/module_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down