From c1600cff482abf314bf66bcf758979969672bfca Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 21 Oct 2025 09:21:53 -0500 Subject: housecleaning & dump more old stuff --- errors.go | 6 ++- exit.go | 15 ------ generate.go | 9 ---- init.go | 45 ---------------- loadByAppname.go | 2 + loadCache.go | 3 -- loadConfig.go | 3 +- save.go | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ saveRaw.go | 162 ------------------------------------------------------- 9 files changed, 171 insertions(+), 236 deletions(-) delete mode 100644 exit.go delete mode 100644 generate.go delete mode 100644 init.go create mode 100644 save.go delete mode 100644 saveRaw.go diff --git a/errors.go b/errors.go index 154f3af..a2f7c66 100644 --- a/errors.go +++ b/errors.go @@ -6,9 +6,13 @@ import ( ) var NotInitialized error = errors.New("your application config not initialized") - var VersionMismatch error = errors.New("your PB version does not match") +var ErrEmpty error = fmt.Errorf("config file was empty") +var ErrMarshal error = fmt.Errorf("protobuf parse error") + +// more complicated errors. good idea or bad idea? + // Finally, declare a package-level variable of your new type. // This is the "sentinel" error that users will compare against. var ErrNotInitialized = NotInitializedError{} diff --git a/exit.go b/exit.go deleted file mode 100644 index 01ac9a5..0000000 --- a/exit.go +++ /dev/null @@ -1,15 +0,0 @@ -package config - -// this is an experiment at this point to -// see how this turns out - -import ( - "fmt" - "os" -) - -// todo: figure out how to pass this back to argv -func BadExit(reason string, err error) { - fmt.Println("lib/config bad exit. no app callback here. FIXME", reason, err) - os.Exit(-1) -} diff --git a/generate.go b/generate.go deleted file mode 100644 index c43e61b..0000000 --- a/generate.go +++ /dev/null @@ -1,9 +0,0 @@ -package config - -// 'make generate' will call this -// -// go generate is a cool feature. many thanks to the GO dev team -// -// NOTE: please add to go generate: if ! exists go.mod, run 'go mod init' & 'go mod tidy' -// -//go:generate autogenpb --proto config.proto diff --git a/init.go b/init.go deleted file mode 100644 index e9c5779..0000000 --- a/init.go +++ /dev/null @@ -1,45 +0,0 @@ -package config - -// this is an experiment at this point to -// see how this turns out - -/* -var configPB *Configs - -// these are normally what are sent from ldflags -var APPNAME string -var BUILDTIME string -var VERSION string - -var argv []string - -func Init(appname, version, buildtime string, fromargv []string) error { - APPNAME = appname - VERSION = version - BUILDTIME = buildtime - argv = fromargv - - return loadENV() -} - -func makeNewConfigFile(appname string) error { - configDir, err := os.UserConfigDir() - if err != nil { - fmt.Println("OS isn't returning UserConfigDir()", err) - return err - } - fullname := filepath.Join(configDir, appname, "config.text") - - configPB = NewConfigs() - configPB.Filename = fullname - - newvar := new(Config) - newvar.Key = "example config var" - newvar.Value = "protobufs are neat" - configPB.Clone(newvar) - - // writes the config file to disk - err = Save() - return err -} -*/ diff --git a/loadByAppname.go b/loadByAppname.go index cfa22cf..28f7fee 100644 --- a/loadByAppname.go +++ b/loadByAppname.go @@ -44,6 +44,7 @@ func LoadAppnameCache(pb proto.Message, appname string) error { return nil } +/* // deprecate func LoadByAppname(pb proto.Message, appname string) error { protoname, err := GetProtobufName(pb) // defined in the foo.proto file @@ -80,3 +81,4 @@ func LoadByAppname(pb proto.Message, appname string) error { } return nil } +*/ diff --git a/loadCache.go b/loadCache.go index f5cbef1..c45c6d7 100644 --- a/loadCache.go +++ b/loadCache.go @@ -14,9 +14,6 @@ import ( "google.golang.org/protobuf/proto" ) -var ErrEmpty error = fmt.Errorf("config file was empty") -var ErrMarshal error = fmt.Errorf("protobuf parse error") - // loads foo.proto from ~/.cache//foo.text func CacheLoad(pb proto.Message) error { appname, err := ENV.GetAppname() // already configured by your application diff --git a/loadConfig.go b/loadConfig.go index 8e0f5ab..43b40e1 100644 --- a/loadConfig.go +++ b/loadConfig.go @@ -84,7 +84,8 @@ func ConfigLoadByName(pb proto.Message, customName string) (string, error) { // return fullname, err } if curfilename == "" { - log.Printf("ConfigLoadByName() reading in %s\n", fullname) + // reading in new file + // log.Printf("ConfigLoadByName() reading in %s\n", fullname) } if err = loadTEXT(pb, fullname); err == nil { diff --git a/save.go b/save.go new file mode 100644 index 0000000..d257649 --- /dev/null +++ b/save.go @@ -0,0 +1,162 @@ +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 +} diff --git a/saveRaw.go b/saveRaw.go deleted file mode 100644 index d257649..0000000 --- a/saveRaw.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 -} -- cgit v1.2.3