forked from kreuzwerker/envplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
49 lines (33 loc) · 720 Bytes
/
util.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
package envplate
import (
"fmt"
"io"
"os"
)
func createBackup(file string) error {
source, err := os.Open(file)
if err != nil {
return err
}
defer source.Close()
target, err := os.Create(fmt.Sprintf("%s.bak", file))
if err != nil {
return err
}
defer target.Close()
if _, err := io.Copy(target, source); err != nil {
return err
} else if mode, err := filemode(source.Name()); err != nil {
return err
} else if err := os.Chmod(target.Name(), mode); err != nil {
return err
}
return nil
}
func filemode(file string) (os.FileMode, error) {
fileinfo, err := os.Stat(file)
if err != nil {
return 0, fmt.Errorf("cannot stat '%s': %v", file, err)
}
return fileinfo.Mode(), nil
}