-
Notifications
You must be signed in to change notification settings - Fork 2
/
capabilities.go
57 lines (48 loc) · 1013 Bytes
/
capabilities.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
package vm
import (
"encoding/json"
"encoding/xml"
"fmt"
"strings"
"github.com/libvirt/libvirt-go"
)
// Capabilities prints detailed information about the host hypervisor capabilities
func Capabilities(uri string, outputformat string) error {
cap, err := getCapabilities(uri)
if err != nil {
return err
}
var output []byte
switch outputformat {
case "json":
output, err = json.MarshalIndent(cap, "", "\t")
if err != nil {
return err
}
case "xml":
output, err = xml.MarshalIndent(cap, "", "\t")
if err != nil {
return err
}
default:
return UnsupportedFormatErr{outputformat}
}
fmt.Println(strings.TrimSpace(string(output)))
return nil
}
func getCapabilities(uri string) (*capabilities, error) {
conn, err := libvirt.NewConnect(uri)
if err != nil {
return nil, err
}
data, err := conn.GetCapabilities()
if err != nil {
return nil, err
}
var cap capabilities
err = xml.Unmarshal([]byte(data), &cap)
if err != nil {
return nil, err
}
return &cap, nil
}