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

View File

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

View File

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

View File

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