forked from shuveb/containers-the-hard-way
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
104 lines (87 loc) · 1.87 KB
/
utils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
)
const gockerHomePath = "/var/lib/gocker"
const gockerTempPath = gockerHomePath + "/tmp"
const gockerImagesPath = gockerHomePath + "/images"
const gockerContainersPath = "/var/run/gocker/containers"
const gockerNetNsPath = "/var/run/gocker/net-ns"
func doOrDie(err error) {
if err != nil {
log.Fatalf("Fatal error: %v\n", err)
}
}
func doOrDieWithMsg(err error, msg string) {
if err != nil {
log.Fatalf("Fatal error: %s: %v\n", msg, err)
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func createDirsIfDontExist(dirs []string) error {
for _, dir := range dirs {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0755); err != nil {
log.Printf("Error creating directory: %v\n", err)
return err
}
}
}
return nil
}
func initGockerDirs() (err error) {
dirs := []string {gockerHomePath, gockerTempPath, gockerImagesPath, gockerContainersPath}
return createDirsIfDontExist(dirs)
}
func getGockerHomeDir() string {
return gockerHomePath
}
func getGockerImagesPath() string {
return gockerImagesPath
}
func getGockerTempPath() string {
return gockerTempPath
}
func getGockerContainersPath() string {
return gockerContainersPath
}
func getGockerNetNsPath() string {
return gockerNetNsPath
}
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()
if _, err := io.Copy(out, in); err != nil {
return err
}
return nil
}
func parseManifest(manifestPath string, mani *manifest) error {
data, err := ioutil.ReadFile(manifestPath)
if err != nil {
return err
}
if err := json.Unmarshal(data, mani); err != nil {
return err
}
return nil
}