Skip to content

Commit

Permalink
Add zip create functionality to map executor
Browse files Browse the repository at this point in the history
The zip-create.go file was introduced to provide functionality for creating zip files. Changes were also made in the map-executor.go file to integrate this new zip create function. The implemented function takes a file path and archive file name as input and creates a zip archive.
  • Loading branch information
PiotrFerenc committed Jun 1, 2024
1 parent 9630366 commit 2c4e740
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
129 changes: 129 additions & 0 deletions cmd/worker/actions/zip/zip-create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package zip

import (
"archive/zip"
"github.com/PiotrFerenc/mash2/cmd/worker/actions"
"github.com/PiotrFerenc/mash2/internal/configuration"
"github.com/PiotrFerenc/mash2/internal/types"
"io"
"os"
"path/filepath"
)

type archiveToFile struct {
config *configuration.Config
filePath actions.Property
archiveFileName actions.Property
createdArchivePath actions.Property
}

func CreateArchiveToFile(config *configuration.Config) actions.Action {
return &archiveToFile{
config: config,
filePath: actions.Property{
Name: "filePath",
Type: actions.Text,
Description: "The path of the directory to be archived",
DisplayName: "Directory Path",
Validation: "required",
},
archiveFileName: actions.Property{
Name: "archiveFileName",
Type: actions.Text,
Description: "The name of the archive file",
DisplayName: "Archive File Name",
Validation: "required",
},
createdArchivePath: actions.Property{
Name: "createdArchivePath",
Type: actions.Text,
Description: "The path where the new archive was created",
DisplayName: "Created Archive Path",
Validation: "",
},
}
}
func (action *archiveToFile) GetCategoryName() string {
return "zip"
}

func (action *archiveToFile) Inputs() []actions.Property {
return []actions.Property{
action.filePath,
action.archiveFileName,
}
}

func (action *archiveToFile) Outputs() []actions.Property {
return []actions.Property{
action.createdArchivePath,
}
}

// Execute performs the archive to file action.
// It takes a Process message as input, extracts the file path and archive file name properties from the message,
// creates a new archive file at the specified path, and adds the files from the specified file path to the archive.
// The resulting archive file path is set as a property in the message.
//
// Parameters:
// - message: The input Process message containing the file path and archive file name properties.
//
// Returns:
// - types.Process: The modified Process message with the archive file path property set.
// - error: An error if any occurred during the execution.
func (action *archiveToFile) Execute(message types.Process) (types.Process, error) {
filePath, err := action.filePath.GetStringFrom(&message)
if err != nil {
return types.Process{}, err
}
archiveFileName, err := action.archiveFileName.GetStringFrom(&message)
if err != nil {
return types.Process{}, err
}
archiveFullPath := filepath.Join(action.config.Folder.TmpFolder, archiveFileName)
archiveFile, err := os.Create(archiveFullPath)
if err != nil {
return types.Process{}, err
}
defer archiveFile.Close()

zipWriter := zip.NewWriter(archiveFile)
defer zipWriter.Close()

err = filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Only add files to the archive, skip directories
//if info.IsDir() {
// return nil
//}

fileToZip, err := os.Open(path)
if err != nil {
return err
}
defer fileToZip.Close()

header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}

header.Method = zip.Deflate
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}

_, err = io.Copy(writer, fileToZip)
return err
})
if err != nil {
return types.Process{}, err
}

message.SetString(action.createdArchivePath.Name, archiveFullPath)
return message, nil
}
2 changes: 2 additions & 0 deletions internal/executor/map-executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/PiotrFerenc/mash2/cmd/worker/actions/git"
"github.com/PiotrFerenc/mash2/cmd/worker/actions/math"
"github.com/PiotrFerenc/mash2/cmd/worker/actions/others"
"github.com/PiotrFerenc/mash2/cmd/worker/actions/zip"
"github.com/PiotrFerenc/mash2/internal/configuration"
"github.com/PiotrFerenc/mash2/internal/queues"
"github.com/PiotrFerenc/mash2/internal/types"
Expand Down Expand Up @@ -122,6 +123,7 @@ func CreateActionMap(config *configuration.Config) map[string]actions.Action {
"file-create": file.CreateContentToFile(config),
"docker-run": docker.CreateDockerRun(),
"docker-remove": docker.CreateDockerRemove(),
"zip-create": zip.CreateArchiveToFile(),

Check failure on line 126 in internal/executor/map-executor.go

View workflow job for this annotation

GitHub Actions / build

not enough arguments in call to zip.CreateArchiveToFile
"file-delete": file.CreateDeleteFileAction(config),
"file-append": file.CreateAppendContentToFile(config),
//"for-each": common.CreateForEachLoop(),
Expand Down

0 comments on commit 2c4e740

Please sign in to comment.