add prototext config format

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-22 06:19:24 -05:00
parent 104aa51260
commit ed7dd145f6
4 changed files with 39 additions and 23 deletions

View File

@ -15,7 +15,7 @@ all:
make droplet.pb.go
make hypervisor.pb.go
make cluster.pb.go
cd configfile && make
make -C configfile
vet:
GO111MODULE=off go vet
@ -26,6 +26,7 @@ lint:
# autofixes your import headers in your golang files
goimports:
goimports -w *.go
make -C configfile goimports
redomod:
rm -f go.*
@ -35,7 +36,7 @@ redomod:
clean:
rm -f *.pb.go
-rm -f go.*
cd configfile && make clean
make -C configfile clean
droplet.pb.go: droplet.proto
# protoc --go_out=. droplet.proto

View File

@ -2,6 +2,9 @@ build:
GO111MODULE=off go build
./configfile
goimports:
goimports -w *.go
prep:
go get -v -t -u

View File

@ -1,13 +1,17 @@
package main
import "log"
import "bytes"
import "os"
import "bufio"
import "io/ioutil"
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
import "google.golang.org/protobuf/proto"
import pb "go.wit.com/lib/protobuf/virtbuf"
"google.golang.org/protobuf/proto"
pb "go.wit.com/lib/protobuf/virtbuf"
)
//
// saves entries in a config file
@ -21,7 +25,7 @@ func main() {
log.Fatalln("Error reading file:", err)
}
var aCluster pb.Cluster
var aCluster pb.Cluster
if err := proto.Unmarshal(in, &aCluster); err != nil {
log.Fatalln("Failed to parse droplet:", err)
}
@ -29,20 +33,19 @@ func main() {
log.Println(aCluster.String())
// show the droplets to STDOUT
for _, d := range aCluster.Droplets {
log.Println("\tdroplet =", d.Hostname, "preffered host:", d.PreferredHypervisor)
fmt.Println("\tdroplet =", d.Hostname, "preffered host:", d.PreferredHypervisor)
}
// show the hypervisors to STDOUT
for _, h := range aCluster.Hypervisors {
log.Println("\thypervisor =", h.Hostname, h.GetMemoryPrintable())
fmt.Println("\thypervisor =", h.Hostname, h.GetMemoryPrintable())
}
b, err := aCluster.MarshalJSON()
if err != nil {
log.Println("json failed")
} else {
log.Println(string(b))
}
json := aCluster.FormatJSON()
fmt.Println(json)
text := aCluster.FormatTEXT()
fmt.Println(text)
}
func marshalWriteToFile(myWriter *bufio.Writer, c *pb.Cluster) {

View File

@ -1,8 +1,10 @@
package virtbuf
import "fmt"
import (
"google.golang.org/protobuf/encoding/protojson"
"fmt"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
)
func (x *Hypervisor) SetMemoryGB(gb int) {
@ -14,11 +16,18 @@ func (x *Hypervisor) GetMemoryPrintable() string {
return fmt.Sprintf("%d GB", i)
}
func (c *Cluster) MarshalJSON() ([]byte, error) {
return protojson.Marshal(c)
return protojson.Marshal(c)
}
func (c *Cluster) FormatJSON() string {
return protojson.Format(c)
}
func (c *Cluster) FormatTEXT() string {
return prototext.Format(c)
}
func (c *Cluster) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, c)
return protojson.Unmarshal(data, c)
}