-
Notifications
You must be signed in to change notification settings - Fork 11
/
writer.go
51 lines (43 loc) · 1.03 KB
/
writer.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 images
import (
"github.com/mhewedy/vermin/db"
"github.com/mhewedy/vermin/images/vagrant"
"io"
"os"
"strings"
)
func writeNewImage(tmpFile *os.File, imageName string) error {
imageName = strings.Replace(imageName, "vagrant/", "", 1)
// copy the downloaded file to images directory
parts := strings.Split(imageName, "/")
if err := os.MkdirAll(db.ImagesDir+"/"+strings.Join(parts[0:len(parts)-1], "/"), 0755); err != nil {
return err
}
imagePath := db.ImagesDir + "/" + imageName + ova
if err := copyFile(tmpFile.Name(), imagePath); err != nil {
return err
}
if err := vagrant.ProcessImage(imagePath); err != nil {
return err
}
return nil
}
// Copy the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}