-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Also add some helper function to handle the XML file for libvirt Disk Signed-off-by: Vicente Cheng <[email protected]> (cherry picked from commit ee1afaa)
- Loading branch information
1 parent
f3bac2a
commit f6f6f3d
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type Disk struct { | ||
XMLName xml.Name `xml:"disk"` | ||
Type string `xml:"type,attr"` | ||
Device string `xml:"device,attr"` | ||
Driver Driver `xml:"driver"` | ||
Source Source `xml:"source"` | ||
Target Target `xml:"target"` | ||
WWN string `xml:"wwn"` | ||
} | ||
|
||
type Driver struct { | ||
Name string `xml:"name,attr"` | ||
Type string `xml:"type,attr"` | ||
} | ||
|
||
type Source struct { | ||
File string `xml:"file,attr"` | ||
} | ||
|
||
type Target struct { | ||
Dev string `xml:"dev,attr"` | ||
Bus string `xml:"bus,attr"` | ||
} | ||
|
||
// DiskXMLReader can read the libvirt disk xml file and return a Disk struct | ||
func DiskXMLReader(filePath string) (Disk, error) { | ||
f, err := os.Open(filePath) | ||
if err != nil { | ||
return Disk{}, fmt.Errorf("open file(%s) error: %v", filePath, err) | ||
} | ||
|
||
defer f.Close() | ||
|
||
var disk Disk | ||
err = xml.NewDecoder(f).Decode(&disk) | ||
if err != nil { | ||
return Disk{}, fmt.Errorf("decode XML Error: %v", err) | ||
} | ||
|
||
return disk, nil | ||
} | ||
|
||
// XMLWriter write XML to target file, make sure your xmlData should valid | ||
func XMLWriter(targetFilePath string, xmlData any) error { | ||
// Create a new file for writing | ||
targetFile, err := os.Create(targetFilePath) | ||
if err != nil { | ||
return fmt.Errorf("create file(%s) error: %v", targetFilePath, err) | ||
} | ||
defer targetFile.Close() | ||
|
||
// Encode the disk data and write it to the output file | ||
encoder := xml.NewEncoder(targetFile) | ||
err = encoder.Encode(xmlData) | ||
if err != nil { | ||
return fmt.Errorf("encod XML Error: %v", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters