writes out 4 config files

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-26 06:44:44 -05:00
parent ade54fdeda
commit 32b0be6149
1 changed files with 43 additions and 57 deletions

100
config.go
View File

@ -13,15 +13,8 @@ import (
"google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/reflect/protoreflect"
) )
// save the events config file in both json and prototext // writes out the cluster information it seperate files
func (e *Events) ConfigSave() error { // to make it humanly possible to hand edit things as needed
if err := ConfigWriteJSON(e, "events.json"); err != nil {
return err
}
return ConfigWriteTEXT(e, "events.text")
}
func (c *Cluster) ConfigSave() error { func (c *Cluster) ConfigSave() error {
var d *Droplets var d *Droplets
d = new(Droplets) d = new(Droplets)
@ -30,6 +23,10 @@ func (c *Cluster) ConfigSave() error {
fmt.Println("droplets.json write failed") fmt.Println("droplets.json write failed")
return err return err
} }
if err := ConfigWriteTEXT(d, "newdroplets.text"); err != nil {
fmt.Println("droplets.json write failed")
return err
}
var h *Hypervisors var h *Hypervisors
h = new(Hypervisors) h = new(Hypervisors)
@ -38,6 +35,10 @@ func (c *Cluster) ConfigSave() error {
fmt.Println("hypervisors.json write failed") fmt.Println("hypervisors.json write failed")
return err return err
} }
if err := ConfigWriteTEXT(h, "newhypervisors.text"); err != nil {
fmt.Println("hypervisors.json write failed")
return err
}
var e *Events var e *Events
e = new(Events) e = new(Events)
@ -46,60 +47,44 @@ func (c *Cluster) ConfigSave() error {
fmt.Println("newEvents.json write failed") fmt.Println("newEvents.json write failed")
return err 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 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 // reads in from the prototext file
// prototext file formats are not garrenteed to be stable. todo: hammer that out // 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 { func ConfigWriteJSON(a any, filename string) error {
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename) fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
@ -152,6 +137,7 @@ func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool {
return true return true
} }
// read in events.json // read in events.json
func ReadEventsConfig() (*Events, error) { func ReadEventsConfig() (*Events, error) {
e := new(Events) e := new(Events)