rename and rm old code

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-30 11:06:14 -05:00
parent 6a0d4d3e38
commit 9608bb680f
3 changed files with 83 additions and 169 deletions

View File

@ -188,6 +188,6 @@ func (c *Cluster) DropletMoved(d *Droplet, newh *Hypervisor) error {
c.Events = append(c.Events, e) c.Events = append(c.Events, e)
// update the droplet record // update the droplet record
d.CurrentHypervisor = newh.Hostname d.CurrentHypervisor = newh.Hostname
return nil return nil
} }

169
config.go
View File

@ -88,6 +88,7 @@ func backupConfigFiles() error {
return backupFiles(srcDir, destDir) return backupFiles(srcDir, destDir)
} }
/*
func (c *Cluster) ConfigLoadOld2() error { func (c *Cluster) ConfigLoadOld2() error {
if c == nil { if c == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil cluster") return errors.New("It's not safe to run ConfigLoad() on a nil cluster")
@ -124,6 +125,7 @@ func (c *Cluster) ConfigLoadOld2() error {
} }
return nil return nil
} }
*/
func (c *Cluster) ConfigLoad() error { func (c *Cluster) ConfigLoad() error {
if c == nil { if c == nil {
@ -239,170 +241,3 @@ func ConfigWriteTEXT(a any, filename string) error {
fmt.Fprintln(cfgfile, text) fmt.Fprintln(cfgfile, text)
return nil return nil
} }
/*
func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool {
if !d.WriteConfigJSON() {
return false
}
if !d.WriteConfigTEXT() {
return false
}
if err := e.WriteConfigJSON(); err != nil {
return false
}
if err := e.WriteConfigTEXT(); err != nil {
return false
}
return true
}
// read in events.json
func ReadEventsConfig() (*Events, error) {
e := new(Events)
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json")
data, err := os.ReadFile(fullname)
if err != nil {
// log.Info("open config file :", err)
return nil, err
}
err = e.UnmarshalJSON(data)
if err != nil {
// log.Info("read json failed", err)
return nil, err
}
return e, nil
}
// export as json
func (e *Events) WriteConfigJSON() error {
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 err
}
text := e.FormatJSON()
fmt.Fprintln(cfgfile, text)
fmt.Println("Write:", fullname, "OK")
return nil
}
// export as prototext
func (e *Events) WriteConfigTEXT() error {
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 err
}
text := e.FormatTEXT()
fmt.Fprintln(cfgfile, text)
fmt.Println("Write:", fullname, "OK")
return nil
}
// export as json
func (d *Droplets) WriteConfigJSON() bool {
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 false
}
text := d.FormatJSON()
fmt.Fprintln(cfgfile, text)
fmt.Println("Write:", fullname, "OK")
return true
}
// export as prototext
func (d *Droplets) WriteConfigTEXT() bool {
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 false
}
text := d.FormatTEXT()
fmt.Fprintln(cfgfile, text)
fmt.Println("Write:", fullname, "OK")
return true
}
*/
// human readable JSON
func (c *Cluster) FormatJSON() string {
return protojson.Format(c)
}
func (d *Droplets) FormatJSON() string {
return protojson.Format(d)
}
func (d *Droplet) FormatJSON() string {
return protojson.Format(d)
}
func (e *Events) FormatJSON() string {
return protojson.Format(e)
}
func (h *Hypervisors) FormatJSON() string {
return protojson.Format(h)
}
// 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 (d *Droplet) 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 (d *Droplet) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, d)
}
func (e *Events) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, e)
}

79
helpers.go Normal file
View File

@ -0,0 +1,79 @@
package virtbuf
// functions to import and export the protobuf
// data to and from config files
import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
)
// human readable JSON
func (c *Cluster) FormatJSON() string {
return protojson.Format(c)
}
func (d *Droplets) FormatJSON() string {
return protojson.Format(d)
}
func (d *Droplet) FormatJSON() string {
return protojson.Format(d)
}
func (e *Events) FormatJSON() string {
return protojson.Format(e)
}
func (h *Hypervisors) FormatJSON() string {
return protojson.Format(h)
}
// 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 (d *Droplet) 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 (d *Droplet) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, d)
}
func (e *Events) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, e)
}