-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
57 lines (46 loc) · 907 Bytes
/
files.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
package utils
import (
"io"
"os"
"path/filepath"
)
func CopyFile(source, destination string) error {
sourceFile, err := os.Open(source)
if err != nil {
return err
}
defer sourceFile.Close()
destinationFile, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
return err
}
return nil
}
func SafelyOverwriteFile(filePath string, content string) error {
tempFile, err := os.CreateTemp(filepath.Dir(filePath), "swervo_temp")
if err != nil {
return err
}
_, err = tempFile.WriteString(content)
if err != nil {
return err
}
err = tempFile.Sync()
if err != nil {
return err
}
err = tempFile.Close()
if err != nil {
return err
}
err = os.Rename(tempFile.Name(), filePath)
if err != nil {
return err
}
return nil
}