-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
50 lines (42 loc) · 883 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
50
package main
import (
"io"
"os"
"path/filepath"
)
// copyDir non-recursively copies the contents of the directory src to the directory dst
func copyDir(src, dst string) error {
dir, err := os.Open(src)
if err != nil {
return err
}
contents, err := dir.Readdir(0)
if err != nil {
return err
}
for _, f := range contents {
if f.IsDir() || f.Mode()&os.ModeType > 0 {
continue
}
if err := copyFile(filepath.Join(src, f.Name()), dst); err != nil {
return err
}
}
return nil
}
// copyFile copies the file given by src to the directory dir
func copyFile(src, dir string) error {
name := filepath.Base(src)
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(filepath.Join(dir, name))
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
return err
}