github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/config/module/copy_dir.go (about) 1 package module 2 3 import ( 4 "io" 5 "os" 6 "path/filepath" 7 "strings" 8 ) 9 10 // copyDir copies the src directory contents into dst. Both directories 11 // should already exist. 12 func copyDir(dst, src string) error { 13 src, err := filepath.EvalSymlinks(src) 14 if err != nil { 15 return err 16 } 17 18 walkFn := func(path string, info os.FileInfo, err error) error { 19 if err != nil { 20 return err 21 } 22 if path == src { 23 return nil 24 } 25 26 if strings.HasPrefix(filepath.Base(path), ".") { 27 // Skip any dot files 28 if info.IsDir() { 29 return filepath.SkipDir 30 } else { 31 return nil 32 } 33 } 34 35 // The "path" has the src prefixed to it. We need to join our 36 // destination with the path without the src on it. 37 dstPath := filepath.Join(dst, path[len(src):]) 38 39 // If we have a directory, make that subdirectory, then continue 40 // the walk. 41 if info.IsDir() { 42 if err := os.MkdirAll(dstPath, 0755); err != nil { 43 return err 44 } 45 46 return nil 47 } 48 49 // If we have a file, copy the contents. 50 srcF, err := os.Open(path) 51 if err != nil { 52 return err 53 } 54 defer srcF.Close() 55 56 dstF, err := os.Create(dstPath) 57 if err != nil { 58 return err 59 } 60 defer dstF.Close() 61 62 if _, err := io.Copy(dstF, srcF); err != nil { 63 return err 64 } 65 66 // Chmod it 67 return os.Chmod(dstPath, info.Mode()) 68 } 69 70 return filepath.Walk(src, walkFn) 71 }