package config // functions to import and export the protobuf // data to and from config files import ( "errors" "fmt" "os" "path/filepath" "strings" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/proto" ) /* // loads a file from ~/.config// func Load(argname string) ([]byte, string) { } */ var ErrEmpty error = fmt.Errorf("config file was empty") var ErrMarshal error = fmt.Errorf("protobuf parse error") // returns: // - Full path to the config file. usually: ~/.config/ // - []byte : the contents of the file // - error on read func ConfigLoad(pb proto.Message, argname string, protoname string) error { var fullname string var err error configDir, err := os.UserConfigDir() if err != nil { return err } fullname = filepath.Join(configDir, argname, protoname+".text") SetFilename(pb, fullname) // if both don't exist or both are empty, return known errors // these can be used to detect if the user is new to the application if err := missingConfig(fullname); err != nil { if errors.Is(err, os.ErrNotExist) { return os.ErrNotExist } if errors.Is(err, ErrEmpty) { return ErrEmpty } return err } if err = loadTEXT(pb, fullname); err == nil { return nil } else { if strings.HasSuffix(fullname, ".text") { fulljson := fullname + ".json" if err := loadJSON(pb, fulljson); err == nil { return nil } } } return ErrMarshal } // loads from the users .cache dir // if the .proto file version changes, automatically delete the .pb // file. This is important to avoid marshalling garbage data // .cache files are treated as such, a "cache" file. don't keep important // things in here. argv stores the information here for autodelete func LoadCache(pb proto.Message, argname string, protoname string) error { cacheDir, _ := os.UserCacheDir() fullpath := filepath.Join(cacheDir, argname) os.MkdirAll(fullpath, os.ModePerm) fullname := filepath.Join(fullpath, protoname+".pb") _, err := SetFilename(pb, fullname) if err != nil { pb = nil os.Remove(fullname) return err } newver, curver, err := LoadVersionCheckPB(pb) if err != nil { pb = nil os.Remove(fullname) return err } _, _ = newver, curver return nil } func LoadVersionCheckPB(pb proto.Message) (string, string, error) { var newver string var pbver string var err error fullname, err := GetFilename(pb) if err != nil { return newver, pbver, err } // text is supposed to be "easy". Don't verify 'version' if strings.HasSuffix(fullname, ".text") { err = loadTEXT(pb, fullname) return newver, pbver, err } // verify 'version' for .pb files // application will panic if they don't match var worked bool newver, err = GetString(pb, "version") if err != nil { return newver, pbver, err } // maybe don't really verify .json files (?) // doing it for now anyway. maybe just return an error if strings.HasSuffix(fullname, ".json") { if err = loadJSON(pb, fullname); err != nil { return newver, pbver, err } worked = true } if strings.HasSuffix(fullname, ".pb") { if err = loadPB(pb, fullname); err != nil { return newver, pbver, err } _, err = SetFilename(pb, fullname) if err != nil { return newver, pbver, err } worked = true } if !worked { return newver, pbver, fmt.Errorf("unknown filetype '%s'", fullname) } // get the version from the current PB saved on disk pbver, _ = GetString(pb, "version") if newver != pbver { return newver, pbver, VersionMismatch } return newver, pbver, nil } // uses the version to panic. This is needed because loading binary // protobuf files with rearranged messages is indeterminate func LoadPB(pb proto.Message) error { fullname, err := GetFilename(pb) if err != nil { return err } newver, pbver, err := LoadVersionCheckPB(pb) if newver != pbver { fmt.Println("") fmt.Printf("VERSION new '%s' != cur PB '%s'\n", newver, pbver) fmt.Println("") fmt.Println("Your protobuf file is old and can not be loaded") fmt.Println("your application must decide how to handle this (delete or fix)") fmt.Println("always panic here. application is broken") fmt.Println("You must delete or convert the file", fullname) fmt.Println("") // probably should ALWAYS PANIC HERE // upon further study, always panic here is better than not s := fmt.Sprintf("protobuf version wrong. delete or fix %s", fullname) panic(s) } return err } func LoadFromFilename(pb proto.Message, fullname string) error { return LoadFile(pb, fullname) } func LoadFile(pb proto.Message, 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 LoadConfigPB(pb proto.Message, argname string, protoname string) (string, error) { var fullname string if strings.HasPrefix(argname, "/") { fullname = filepath.Join(argname, protoname+".pb") } else { configDir, err := os.UserConfigDir() if err != nil { return "", err } fullname = filepath.Join(configDir, argname, protoname+".pb") } data, err := loadFile(fullname) if err != nil { return fullname, err } // Unmarshal() if err = proto.Unmarshal(data, pb); err != nil { return fullname, err } return fullname, nil } func loadTEXT(pb proto.Message, fullname string) error { var data []byte var err error SetFilename(pb, fullname) 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 } if fn, err := GetFilename(pb); err != nil { if fn != fullname { SetFilename(pb, fullname) } } 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 } if fn, err := GetFilename(pb); err != nil { if fn != fullname { SetFilename(pb, fullname) } } return nil } // dumb but simple to read logic func missingConfig(fullname string) error { data1, err1 := os.ReadFile(fullname) if !errors.Is(err1, os.ErrNotExist) { return err1 } data2, err2 := os.ReadFile(fullname + ".json") if !errors.Is(err2, os.ErrNotExist) { return err2 } if errors.Is(err1, os.ErrNotExist) && errors.Is(err2, os.ErrNotExist) { return os.ErrNotExist } if (len(data1) == 0) && (len(data2) == 0) { return ErrEmpty } 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 }