seperate config files for droplets, hypervisors & events
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
dac27e31b5
commit
87b7bc17b3
|
@ -2,4 +2,4 @@ go.*
|
|||
|
||||
*.pb.go
|
||||
|
||||
configfile/configfile
|
||||
example/example
|
||||
|
|
6
Makefile
6
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
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
Binary file not shown.
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue