/
copy.go
110 lines (91 loc) · 2.46 KB
/
copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package builder
// Copy "recursivelies copy a file object from source to dest while perserving
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
)
// CopyFiles copies files from src to destination.
func CopyFiles(src, dest string) error {
info, err := os.Stat(src)
if err != nil {
return err
}
if info.IsDir() {
debugPrint(fmt.Sprintf("Creating directory: %s at %s", info.Name(), dest))
return copyDir(src, dest)
}
debugPrint(fmt.Sprintf("cp - %s %s", src, dest))
return copyFile(src, dest)
}
// copyDir will recursively copy a directory to dest
func copyDir(src, dest string) error {
info, err := os.Stat(src)
if err != nil {
return fmt.Errorf("error reading dest stats: %s", err.Error())
}
if err := os.MkdirAll(dest, info.Mode()); err != nil {
return fmt.Errorf("error creating path: %s - %s", dest, err.Error())
}
infos, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, info := range infos {
if err := CopyFiles(
filepath.Join(src, info.Name()),
filepath.Join(dest, info.Name()),
); err != nil {
return err
}
}
return nil
}
// copyFile will copy a file with the same mode as the src file
func copyFile(src, dest string) error {
info, err := os.Stat(src)
if err != nil {
return fmt.Errorf("error reading src file stats: %s", err.Error())
}
err = ensureBaseDir(dest)
if err != nil {
return fmt.Errorf("error creating dest base directory: %s", err.Error())
}
f, err := os.Create(dest)
if err != nil {
return fmt.Errorf("error creating dest file: %s", err.Error())
}
defer f.Close()
if err = os.Chmod(f.Name(), info.Mode()); err != nil {
return fmt.Errorf("error setting dest file mode: %s", err.Error())
}
s, err := os.Open(src)
if err != nil {
return fmt.Errorf("error opening src file: %s", err.Error())
}
defer s.Close()
_, err = io.Copy(f, s)
if err != nil {
return fmt.Errorf("Error copying dest file: %s\n" + err.Error())
}
return nil
}
// ensureBaseDir creates the base directory of the given file path, if needed.
// For example, if fpath is 'build/extras/dictionary.txt`, ensureBaseDir will
// make sure that the directory `buid/extras/` is created.
func ensureBaseDir(fpath string) error {
baseDir := path.Dir(fpath)
info, err := os.Stat(baseDir)
if err == nil && info.IsDir() {
return nil
}
return os.MkdirAll(baseDir, 0755)
}
func debugPrint(message string) {
if val, exists := os.LookupEnv("debug"); exists && (val == "1" || val == "true") {
fmt.Println(message)
}
}