summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile6
-rw-r--r--config.go131
-rw-r--r--droplet.proto4
-rw-r--r--example/Makefile (renamed from configfile/Makefile)4
-rwxr-xr-xexample/configfilebin0 -> 5544235 bytes
-rw-r--r--example/main.go (renamed from configfile/main.go)0
-rw-r--r--helpers.go6
-rw-r--r--hypervisor.proto6
9 files changed, 150 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
index c741eed..6cce53e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,4 @@ go.*
*.pb.go
-configfile/configfile
+example/example
diff --git a/Makefile b/Makefile
index fc490a4..9f4d4ce 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
all: droplet.pb.go hypervisor.pb.go cluster.pb.go events.pb.go
- make -C configfile
+ make -C example
vet: lint
GO111MODULE=off go vet
@@ -17,7 +17,7 @@ lint:
# autofixes your import headers in your golang files
goimports:
goimports -w *.go
- make -C configfile goimports
+ make -C example goimports
redomod:
rm -f go.*
@@ -27,7 +27,7 @@ redomod:
clean:
rm -f *.pb.go
-rm -f go.*
- make -C configfile clean
+ make -C example clean
droplet.pb.go: droplet.proto
# protoc --go_out=. droplet.proto
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..192d60d
--- /dev/null
+++ b/config.go
@@ -0,0 +1,131 @@
+package virtbuf
+
+// functions to import and export the protobuf
+// data to and from config files
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "google.golang.org/protobuf/encoding/protojson"
+ "google.golang.org/protobuf/encoding/prototext"
+)
+
+func WriteConfig(d *Droplets, h *Hypervisors, e *Events) {
+ d.WriteConfigJSON()
+ d.WriteConfigTEXT()
+
+ e.WriteConfigJSON()
+ e.WriteConfigTEXT()
+}
+
+// export as json
+func (e *Events) WriteConfigJSON() {
+ fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json")
+ cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
+ defer cfgfile.Close()
+ if err != nil {
+ fmt.Println("open config file :", err)
+ return
+ }
+ text := e.FormatJSON()
+ fmt.Fprintln(cfgfile, text)
+ fmt.Println("Write:", fullname, "OK")
+}
+
+// export as prototext
+func (e *Events) WriteConfigTEXT() {
+ fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.text")
+ cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
+ defer cfgfile.Close()
+ if err != nil {
+ fmt.Println("open config file :", err)
+ return
+ }
+ text := e.FormatTEXT()
+ fmt.Fprintln(cfgfile, text)
+ fmt.Println("Write:", fullname, "OK")
+}
+
+// export as json
+func (d *Droplets) WriteConfigJSON() {
+ fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.json")
+ cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
+ defer cfgfile.Close()
+ if err != nil {
+ fmt.Println("open config file :", err)
+ return
+ }
+ text := d.FormatJSON()
+ fmt.Fprintln(cfgfile, text)
+ fmt.Println("Write:", fullname, "OK")
+}
+
+// export as prototext
+func (d *Droplets) WriteConfigTEXT() {
+ fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.text")
+ cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
+ defer cfgfile.Close()
+ if err != nil {
+ fmt.Println("open config file :", err)
+ return
+ }
+ text := d.FormatTEXT()
+ fmt.Fprintln(cfgfile, text)
+ fmt.Println("Write:", fullname, "OK")
+}
+
+// human readable JSON
+func (c *Cluster) FormatJSON() string {
+ return protojson.Format(c)
+}
+
+func (d *Droplets) FormatJSON() string {
+ return protojson.Format(d)
+}
+
+func (e *Events) FormatJSON() string {
+ return protojson.Format(e)
+}
+
+// apparently this isn't supposed to be used?
+// https://protobuf.dev/reference/go/faq/#unstable-text
+// this is a shame because this is much nicer output than JSON Format()
+func (c *Cluster) FormatTEXT() string {
+ return prototext.Format(c)
+}
+
+func (d *Droplets) FormatTEXT() string {
+ return prototext.Format(d)
+}
+
+func (e *Events) FormatTEXT() string {
+ return prototext.Format(e)
+}
+
+// marshal
+func (c *Cluster) MarshalJSON() ([]byte, error) {
+ return protojson.Marshal(c)
+}
+
+func (d *Droplets) MarshalJSON() ([]byte, error) {
+ return protojson.Marshal(d)
+}
+
+func (e *Events) MarshalJSON() ([]byte, error) {
+ return protojson.Marshal(e)
+}
+
+// unmarshal
+func (c *Cluster) UnmarshalJSON(data []byte) error {
+ return protojson.Unmarshal(data, c)
+}
+
+func (d *Droplets) UnmarshalJSON(data []byte) error {
+ return protojson.Unmarshal(data, d)
+}
+
+func (e *Events) UnmarshalJSON(data []byte) error {
+ return protojson.Unmarshal(data, e)
+}
diff --git a/droplet.proto b/droplet.proto
index 9dbba2c..60af988 100644
--- a/droplet.proto
+++ b/droplet.proto
@@ -4,7 +4,9 @@ package virtbuf;
import "google/protobuf/any.proto";
message Droplets {
- repeated Droplet droplets = 1;
+ string uuid = 1; // I guess why not just have this on each file
+ string version = 2; // maybe can be used for protobuf schema change violations
+ repeated Droplet droplets = 3;
}
message Droplet {
diff --git a/configfile/Makefile b/example/Makefile
index a52de6a..ba930d7 100644
--- a/configfile/Makefile
+++ b/example/Makefile
@@ -1,6 +1,6 @@
build:
GO111MODULE=off go build
- ./configfile
+ ./example
goimports:
goimports -w *.go
@@ -12,4 +12,4 @@ run:
go run *.go
clean:
- -rm -f configfile
+ -rm -f example
diff --git a/example/configfile b/example/configfile
new file mode 100755
index 0000000..81377e3
--- /dev/null
+++ b/example/configfile
Binary files differ
diff --git a/configfile/main.go b/example/main.go
index d54803d..d54803d 100644
--- a/configfile/main.go
+++ b/example/main.go
diff --git a/helpers.go b/helpers.go
index 8df7040..eb54261 100644
--- a/helpers.go
+++ b/helpers.go
@@ -4,8 +4,6 @@ import (
"fmt"
"github.com/google/uuid"
- "google.golang.org/protobuf/encoding/protojson"
- "google.golang.org/protobuf/encoding/prototext"
)
// can the json protobuf output use a string and have a type handler
@@ -27,6 +25,7 @@ func (x *Hypervisor) GetMemoryPrintable() string {
return fmt.Sprintf("%d GB", i)
}
+/*
func (c *Cluster) MarshalJSON() ([]byte, error) {
return protojson.Marshal(c)
}
@@ -38,10 +37,12 @@ func (c *Cluster) FormatJSON() string {
func (c *Cluster) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, c)
}
+*/
// apparently this isn't supposed to be used?
// https://protobuf.dev/reference/go/faq/#unstable-text
// this is a shame because this is much nicer output than JSON Format()
+/*
func (c *Cluster) FormatTEXT() string {
return prototext.Format(c)
}
@@ -49,6 +50,7 @@ func (c *Cluster) FormatTEXT() string {
func (d *Droplets) FormatTEXT() string {
return prototext.Format(d)
}
+*/
func (c *Cluster) FindDroplet(name string) *Droplet {
for _, d := range c.Droplets {
diff --git a/hypervisor.proto b/hypervisor.proto
index 3d2d87d..7e9d6be 100644
--- a/hypervisor.proto
+++ b/hypervisor.proto
@@ -1,6 +1,12 @@
syntax = "proto3";
package virtbuf;
+message Hypervisors {
+ string uuid = 1; // I guess why not just have this on each file
+ string version = 2; // maybe can be used for protobuf schema change violations
+ repeated Hypervisor hypervisors = 3;
+}
+
message Hypervisor {
string uuid = 1;
string hostname = 2;