From 32b0be61493099e3a687fdd3ff204c641084f474 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 26 Oct 2024 06:44:44 -0500 Subject: [PATCH] writes out 4 config files Signed-off-by: Jeff Carr --- config.go | 100 +++++++++++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/config.go b/config.go index 639844d..1f75372 100644 --- a/config.go +++ b/config.go @@ -13,15 +13,8 @@ import ( "google.golang.org/protobuf/reflect/protoreflect" ) -// save the events config file in both json and prototext -func (e *Events) ConfigSave() error { - if err := ConfigWriteJSON(e, "events.json"); err != nil { - return err - } - - return ConfigWriteTEXT(e, "events.text") -} - +// writes out the cluster information it seperate files +// to make it humanly possible to hand edit things as needed func (c *Cluster) ConfigSave() error { var d *Droplets d = new(Droplets) @@ -30,6 +23,10 @@ func (c *Cluster) ConfigSave() error { fmt.Println("droplets.json write failed") return err } + if err := ConfigWriteTEXT(d, "newdroplets.text"); err != nil { + fmt.Println("droplets.json write failed") + return err + } var h *Hypervisors h = new(Hypervisors) @@ -38,6 +35,10 @@ func (c *Cluster) ConfigSave() error { fmt.Println("hypervisors.json write failed") return err } + if err := ConfigWriteTEXT(h, "newhypervisors.text"); err != nil { + fmt.Println("hypervisors.json write failed") + return err + } var e *Events e = new(Events) @@ -46,60 +47,44 @@ func (c *Cluster) ConfigSave() error { fmt.Println("newEvents.json write failed") return err } + if err := ConfigWriteTEXT(h, "newEvents.text"); err != nil { + fmt.Println("newEvents.json write failed") + return err + } + + if err := ConfigWriteTEXT(c, "newCluster.text"); err != nil { + fmt.Println("newCluster.json write failed") + return err + } + + var newc Cluster + newc.Dirs = c.Dirs + newc.Droplets = nil + newc.Hypervisors = nil + newc.Events = nil + if err := ConfigWriteTEXT(&newc, "newSmallCluster.text"); err != nil { + fmt.Println("newSmallCluster.json write failed") + return err + } return nil } -// read in the events log file +func (c *Cluster) ConfigLoad() error { + if c == nil { + c = new(Cluster) + } + fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "newCluster.json") + data, err := os.ReadFile(fullname) + if err != nil { + // log.Info("open config file :", err) + return err + } + return c.UnmarshalJSON(data) +} + // reads in from the prototext file // prototext file formats are not garrenteed to be stable. todo: hammer that out -func (e *Events) ConfigRead() error { - fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.text") - data, err := os.ReadFile(fullname) - if err != nil { - return err - } - return prototext.Unmarshal(data, e) -} - -// get the list of droplets from the config file -func (d *Droplets) ConfigRead() error { - fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.json") - data, err := os.ReadFile(fullname) - if err != nil { - return err - } - return protojson.Unmarshal(data, d) - /* - err = d.UnmarshalJSON(data) - if err != nil { - return err - } - return nil - */ -} - -//func (e *Events) ConfigSave() error { -// return ConfigWriteJSON(e, "events.json") -//} - -// save the droplet settings in a config file -// uses 'protojson.Format' which is more or less human readable -func (d *Droplets) ConfigSave() error { - return ConfigWriteJSON(d, "droplets.json") - /* - 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 err - } - text := protojson.Format(d) - fmt.Fprintln(cfgfile, text) - return nil - */ -} func ConfigWriteJSON(a any, filename string) error { fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename) @@ -152,6 +137,7 @@ func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool { return true } + // read in events.json func ReadEventsConfig() (*Events, error) { e := new(Events)