add JSON export

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-22 04:37:28 -05:00
parent ca0d4f423a
commit 104aa51260
2 changed files with 19 additions and 0 deletions

View File

@ -36,6 +36,13 @@ func main() {
for _, h := range aCluster.Hypervisors { for _, h := range aCluster.Hypervisors {
log.Println("\thypervisor =", h.Hostname, h.GetMemoryPrintable()) log.Println("\thypervisor =", h.Hostname, h.GetMemoryPrintable())
} }
b, err := aCluster.MarshalJSON()
if err != nil {
log.Println("json failed")
} else {
log.Println(string(b))
}
} }
func marshalWriteToFile(myWriter *bufio.Writer, c *pb.Cluster) { func marshalWriteToFile(myWriter *bufio.Writer, c *pb.Cluster) {

View File

@ -1,6 +1,9 @@
package virtbuf package virtbuf
import "fmt" import "fmt"
import (
"google.golang.org/protobuf/encoding/protojson"
)
func (x *Hypervisor) SetMemoryGB(gb int) { func (x *Hypervisor) SetMemoryGB(gb int) {
x.Memory = int64(gb * 1024 * 1024 * 1024) x.Memory = int64(gb * 1024 * 1024 * 1024)
@ -10,3 +13,12 @@ func (x *Hypervisor) GetMemoryPrintable() string {
i := x.Memory / (1024 * 1024 * 1024) i := x.Memory / (1024 * 1024 * 1024)
return fmt.Sprintf("%d GB", i) return fmt.Sprintf("%d GB", i)
} }
func (c *Cluster) MarshalJSON() ([]byte, error) {
return protojson.Marshal(c)
}
func (c *Cluster) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, c)
}