Skip to content

Commit

Permalink
fix pre-push zip logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bohendo committed Sep 28, 2023
1 parent 60f438f commit a69bacb
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions cmd/cloudexec/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,97 @@ import (
"github.com/crytic/cloudexec/pkg/s3"
)

func UploadDirectoryToSpaces(config config.Config, bucketName string, sourcePath string, destPath string) error {
// Create a file to store the compressed archive of the input sourcePath
zipFilePath := filepath.Join(filepath.Dir(sourcePath), filepath.Base(sourcePath) + ".zip")
func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath string, destPath string) error {
// Compute the path for the zipped archive of sourcePath
zipFileName := "input.zip"
zipFilePath, err := filepath.Abs(filepath.Join(filepath.Dir(sourcePath), zipFileName))
if err != nil {
return err
}

// Create a file where we will write the zipped archive
zipFile, err := os.Create(zipFilePath)
if err != nil {
return err
}
defer zipFile.Close()

archive := zip.NewWriter(zipFile)
defer archive.Close()
// Create a new zip writer
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()

// Walk the directory and recursively add files to the zip archive
// Walk the directory and recursively add files to the zipped archive
err = filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fmt.Printf("Adding %s to the zipped archive", path)

// Create a new ZIP file entry
zipFileEntry, err := archive.Create(path[len(sourcePath):])
if err != nil {
return err
}

// If this is a subdirectory, we're done. Move on.
// If this is a subdirectory, make sure the path ends with a trailing slash before we create it
// See https://pkg.go.dev/archive/zip#Writer.Create for details
if info.IsDir() {
cleanPath := filepath.Clean(path) + string(filepath.Separator)
_, err := zipWriter.Create(cleanPath)
if err != nil {
return err
}
fmt.Printf("Created directory %s in the zipped archive\n", cleanPath)
return nil
}

// Create a new file entry in the zipped archive
zipFileEntry, err := zipWriter.Create(path)
if err != nil {
return err
}

// Open the file we're adding to the zipped archive
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()

// Write this file to the zipped archive
_, err = io.Copy(zipFileEntry, file)
if err != nil {
return err
}

fmt.Printf("Added %s to the zipped archive\n", path)
return nil
})
if err != nil {
return err
}
fmt.Printf("Successfully added all files from %s to zipped archive at %s\n", sourcePath, zipFilePath)

// Make sure all prior writes are sync'd to the filesystem
// This is necessary because we're going to read the file immediately after writing it
err = zipWriter.Flush()
if err != nil {
return err
}
err = zipFile.Sync()
if err != nil {
return err
}

// Read the zipped archive
fileBytes, err := os.ReadFile(zipFilePath)
if err != nil {
return fmt.Errorf("Failed to read zipped archive %s: %w", zipFilePath, err)
}
if len(fileBytes) == 0 {
return fmt.Errorf("Failed to read zipped archive at %s: read zero bytes of data", zipFilePath)
}

// Compute the destination key in the bucket
relativePath, _ := filepath.Rel(sourcePath, zipFilePath)
destinationKey := filepath.Join(destPath, "input", relativePath)

err = s3.PutObject(config, bucketName, destinationKey, fileBytes)
// Upload the zipped archive
destKey := filepath.Join(destPath, "input.zip")
fmt.Printf("Uploading archive (%v bytes) to %s\n", len(fileBytes), destKey)
err = s3.PutObject(config, bucket, destKey, fileBytes)
if err != nil {
return err
}

fmt.Printf("Successfully uploaded %s to %s/%s\n", zipFilePath, bucketName, destinationKey)
return nil
}

0 comments on commit a69bacb

Please sign in to comment.