From e176a9b536077bcbebd19e0274218c3ca5d65ec9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 26 Oct 2024 07:28:33 -0500 Subject: [PATCH] config files in 4 pieces for human ease of use Signed-off-by: Jeff Carr --- config.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 1f75372..57fa319 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ package virtbuf // data to and from config files import ( + "errors" "fmt" "os" "path/filepath" @@ -43,11 +44,11 @@ func (c *Cluster) ConfigSave() error { var e *Events e = new(Events) e.Events = c.Events - if err := ConfigWriteJSON(h, "newEvents.json"); err != nil { + if err := ConfigWriteJSON(e, "newEvents.json"); err != nil { fmt.Println("newEvents.json write failed") return err } - if err := ConfigWriteTEXT(h, "newEvents.text"); err != nil { + if err := ConfigWriteTEXT(e, "newEvents.text"); err != nil { fmt.Println("newEvents.json write failed") return err } @@ -72,15 +73,79 @@ func (c *Cluster) ConfigSave() error { func (c *Cluster) ConfigLoad() error { if c == nil { - c = new(Cluster) + return errors.New("It's not safe to run ConfigLoad() on a nil cluster") } - fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "newCluster.json") + + // load the cluster config file + if data, err := loadFile("cluster.text"); err == nil { + if err = prototext.Unmarshal(data, c); err != nil { + fmt.Println("broken cluster.text config file") + fmt.Println(err) + return errors.New("cluster.text file is broken") + } + } else { + return err + } + + var d *Droplets + d = new(Droplets) + // load the droplet config file + if data, err := loadFile("droplets.json"); err == nil { + if err = protojson.Unmarshal(data, d); err != nil { + fmt.Println("broken droplets.json config file") + return err + } + } else { + return err + } + // copy them over. is this needed? does the memory free otherwise? + for _, drop := range d.Droplets { + c.Droplets = append(c.Droplets, drop) + } + + var h *Hypervisors + h = new(Hypervisors) + // load the hypervisors config file + if data, err := loadFile("hypervisors.json"); err == nil { + if err = protojson.Unmarshal(data, h); err != nil { + fmt.Println("broken hypervisors.json config file") + return err + } + } else { + fmt.Println("ERROR HERE IN Hypervisors") + return err + } + // copy them over. is this needed? does the memory free otherwise? + for _, a := range h.Hypervisors { + c.Hypervisors = append(c.Hypervisors, a) + } + + var e *Events + e = new(Events) + // load the events config file + if data, err := loadFile("events.json"); err == nil { + if err = protojson.Unmarshal(data, e); err != nil { + fmt.Println("broken events.json config file") + return err + } + } else { + return err + } + // copy them over. is this needed? does the memory free otherwise? + for _, a := range e.Events { + c.Events = append(c.Events, a) + } + return nil +} + +func loadFile(filename string) ([]byte, error) { + fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename) data, err := os.ReadFile(fullname) if err != nil { // log.Info("open config file :", err) - return err + return nil, err } - return c.UnmarshalJSON(data) + return data, nil } // reads in from the prototext file @@ -137,7 +202,6 @@ func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool { return true } - // read in events.json func ReadEventsConfig() (*Events, error) { e := new(Events)