summaryrefslogtreecommitdiff
path: root/xml.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-10-23 15:01:10 -0500
committerJeff Carr <[email protected]>2024-10-23 15:01:10 -0500
commit19880a81f3988a13ddd7bc5b457153e37477b7e7 (patch)
tree252eb086b13d8317b00c1df19b71e54f922aa584 /xml.go
parentb77eb0db4e38f1b3eaa40a8c5c9a0c20e6cb1fe9 (diff)
completely dump libvirtxml.Domain.Devices if empty
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'xml.go')
-rw-r--r--xml.go73
1 files changed, 69 insertions, 4 deletions
diff --git a/xml.go b/xml.go
index a72c88b..d2b271c 100644
--- a/xml.go
+++ b/xml.go
@@ -458,7 +458,7 @@ func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
var normalVideo bool = true
if domcfg.Devices.Videos != nil {
for _, v := range domcfg.Devices.Videos {
- if (v.Model.Type == "qxl") && (v.Model.VRam == 65536) {
+ if (v.Model.Type == "qxl") && (v.Model.VRam == 65536) {
// standard qxl video
} else {
fmt.Printf("? Video: %+v\n", v)
@@ -471,7 +471,13 @@ func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
domcfg.Devices.Videos = nil
}
- // dumpLibvirtxmlDomain()
+ // dumpLibvirtxmlDomainNames()
+ if libvirtxmlDomainEmpty(*domcfg.Devices) {
+ fmt.Println("Domain Devices are empty")
+ domcfg.Devices = nil
+ } else {
+ fmt.Println("Domain Devices are not empty")
+ }
updatedXML, err := xml.MarshalIndent(domcfg, "", " ")
if err != nil {
@@ -481,11 +487,11 @@ func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
// Print the updated XML to verify
fmt.Println(string(updatedXML))
- os.Exit(-1)
+ // os.Exit(-1)
}
// dump out all the fields in libvirtxml.DomainDeviceList
-func dumpLibvirtxmlDomain() {
+func dumpLibvirtxmlDomainNames() {
var domain libvirtxml.Domain
t := reflect.TypeOf(domain)
@@ -504,3 +510,62 @@ func dumpLibvirtxmlDomain() {
fmt.Println("DomainDeviceList:", field.Name)
}
}
+
+// dump out all the fields in libvirtxml.DomainDeviceList
+func libvirtxmlDomainEmpty(mydom libvirtxml.DomainDeviceList) bool {
+ var empty bool = true
+ // Get the reflection object of the variable
+ v := reflect.ValueOf(mydom)
+
+ // Ensure that we are working with a struct
+ if v.Kind() == reflect.Struct {
+ fmt.Println("Fields and values in libvirtxml.DomainDeviceList:")
+
+ // Loop through each field in the struct
+ for i := 0; i < v.NumField(); i++ {
+ // Get field name
+ field := v.Type().Field(i).Name
+
+ // Get field value
+ value := v.Field(i)
+
+ if !value.IsValid() {
+ fmt.Printf("Field: %s is nil or invalid\n", field)
+ continue
+ }
+
+ // Check if the field is a string, array, or slice
+ switch value.Kind() {
+ case reflect.String:
+ if value.String() != "" {
+ fmt.Printf("Field: %s is a String with value: %s\n", field, value.String())
+ empty = false
+ }
+ case reflect.Slice:
+ if value.Len() != 0 {
+ fmt.Printf("Field: %s is a Slice with length: %d\n", field, value.Len())
+ empty = false
+ }
+ case reflect.Array:
+ if value.Len() != 0 {
+ fmt.Printf("Field: %s is an Array with length: %d\n", field, value.Len())
+ empty = false
+ }
+ case reflect.Ptr:
+ if !value.IsValid() {
+ fmt.Println("Field ptr: value:", value)
+ fmt.Printf("Field ptr: %s is of type: %s\n", field, value.Kind())
+ empty = false
+ }
+ default:
+ fmt.Printf("Field: %s is of type: %s\n", field, value.Kind())
+ empty = false
+ }
+ // Print the field name and value
+ // fmt.Printf("Field: %s, Value: %v\n", field, value)
+ }
+ } else {
+ fmt.Println("Provided variable is not a struct.")
+ }
+ return empty
+}