Compare commits

..

2 Commits

Author SHA1 Message Date
Jeff Carr 77f7f39c25 load and save PB() funcs 2025-09-22 16:34:38 -05:00
Jeff Carr 0e805acc67 make the backup file .text.json 2025-09-21 20:41:30 -05:00
2 changed files with 102 additions and 38 deletions

38
load.go
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
@ -54,13 +53,44 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
return log.Errorf("could not load config file")
}
func LoadPB(pb proto.Message, argname string, protoname string) (string, error) {
var fullname string
if strings.HasPrefix(argname, "/") {
fullname = filepath.Join(argname, protoname+".pb")
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Warn("ConfigLoad() UserHomeDir() err", err)
return "", err
}
fullname = filepath.Join(homeDir, ".config", argname, protoname+".pb")
}
data, err := loadFile(fullname)
if err != nil {
log.Warn("LoadPB()", fullname, err)
// set pb.Filename that was attempted
return fullname, err
}
// Unmarshal()
if err = proto.Unmarshal(data, pb); err != nil {
log.Warn("LoadPB() file", fullname)
log.Warn("LoadPB() Unmarshal() err", err)
return fullname, err
}
return fullname, nil
}
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 +120,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
}

90
save.go
View File

@ -15,32 +15,14 @@ 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 {
// get pb.Filename if it is there in the .proto file
fullname, ok := GetFilename(pb)
if !ok {
return ErrProtoFilename
func SavePB(pb proto.Message, fullname string) error {
if !strings.HasSuffix(fullname, ".pb") {
// todo: append .text here?
return log.Errorf("%s needs to end in '.pb'", fullname)
}
dir, name := filepath.Split(fullname)
@ -51,11 +33,25 @@ func ConfigSaveWithHeader(pb proto.Message, header string) error {
return err
}
data, err := proto.Marshal(pb)
if err != nil {
return err
}
log.Infof("ConfigSave() filename=%s %d\n", fullname, len(data))
return configWrite(fullname, data)
}
func ConfigSaveWithHeader(pb proto.Message, header string) error {
var final error
if err := configTEXT(fullname, pb, header); err != nil {
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"
@ -63,25 +59,63 @@ func ConfigSaveWithHeader(pb proto.Message, header string) error {
final = err
}
}
*/
return final
}
func configTEXT(fullname string, pb proto.Message, header string) error {
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 == "" {
return fmt.Errorf("filename was blank")
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
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 {