using new config package

This commit is contained in:
Jeff Carr 2025-09-11 02:19:56 -05:00
parent 41fe3edc93
commit 92d7a71f12
1 changed files with 22 additions and 1 deletions

23
save.go
View File

@ -19,7 +19,6 @@ func ConfigSave(pb proto.Message) error {
return ErrProtoFilename return ErrProtoFilename
} }
// Unmarshal()
s := prototext.Format(pb) s := prototext.Format(pb)
dir, name := filepath.Split(fullname) dir, name := filepath.Split(fullname)
@ -35,6 +34,28 @@ func ConfigSave(pb proto.Message) error {
return configWrite(fullname, []byte(s)) return configWrite(fullname, []byte(s))
} }
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
}
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(header+s))
}
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)