2024-10-24 16:57:50 -05:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-10-24 18:22:31 -05:00
|
|
|
func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool {
|
|
|
|
if !d.WriteConfigJSON() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !d.WriteConfigTEXT() {
|
|
|
|
return false
|
|
|
|
}
|
2024-10-24 16:57:50 -05:00
|
|
|
|
2024-10-24 18:22:31 -05:00
|
|
|
if e.WriteConfigJSON() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if e.WriteConfigTEXT() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// export as json
|
2024-10-24 18:22:31 -05:00
|
|
|
func (e *Events) WriteConfigJSON() bool {
|
2024-10-24 16:57:50 -05:00
|
|
|
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)
|
2024-10-24 18:22:31 -05:00
|
|
|
return false
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
text := e.FormatJSON()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
fmt.Println("Write:", fullname, "OK")
|
2024-10-24 18:22:31 -05:00
|
|
|
return true
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// export as prototext
|
2024-10-24 18:22:31 -05:00
|
|
|
func (e *Events) WriteConfigTEXT() bool {
|
2024-10-24 16:57:50 -05:00
|
|
|
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)
|
2024-10-24 18:22:31 -05:00
|
|
|
return false
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
text := e.FormatTEXT()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
fmt.Println("Write:", fullname, "OK")
|
2024-10-24 18:22:31 -05:00
|
|
|
return true
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// export as json
|
2024-10-24 18:22:31 -05:00
|
|
|
func (d *Droplets) WriteConfigJSON() bool {
|
2024-10-24 16:57:50 -05:00
|
|
|
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)
|
2024-10-24 18:22:31 -05:00
|
|
|
return false
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
text := d.FormatJSON()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
fmt.Println("Write:", fullname, "OK")
|
2024-10-24 18:22:31 -05:00
|
|
|
return true
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// export as prototext
|
2024-10-24 18:22:31 -05:00
|
|
|
func (d *Droplets) WriteConfigTEXT() bool {
|
2024-10-24 16:57:50 -05:00
|
|
|
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)
|
2024-10-24 18:22:31 -05:00
|
|
|
return false
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
text := d.FormatTEXT()
|
|
|
|
fmt.Fprintln(cfgfile, text)
|
|
|
|
fmt.Println("Write:", fullname, "OK")
|
2024-10-24 18:22:31 -05:00
|
|
|
return true
|
2024-10-24 16:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|