-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
91 lines (79 loc) · 1.61 KB
/
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
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
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/lxc/lxd"
log "github.com/sirupsen/logrus"
)
const (
// ERROR is logrus' error level
ERROR = log.ErrorLevel
// FATAL is logrus' fatal level
FATAL = log.FatalLevel
// WARN is logrus' warning level
WARN = log.WarnLevel
)
func asrt(iface interface{}, err error) interface{} {
if err != nil {
log.Errorf("ERROR: %v", err)
}
return iface
}
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func dirExists(name string) bool {
if d, err := os.Stat(name); err != nil || !d.IsDir() {
return false
}
return true
}
// Copy copies files into/out of containers
func Copy(src, dst string) (int64, error) {
srcFile, err := os.Open(src)
if err != nil {
return 0, err
}
defer srcFile.Close()
srcFileStat, err := srcFile.Stat()
if err != nil {
return 0, err
}
if !srcFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
dstFile, err := os.Create(dst)
if err != nil {
return 0, err
}
defer dstFile.Close()
return io.Copy(dstFile, srcFile)
}
func hasExtension(client *lxd.Client, extension string) bool {
r := false
s, _ := client.ServerStatus()
for _, ext := range s.APIExtensions {
if ext == extension {
r = true
break
}
}
return r
}
func splitFilePath(path string) (contextPath, containerPath string, err error) {
split := strings.SplitN(path, ":", 2)
if len(split) != 2 {
err = fmt.Errorf("Incorrect file path format: %s", path)
return
}
contextPath = split[0]
containerPath = split[1]
return
}