diff --git a/load.go b/load.go index 711c1a5..427ae5e 100644 --- a/load.go +++ b/load.go @@ -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 } diff --git a/save.go b/save.go index 6231b3b..2d64d68 100644 --- a/save.go +++ b/save.go @@ -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 {