diff options
| -rw-r--r-- | load.go | 7 | ||||
| -rw-r--r-- | save.go | 71 |
2 files changed, 44 insertions, 34 deletions
@@ -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 } @@ -15,33 +15,66 @@ import ( var ErrProtoFilename error = log.Errorf("proto does not have Filename") func ConfigSave(pb proto.Message) error { + // 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 } - - s := prototext.Format(pb) + 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") } - err := os.MkdirAll(dir, os.ModePerm) - if err != nil { + 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(s)) + return configWrite(fullname, []byte(header+s)) } -func ConfigSaveWithHeader(pb proto.Message, header string) 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 == "" { @@ -51,37 +84,15 @@ 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 { 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 { |
