-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_file.go
51 lines (42 loc) · 978 Bytes
/
move_file.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
package main
import (
"io"
"os"
log "github.com/sirupsen/logrus"
)
func moveFile(oldPath string, newPath string) error {
// Open original file
originalFile, err := os.Open(oldPath)
if err != nil {
log.Errorf("Failed to open original file: %v", err)
return err
}
defer originalFile.Close()
// Create new file
newFile, err := os.Create(newPath)
if err != nil {
log.Errorf("Failed to create new file: %v", err)
return err
}
defer newFile.Close()
// Copy the bytes to destination from source
_, err = io.Copy(newFile, originalFile)
if err != nil {
log.Errorf("Failed to copy file: %v", err)
return err
}
// Commit the file contents
err = newFile.Sync()
if err != nil {
log.Errorf("Failed to sync file: %v", err)
return err
}
// Remove original file
err = os.Remove(oldPath)
if err != nil {
log.Errorf("Failed to remove original file: %v", err)
return err
}
log.Infof("Moved file from %v to %v", oldPath, newPath)
return nil
}