seperate config files for droplets, hypervisors & events

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-24 16:57:50 -05:00
parent dac27e31b5
commit 87b7bc17b3
9 changed files with 150 additions and 9 deletions

2
.gitignore vendored
View File

@ -2,4 +2,4 @@ go.*
*.pb.go *.pb.go
configfile/configfile example/example

View File

@ -6,7 +6,7 @@
all: droplet.pb.go hypervisor.pb.go cluster.pb.go events.pb.go all: droplet.pb.go hypervisor.pb.go cluster.pb.go events.pb.go
make -C configfile make -C example
vet: lint vet: lint
GO111MODULE=off go vet GO111MODULE=off go vet
@ -17,7 +17,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 make -C example goimports
redomod: redomod:
rm -f go.* rm -f go.*
@ -27,7 +27,7 @@ redomod:
clean: clean:
rm -f *.pb.go rm -f *.pb.go
-rm -f go.* -rm -f go.*
make -C configfile clean make -C example clean
droplet.pb.go: droplet.proto droplet.pb.go: droplet.proto
# protoc --go_out=. droplet.proto # protoc --go_out=. droplet.proto

131
config.go Normal file
View File

@ -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)
}

View File

@ -4,7 +4,9 @@ package virtbuf;
import "google/protobuf/any.proto"; import "google/protobuf/any.proto";
message Droplets { 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 { message Droplet {

View File

@ -1,6 +1,6 @@
build: build:
GO111MODULE=off go build GO111MODULE=off go build
./configfile ./example
goimports: goimports:
goimports -w *.go goimports -w *.go
@ -12,4 +12,4 @@ run:
go run *.go go run *.go
clean: clean:
-rm -f configfile -rm -f example

BIN
example/configfile Executable file

Binary file not shown.

View File

@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"github.com/google/uuid" "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 // 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) 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)
} }
@ -38,10 +37,12 @@ func (c *Cluster) FormatJSON() string {
func (c *Cluster) UnmarshalJSON(data []byte) error { func (c *Cluster) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, c) return protojson.Unmarshal(data, c)
} }
*/
// apparently this isn't supposed to be used? // apparently this isn't supposed to be used?
// https://protobuf.dev/reference/go/faq/#unstable-text // https://protobuf.dev/reference/go/faq/#unstable-text
// this is a shame because this is much nicer output than JSON Format() // this is a shame because this is much nicer output than JSON Format()
/*
func (c *Cluster) FormatTEXT() string { func (c *Cluster) FormatTEXT() string {
return prototext.Format(c) return prototext.Format(c)
} }
@ -49,6 +50,7 @@ func (c *Cluster) FormatTEXT() string {
func (d *Droplets) FormatTEXT() string { func (d *Droplets) FormatTEXT() string {
return prototext.Format(d) return prototext.Format(d)
} }
*/
func (c *Cluster) FindDroplet(name string) *Droplet { func (c *Cluster) FindDroplet(name string) *Droplet {
for _, d := range c.Droplets { for _, d := range c.Droplets {

View File

@ -1,6 +1,12 @@
syntax = "proto3"; syntax = "proto3";
package virtbuf; 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 { message Hypervisor {
string uuid = 1; string uuid = 1;
string hostname = 2; string hostname = 2;