summaryrefslogtreecommitdiff
path: root/save.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-20 05:50:38 -0500
committerJeff Carr <[email protected]>2025-10-20 05:50:38 -0500
commit8a24584262a90956e012cee7b03c8c2b9f4b794c (patch)
tree8b7d0afd04e717d7c07da5eb2894debd74ec2307 /save.go
parentb6e93c08d601a7a6c27a0fdcdf98f6cb7dc9ccd8 (diff)
reworking this to make it more sane. hopefully.
Diffstat (limited to 'save.go')
-rw-r--r--save.go162
1 files changed, 0 insertions, 162 deletions
diff --git a/save.go b/save.go
deleted file mode 100644
index d257649..0000000
--- a/save.go
+++ /dev/null
@@ -1,162 +0,0 @@
-package config
-
-import (
- "fmt"
- "os"
- "path/filepath"
- "strings"
-
- "google.golang.org/protobuf/encoding/protojson"
- "google.golang.org/protobuf/encoding/prototext"
- "google.golang.org/protobuf/proto"
-)
-
-var ErrProtoFilename error = fmt.Errorf("proto does not have Filename")
-
-func ConfigSave(pb proto.Message) error {
- return saveTEXT(pb, "")
-}
-
-// writes the protobuf to disk
-// uses the already configured Filename
-func SavePB(pb proto.Message) error {
- fullname, err := GetFilename(pb)
- if err != nil {
- return err
- }
- return SaveToFilename(pb, fullname)
-}
-
-// writes the protobuf to disk (sets Filename if PB has 'Filename')
-func SaveToFilename(pb proto.Message, fullname string) error {
- basedir, _ := filepath.Split(fullname)
- if err := os.MkdirAll(basedir, os.ModePerm); err != nil {
- return err
- }
- if strings.HasSuffix(fullname, ".pb") {
- return saveProto(pb, fullname)
- }
- if strings.HasSuffix(fullname, ".text") {
- return saveTEXT(pb, "")
- }
- if strings.HasSuffix(fullname, ".json") {
- return saveJSON(pb)
- }
- return fmt.Errorf("unknown filetype '%s'", fullname)
-}
-
-func saveProto(pb proto.Message, fullname string) error {
- if !strings.HasSuffix(fullname, ".pb") {
- // todo: append .text here?
- return fmt.Errorf("%s needs to end in '.pb'", 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, err := proto.Marshal(pb)
- if err != nil {
- return err
- }
-
- return configWrite(fullname, data)
-}
-
-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, err := GetFilename(pb)
- if err != nil {
- return err
- }
- fullname = strings.TrimSpace(fullname)
- if fullname == "" {
- return fmt.Errorf("saveTEXT() pb.Filename was blank")
- }
-
- if !strings.HasSuffix(fullname, ".text") {
- // todo: append .text here?
- return fmt.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 := prototext.Format(pb)
-
- err = configWrite(fullname, []byte(data))
- if err != nil {
- return err
- }
- return nil
-}
-
-func saveJSON(pb proto.Message) error {
- // get pb.Filename if it is there in the .proto file
- fullname, err := GetFilename(pb)
- if err != nil {
- return err
- }
- if !strings.HasSuffix(fullname, ".text") {
- // todo: append .text here?
- return fmt.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"
-
- err = configWrite(fullname, []byte(data))
- if err != nil {
- return err
- }
- return nil
-}
-
-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 {
- return err
- }
- _, err = cfgfile.Write(data)
- return err
-}