make the backup file .text.json

This commit is contained in:
Jeff Carr 2025-09-21 20:41:30 -05:00
parent 1dfac22abc
commit 0e805acc67
2 changed files with 51 additions and 41 deletions

View File

@ -42,7 +42,6 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
}
if strings.HasSuffix(fullname, ".text") {
fullname = strings.TrimSuffix(fullname, ".text")
fullname += ".json"
if err := loadJSON(pb, fullname); err != nil {
return err
@ -57,10 +56,10 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
func loadTEXT(pb proto.Message, fullname string) error {
var data []byte
var err error
SetFilename(pb, fullname)
if data, err = loadFile(fullname); err != nil {
log.Warn("config file failed to load", err)
// set pb.Filename that was attempted
SetFilename(pb, fullname)
return err
}
@ -90,13 +89,13 @@ func loadTEXT(pb proto.Message, fullname string) error {
return nil
}
// json files are backup Marshal() data in case .text Unmarshal() fails
// they always should have the ".text" filename in them
func loadJSON(pb proto.Message, fullname string) error {
var data []byte
var err error
if data, err = loadFile(fullname); err != nil {
log.Warn("config file failed to load", err)
// set pb.Filename that was attempted
SetFilename(pb, fullname)
return err
}

85
save.go
View File

@ -15,33 +15,41 @@ import (
var ErrProtoFilename error = log.Errorf("proto does not have Filename")
func ConfigSave(pb proto.Message) error {
// get pb.Filename if it is there in the .proto file
fullname, ok := GetFilename(pb)
if !ok {
return ErrProtoFilename
}
s := prototext.Format(pb)
dir, name := filepath.Split(fullname)
if name == "" {
return fmt.Errorf("filename was blank")
}
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
log.Infof("ConfigSave() filename=%s %d\n", fullname, len(s))
return configWrite(fullname, []byte(s))
// log.Infof("ConfigSave() filename=%s %d\n", fullname, len(s))
return saveTEXT(pb, "")
}
func ConfigSaveWithHeader(pb proto.Message, header string) error {
var final error
if err := saveTEXT(pb, header); err != nil {
final = err
}
if err := saveJSON(pb); err != nil {
final = err
}
/*
if strings.HasSuffix(fullname, ".text") {
fullname = strings.TrimSuffix(fullname, ".text")
fullname += ".json"
if err := configJSON(fullname, pb); err != nil {
final = err
}
}
*/
return final
}
func saveTEXT(pb proto.Message, header string) error {
// get pb.Filename if it is there in the .proto file
fullname, ok := GetFilename(pb)
if !ok {
return ErrProtoFilename
}
if !strings.HasSuffix(fullname, ".text") {
// todo: append .text here?
return log.Errorf("not .text file: %s", fullname)
}
dir, name := filepath.Split(fullname)
if name == "" {
@ -51,37 +59,40 @@ func ConfigSaveWithHeader(pb proto.Message, header string) error {
return err
}
var final error
if err := configTEXT(fullname, pb, header); err != nil {
final = err
}
if strings.HasSuffix(fullname, ".text") {
fullname = strings.TrimSuffix(fullname, ".text")
fullname += ".json"
if err := configJSON(fullname, pb); err != nil {
final = err
}
}
return final
}
func configTEXT(fullname string, pb proto.Message, header string) error {
s := prototext.Format(pb)
log.Infof("ConfigSave() filename=%s %d\n", fullname, len(s))
return configWrite(fullname, []byte(header+s))
}
func configJSON(fullname string, pb proto.Message) error {
func saveJSON(pb proto.Message) error {
// get pb.Filename if it is there in the .proto file
fullname, ok := GetFilename(pb)
if !ok {
return ErrProtoFilename
}
if !strings.HasSuffix(fullname, ".text") {
// todo: append .text here?
return log.Errorf("not .text file: %s", fullname)
}
dir, name := filepath.Split(fullname)
if name == "" {
return fmt.Errorf("filename was blank")
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
data := protojson.Format(pb)
fullname += ".json"
log.Infof("ConfigSave() filename=%s %d\n", fullname, len(data))
return configWrite(fullname, []byte(data))
}
func configWrite(fullname string, data []byte) error {
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
defer cfgfile.Close()
if err != nil {