Compare commits

..

No commits in common. "master" and "v0.0.3" have entirely different histories.

2 changed files with 43 additions and 107 deletions

38
load.go
View File

@ -42,6 +42,7 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
} }
if strings.HasSuffix(fullname, ".text") { if strings.HasSuffix(fullname, ".text") {
fullname = strings.TrimSuffix(fullname, ".text")
fullname += ".json" fullname += ".json"
if err := loadJSON(pb, fullname); err != nil { if err := loadJSON(pb, fullname); err != nil {
return err return err
@ -53,44 +54,13 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
return log.Errorf("could not load config file") 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 { func loadTEXT(pb proto.Message, fullname string) error {
var data []byte var data []byte
var err error var err error
SetFilename(pb, fullname)
if data, err = loadFile(fullname); err != nil { if data, err = loadFile(fullname); err != nil {
log.Warn("config file failed to load", err) log.Warn("config file failed to load", err)
// set pb.Filename that was attempted // set pb.Filename that was attempted
SetFilename(pb, fullname)
return err return err
} }
@ -120,13 +90,13 @@ func loadTEXT(pb proto.Message, fullname string) error {
return nil 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 { func loadJSON(pb proto.Message, fullname string) error {
var data []byte var data []byte
var err error var err error
if data, err = loadFile(fullname); err != nil { if data, err = loadFile(fullname); err != nil {
log.Warn("config file failed to load", err) log.Warn("config file failed to load", err)
// set pb.Filename that was attempted
SetFilename(pb, fullname)
return err return err
} }

90
save.go
View File

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