cleaner config file handling
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
f8981ab3c1
commit
a8484013dc
111
config.go
111
config.go
|
@ -10,8 +10,103 @@ import (
|
|||
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/encoding/prototext"
|
||||
"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")
|
||||
}
|
||||
|
||||
// read in the events log file
|
||||
// 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)
|
||||
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
|
||||
}
|
||||
msg, ok := a.(protoreflect.ProtoMessage)
|
||||
if !ok {
|
||||
return fmt.Errorf("provided value does not implement protoreflect.ProtoMessage")
|
||||
}
|
||||
text := protojson.Format(msg)
|
||||
fmt.Fprintln(cfgfile, text)
|
||||
return nil
|
||||
}
|
||||
|
||||
func ConfigWriteTEXT(a any, filename string) error {
|
||||
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), filename)
|
||||
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
|
||||
}
|
||||
msg, ok := a.(protoreflect.ProtoMessage)
|
||||
if !ok {
|
||||
return fmt.Errorf("provided value does not implement protoreflect.ProtoMessage")
|
||||
}
|
||||
text := prototext.Format(msg)
|
||||
fmt.Fprintln(cfgfile, text)
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool {
|
||||
if !d.WriteConfigJSON() {
|
||||
return false
|
||||
|
@ -20,10 +115,10 @@ func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
if e.WriteConfigJSON() {
|
||||
if err := e.WriteConfigJSON(); err != nil {
|
||||
return false
|
||||
}
|
||||
if e.WriteConfigTEXT() {
|
||||
if err := e.WriteConfigTEXT(); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -48,33 +143,33 @@ func ReadEventsConfig() (*Events, error) {
|
|||
}
|
||||
|
||||
// export as json
|
||||
func (e *Events) WriteConfigJSON() bool {
|
||||
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 false
|
||||
return err
|
||||
}
|
||||
text := e.FormatJSON()
|
||||
fmt.Fprintln(cfgfile, text)
|
||||
fmt.Println("Write:", fullname, "OK")
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
// export as prototext
|
||||
func (e *Events) WriteConfigTEXT() bool {
|
||||
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 false
|
||||
return err
|
||||
}
|
||||
text := e.FormatTEXT()
|
||||
fmt.Fprintln(cfgfile, text)
|
||||
fmt.Println("Write:", fullname, "OK")
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
// export as json
|
||||
|
|
Loading…
Reference in New Issue