Skip to content

Commit

Permalink
refactor(zbpack): Refactor functions
Browse files Browse the repository at this point in the history
Signed-off-by: hackerchai <[email protected]>
  • Loading branch information
hackerchai committed Oct 30, 2023
1 parent 52291bc commit c273bb3
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions internal/utils/copy_from_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ func CopyFromImage(image, srcInImage, destOnHost string) error {
}
excludeFiles := []string{".gitkeep", ".ini", ".env", ".DS_Store"}
excludeDirs := []string{".git", ".zeabur"}
err = deleteFilesInDirectory(excludeFiles, destOnHost)
err = deleteFilesRecursively(excludeFiles, destOnHost)
if err != nil {
return fmt.Errorf("delete files in directory: %w", err)
}
err = deleteDirectoriesInDirectory(excludeDirs, destOnHost)
err = deleteDirectories(excludeDirs, destOnHost)
if err != nil {
return fmt.Errorf("delete directories in directory: %w", err)
}

return nil
}

func deleteFilesInDirectory(deleteFiles []string, path string) error {
func deleteFilesRecursively(deleteFiles []string, path string) error {
// walk through the directory
err := filepath.Walk(path, func(filePath string, fileInfo os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -83,25 +83,12 @@ func deleteFilesInDirectory(deleteFiles []string, path string) error {
return nil
}

func deleteDirectoriesInDirectory(deleteDirs []string, path string) error {
fileInfo, err := os.ReadDir(path)
if err != nil {
return err
}

for _, dirInfo := range fileInfo {
if dirInfo.IsDir() {
dirName := dirInfo.Name()

for _, targetDir := range deleteDirs {
if dirName == targetDir {
dirPath := filepath.Join(path, dirName)
err := os.RemoveAll(dirPath)
if err != nil {
return err
}
}
}
func deleteDirectories(deleteDirs []string, path string) error {
for _, targetDir := range deleteDirs {
dirPath := filepath.Join(path, targetDir)
err := os.RemoveAll(dirPath)
if err != nil {
return err
}
}
return nil
Expand Down

0 comments on commit c273bb3

Please sign in to comment.