Skip to content

Commit

Permalink
Clean up CreateFromDir
Browse files Browse the repository at this point in the history
  • Loading branch information
jongio committed Sep 26, 2024
1 parent a10080f commit a08c003
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
3 changes: 3 additions & 0 deletions cli/azd/pkg/dotignore/dotignore.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package dotignore

import (
Expand Down
5 changes: 3 additions & 2 deletions cli/azd/pkg/project/project_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"github.com/otiai10/copy"
)

// createDeployableZip creates a zip file of a folder.
// createDeployableZip creates a zip file of a folder, recursively.
// Returns the path to the created zip file or an error if it fails.
func createDeployableZip(projectName string, appName string, path string) (string, error) {
// Create the output zip file path
filePath := filepath.Join(os.TempDir(), fmt.Sprintf("%s-%s-azddeploy-%d.zip", projectName, appName, time.Now().Unix()))
zipFile, err := os.Create(filePath)
if err != nil {
return "", fmt.Errorf("failed to create zip file: %w", err)
return "", fmt.Errorf("failed when creating zip package to deploy %s: %w", appName, err)
}

// Zip the directory without any exclusions (they've already been handled in buildForZip)
Expand Down
45 changes: 25 additions & 20 deletions cli/azd/pkg/rzip/rzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ import (
"strings"
)

// CreateFromDirectory compresses a directory into a zip file.
func CreateFromDirectory(source string, buf *os.File) error {
w := zip.NewWriter(buf)
defer w.Close()

// Walk through the source directory
err := filepath.WalkDir(source, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}

// Use os.Lstat to get file info without following symlinks
// Fetch file info (Lstat avoids following symlinks)
fileInfo, err := os.Lstat(path)
if err != nil {
return err
Expand All @@ -30,54 +34,55 @@ func CreateFromDirectory(source string, buf *os.File) error {
return nil
}

// Add directories to the zip archive
relativePath := strings.Replace(
strings.TrimPrefix(
strings.TrimPrefix(path, source),
string(filepath.Separator)), "\\", "/", -1)
// Create relative path and normalize it for zip
relativePath := filepath.ToSlash(strings.TrimPrefix(strings.TrimPrefix(path, source), string(filepath.Separator)))

// Handle directories by adding a trailing slash
if fileInfo.IsDir() {
// Add trailing slash for directories in zip
relativePath += "/"
}

// Create a zip header based on file info
// Create zip header from the file info
header, err := zip.FileInfoHeader(fileInfo)
if err != nil {
return err
}

header.Name = relativePath
header.Modified = fileInfo.ModTime()

// Add files with compression
// Compress files (leave directories uncompressed)
if !fileInfo.IsDir() {
header.Method = zip.Deflate
}

// Create the header in the zip file
// Write the header to the zip
writer, err := w.CreateHeader(header)
if err != nil {
return err
}

// Only copy contents for files (not directories)
// Write the file's content if it's not a directory
if !fileInfo.IsDir() {
in, err := os.Open(path)
if err != nil {
return err
}
defer in.Close()

_, err = io.Copy(writer, in)
if err != nil {
if err := writeFileToZip(writer, path); err != nil {
return err
}
}

return nil
})

return err
}

// writeFileToZip writes the contents of a file to the zip writer.
func writeFileToZip(writer io.Writer, filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()

return w.Close()
_, err = io.Copy(writer, file)
return err
}

0 comments on commit a08c003

Please sign in to comment.