Skip to content

Commit

Permalink
Added directory creation if it does not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
DeanHnter committed Feb 19, 2024
1 parent da04d25 commit cb9775c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
60 changes: 34 additions & 26 deletions Client/io/io.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
package io

import (
"io"
"os"
"path/filepath"
"io"
)


func CopyDir(src string, dest string) error {
// Get properties of source directory
srcInfo, err := os.Stat(src)
if err != nil {
return err
}

// Create the destination directory
// Create the destination directory with the same permissions as the source
err = os.MkdirAll(dest, srcInfo.Mode())
if err != nil {
return err
}

directory, _ := os.Open(src)
objects, err := directory.Readdir(-1)
entries, err := os.ReadDir(src)
if err != nil {
return err
}

for _, obj := range objects {
srcFilePointer := filepath.Join(src, obj.Name())
destFilePointer := filepath.Join(dest, obj.Name())
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
destPath := filepath.Join(dest, entry.Name())

if obj.IsDir() {
// Create sub-directories, recursively.
err = CopyDir(srcFilePointer, destFilePointer)
if entry.IsDir() {
// Recursively copy sub-directories
err = CopyDir(srcPath, destPath)
if err != nil {
return err
}
} else {
// Perform the file copy.
err = CopyFile(srcFilePointer, destFilePointer)
// Copy the file
err = CopyFile(srcPath, destPath)
if err != nil {
return err
}
Expand All @@ -45,27 +46,34 @@ func CopyDir(src string, dest string) error {
return nil
}

func CopyFile(src, dst string) error {
var err error
var srcfd *os.File
var dstfd *os.File
var srcinfo os.FileInfo

if srcfd, err = os.Open(src); err != nil {
func CopyFile(src, dest string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcfd.Close()
defer srcFile.Close()

if dstfd, err = os.Create(dst); err != nil {
// Get the file information
srcInfo, err := srcFile.Stat()
if err != nil {
return err
}
defer dstfd.Close()

if _, err = io.Copy(dstfd, srcfd); err != nil {
// Ensure the destination directory exists
destDir := filepath.Dir(dest)
err = os.MkdirAll(destDir, srcInfo.Mode().Perm())
if err != nil {
return err
}
if srcinfo, err = os.Stat(src); err != nil {

// Create the destination file
destFile, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, srcInfo.Mode())
if err != nil {
return err
}
return os.Chmod(dst, srcinfo.Mode())
defer destFile.Close()

// Copy the file content
_, err = io.Copy(destFile, srcFile)
return err
}
2 changes: 1 addition & 1 deletion Client/kaniko/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (kd *KanikoDocker) CopyContextAndDockerfile(contextPath string, dockerfileP

// Calculate the path that the Dockerfile will have inside the RootDir.
dockerfileInsideRootDir := filepath.Join(kanikoExecutor.RootDir, filepath.Base(dockerfilePath))

fmt.Println("Copying "+dockerfilePath+" to :"+dockerfileInsideRootDir)
// Copy the Dockerfile to the desired location inside RootDir.
err = io.CopyFile(dockerfilePath, dockerfileInsideRootDir)
if err != nil {
Expand Down

0 comments on commit cb9775c

Please sign in to comment.