github.com/argoproj/argo-cd@v1.8.7/test/fixture/path/files.go (about) 1 package path 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 ) 10 11 // CopyDir copies the contents of a directory from 'src' to 'dest' 12 func CopyDir(src string, dest string) error { 13 14 mode, err := os.Stat(src) 15 if err != nil { 16 return err 17 } 18 19 if mode.IsDir() { 20 dirContents, err := ioutil.ReadDir(src) 21 if err != nil { 22 return err 23 } 24 25 for _, f := range dirContents { 26 srcFileaPath := filepath.Join(src, f.Name()) 27 destFilePath := filepath.Join(dest, f.Name()) 28 if err := CopyDir(srcFileaPath, destFilePath); err != nil { 29 return err 30 } 31 } 32 return nil 33 } 34 35 if err := os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil { 36 return err 37 } 38 39 return copySingleFile(src, dest, mode) 40 } 41 42 func copySingleFile(src string, dest string, mode os.FileInfo) error { 43 if src == dest { 44 return nil 45 } 46 47 // Ensure the file is not a directory 48 file, err := os.Stat(src) 49 if err != nil { 50 return err 51 } 52 if file.IsDir() { 53 return fmt.Errorf("unable to copy directories: %s", src) 54 } 55 56 srcFile, err := os.Open(src) 57 if err != nil { 58 return err 59 } 60 defer srcFile.Close() 61 62 destFile, err := os.Create(dest) 63 if err != nil { 64 return err 65 } 66 defer destFile.Close() 67 68 err = os.Chmod(destFile.Name(), mode.Mode()) 69 if err != nil { 70 return err 71 } 72 73 _, err = io.Copy(destFile, srcFile) 74 if err != nil { 75 return err 76 } 77 78 return nil 79 }