-
Notifications
You must be signed in to change notification settings - Fork 2
/
inspect.go
56 lines (48 loc) · 948 Bytes
/
inspect.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 vm
import (
"encoding/json"
"encoding/xml"
"fmt"
"strings"
"github.com/libvirt/libvirt-go"
)
// Inspect prints detailed information about the given domain.
func Inspect(uri, name, outputformat string) error {
conn, err := libvirt.NewConnect(uri)
if err != nil {
return err
}
dom, err := conn.LookupDomainByName(name)
if err != nil {
return err
}
defer dom.Free()
data, err := dom.GetXMLDesc(libvirt.DOMAIN_XML_SECURE)
if err != nil {
return err
}
var d domain
err = xml.Unmarshal([]byte(data), &d)
if err != nil {
return err
}
var output []byte
switch outputformat {
case "xml":
output, err = xml.MarshalIndent(d, "", "\t")
if err != nil {
return err
}
case "json":
output, err = json.MarshalIndent(d, "", "\t")
if err != nil {
return err
}
case "raw":
output = []byte(data)
default:
output = []byte(d.String())
}
fmt.Println(strings.TrimSpace(string(output)))
return nil
}