summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-22 09:20:36 -0500
committerJeff Carr <[email protected]>2025-10-22 09:20:36 -0500
commit26519e946e7e160f0d96e9af9e69090ff8e292f5 (patch)
tree28a210d0565155098eebe13ca8f63817dd8ca81a
parent367f681a490a48944c5d05ba0fe89d0099c0337d (diff)
lib/config is now using this
-rw-r--r--identify.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/identify.go b/identify.go
index 0de53a2..c27dd51 100644
--- a/identify.go
+++ b/identify.go
@@ -1,9 +1,19 @@
package filepb
import (
+ "errors"
+ "fmt"
"os"
+ "strings"
+
+ "google.golang.org/protobuf/encoding/protojson"
+ "google.golang.org/protobuf/encoding/prototext"
+ "google.golang.org/protobuf/proto"
)
+var ErrEmpty error = fmt.Errorf("config file was empty")
+var ErrMarshal error = fmt.Errorf("protobuf parse error")
+
// print the protobuf in human form
func IdentifyPB(filename string) (string, string, error) {
data, err := os.ReadFile(filename)
@@ -20,3 +30,86 @@ func IdentifyPB(filename string) (string, string, error) {
// log.Info("Identify protobuf file uuid =", pb.Uuid, "version =", pb.Version)
return pb.Uuid, pb.Version, nil
}
+
+func (pb *FakeFile) loadFromFilename(fullname string) error {
+ if strings.HasSuffix(fullname, ".text") {
+ return loadTEXT(pb, fullname)
+ }
+ if strings.HasSuffix(fullname, ".json") {
+ return loadJSON(pb, fullname)
+ }
+ if strings.HasSuffix(fullname, ".pb") {
+ return loadPB(pb, fullname)
+ }
+
+ return fmt.Errorf("unknown filetype '%s'", fullname)
+}
+
+func loadPB(pb proto.Message, fullname string) error {
+ data, err := loadFile(fullname)
+ if err != nil {
+ // set pb.Filename that was attempted
+ return err
+ }
+
+ if err = proto.Unmarshal(data, pb); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func loadTEXT(pb proto.Message, fullname string) error {
+ var data []byte
+ var err error
+ if data, err = loadFile(fullname); err != nil {
+ return err
+ }
+
+ // don't even bother with Marshal()
+ if data == nil {
+ return ErrEmpty // file is empty
+ }
+
+ // Unmarshal()
+ if err = prototext.Unmarshal(data, pb); err != nil {
+ return ErrMarshal
+ }
+ 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 {
+ return err
+ }
+
+ // don't even bother with Marshal()
+ if data == nil {
+ return ErrEmpty // file is empty
+ }
+
+ // Unmarshal()
+ if err = protojson.Unmarshal(data, pb); err != nil {
+ return ErrMarshal
+ }
+ return nil
+}
+
+func loadFile(fullname string) ([]byte, error) {
+ data, err := os.ReadFile(fullname)
+ if errors.Is(err, os.ErrNotExist) {
+ // if file does not exist, just return nil. this
+ return nil, err
+ }
+ if err != nil {
+ return nil, err
+ }
+ if len(data) == 0 {
+ return data, ErrEmpty
+ }
+ return data, nil
+}