-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyfiles.go
57 lines (50 loc) · 1.05 KB
/
copyfiles.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 main
import (
"fmt"
"io"
"os"
"log"
"path/filepath"
)
func copyFile(srcPath, dstPath string) error {
srcFile, err := os.Open(srcPath)
if err != nil {
return err
}
defer srcFile.Close()
if _, err := os.Stat(dstPath); err == nil {
fmt.Printf("Overwriting file: %s\n", dstPath)
} else {
fmt.Printf("Creating new file: %s\n", dstPath)
}
dstFile, err := os.Create(dstPath)
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
return err
}
func main() {
srcDir := "/opt/cni/bin/"
dstDir := "/host/opt/cni/bin/"
err := filepath.Walk(srcDir, func(srcPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
dstPath := filepath.Join(dstDir, info.Name())
err = copyFile(srcPath, dstPath)
if err != nil {
log.Printf("Error copying %s to %s: %v", srcPath, dstPath, err)
} else {
log.Printf("Copied %s to %s", srcPath, dstPath)
}
return nil
})
if err != nil {
log.Fatalf("Error walking the source directory: %v", err)
}
}