From 104aa512600682f8221ed2c6e0e3538336a969f3 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 22 Oct 2024 04:37:28 -0500 Subject: [PATCH] add JSON export Signed-off-by: Jeff Carr --- configfile/main.go | 7 +++++++ helpers.go | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/configfile/main.go b/configfile/main.go index eba2dd8..abc306a 100644 --- a/configfile/main.go +++ b/configfile/main.go @@ -36,6 +36,13 @@ func main() { for _, h := range aCluster.Hypervisors { 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) { diff --git a/helpers.go b/helpers.go index 4148a23..e1a1bf9 100644 --- a/helpers.go +++ b/helpers.go @@ -1,6 +1,9 @@ package virtbuf import "fmt" +import ( + "google.golang.org/protobuf/encoding/protojson" +) func (x *Hypervisor) SetMemoryGB(gb int) { x.Memory = int64(gb * 1024 * 1024 * 1024) @@ -10,3 +13,12 @@ func (x *Hypervisor) GetMemoryPrintable() string { i := x.Memory / (1024 * 1024 * 1024) 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) +}