forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
56 lines (51 loc) · 894 Bytes
/
vm.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
package virtualbox
import (
"bufio"
"io"
"strconv"
"strings"
)
type VM struct {
CPUs int
Memory int
}
func (d *Driver) getVMInfo(name string) (*VM, error) {
out, err := d.vbmOut("showvminfo", name, "--machinereadable")
if err != nil {
return nil, err
}
r := strings.NewReader(out)
return parseVMInfo(r)
}
func parseVMInfo(r io.Reader) (*VM, error) {
s := bufio.NewScanner(r)
vm := &VM{}
for s.Scan() {
line := s.Text()
if line == "" {
continue
}
res := reEqualLine.FindStringSubmatch(line)
if res == nil {
continue
}
switch key, val := res[1], res[2]; key {
case "cpus":
v, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
vm.CPUs = v
case "memory":
v, err := strconv.Atoi(val)
if err != nil {
return nil, err
}
vm.Memory = v
}
}
if err := s.Err(); err != nil {
return nil, err
}
return vm, nil
}