summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-10-22 06:19:24 -0500
committerJeff Carr <[email protected]>2024-10-22 06:19:24 -0500
commited7dd145f607edb60df43f457e7e0013f4647aba (patch)
tree5609e5bf746864852123876b564733dd5f9bda4a
parent104aa512600682f8221ed2c6e0e3538336a969f3 (diff)
add prototext config format
Signed-off-by: Jeff Carr <[email protected]>
-rw-r--r--Makefile5
-rw-r--r--configfile/Makefile3
-rw-r--r--configfile/main.go35
-rw-r--r--helpers.go19
4 files changed, 39 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index e6cd85a..28de8aa 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/configfile/Makefile b/configfile/Makefile
index 4f609e1..a52de6a 100644
--- a/configfile/Makefile
+++ b/configfile/Makefile
@@ -2,6 +2,9 @@ build:
GO111MODULE=off go build
./configfile
+goimports:
+ goimports -w *.go
+
prep:
go get -v -t -u
diff --git a/configfile/main.go b/configfile/main.go
index abc306a..e0beec3 100644
--- a/configfile/main.go
+++ b/configfile/main.go
@@ -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) {
diff --git a/helpers.go b/helpers.go
index e1a1bf9..5549358 100644
--- a/helpers.go
+++ b/helpers.go
@@ -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)
}