Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
|
77f7f39c25 | |
|
0e805acc67 | |
|
1dfac22abc | |
|
aea63ab19c | |
|
d158e4cb11 | |
|
68f0c43da6 | |
|
4e94160482 | |
|
92d7a71f12 | |
|
41fe3edc93 | |
|
fe4b47339a |
|
@ -0,0 +1,26 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// this package can provide a trivial way to track which
|
||||||
|
// protobufs have been modified and need to be written to disk
|
||||||
|
// todo: autogenpb could generate code to work with this
|
||||||
|
|
||||||
|
var saveMap map[string]bool
|
||||||
|
var saveLock sync.Mutex
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// init() should be avoided, but this package and for making
|
||||||
|
// this small string map, it seems a sensible exception
|
||||||
|
saveMap = make(map[string]bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetChanged(name string, b bool) {
|
||||||
|
saveLock.Lock()
|
||||||
|
defer saveLock.Unlock()
|
||||||
|
saveMap[name] = b
|
||||||
|
}
|
||||||
|
|
||||||
|
func HasChanged(name string) bool {
|
||||||
|
return saveMap[name]
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
// thank chatgpt for this because why. why write this if you can have it
|
||||||
|
// kick this out in 30 seconds
|
||||||
|
|
||||||
|
/*
|
||||||
|
func (f *Forge) backupConfig() error {
|
||||||
|
// make a new dir to backup the files
|
||||||
|
srcDir := filepath.Join(f.configDir)
|
||||||
|
destDir := filepath.Join(f.configDir, "backup")
|
||||||
|
return backupFiles(srcDir, destDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func backupFiles(srcDir string, destDir string) error {
|
||||||
|
// Create the destination directory
|
||||||
|
err := os.MkdirAll(destDir, os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Failed to create directory: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the contents of the source directory
|
||||||
|
entries, err := os.ReadDir(srcDir)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Failed to read directory: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate over the entries in the source directory
|
||||||
|
for _, entry := range entries {
|
||||||
|
// Skip directories and files that do not have the .test extension
|
||||||
|
if entry.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// log.Println("backing up file", entry.Name())
|
||||||
|
srcPath := filepath.Join(srcDir, entry.Name())
|
||||||
|
destPath := filepath.Join(destDir, entry.Name())
|
||||||
|
|
||||||
|
// Copy the file
|
||||||
|
if err := copyFile(srcPath, destPath); err != nil {
|
||||||
|
return errors.New(fmt.Sprintf("Failed to copy file %s: %v", entry.Name(), err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// copyFile copies a file from src to dest
|
||||||
|
func copyFile(src, dest string) error {
|
||||||
|
srcFile, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer srcFile.Close()
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work?
|
||||||
|
dest = dest + timestamp
|
||||||
|
destFile, err := os.Create(dest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer destFile.Close()
|
||||||
|
|
||||||
|
// Copy the content
|
||||||
|
_, err = io.Copy(destFile, srcFile)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*/
|
107
load.go
107
load.go
|
@ -7,8 +7,10 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
"google.golang.org/protobuf/encoding/prototext"
|
"google.golang.org/protobuf/encoding/prototext"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
@ -26,34 +28,131 @@ var ErrEmpty error = log.Errorf("file was empty")
|
||||||
// - []byte : the contents of the file
|
// - []byte : the contents of the file
|
||||||
// - error on read
|
// - error on read
|
||||||
func ConfigLoad(pb proto.Message, argname string, protoname string) error {
|
func ConfigLoad(pb proto.Message, argname string, protoname string) error {
|
||||||
var data []byte
|
|
||||||
var fullname string
|
var fullname string
|
||||||
homeDir, err := os.UserHomeDir()
|
homeDir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Warn("ConfigLoad() UserHomeDir() err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fullname = filepath.Join(homeDir, ".config", argname, protoname+".text")
|
fullname = filepath.Join(homeDir, ".config", argname, protoname+".text")
|
||||||
|
|
||||||
|
if err := loadTEXT(pb, fullname); err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasSuffix(fullname, ".text") {
|
||||||
|
fullname += ".json"
|
||||||
|
if err := loadJSON(pb, fullname); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Config file load failed:", fullname)
|
||||||
|
return log.Errorf("could not load config file")
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadPB(pb proto.Message, argname string, protoname string) (string, error) {
|
||||||
|
var fullname string
|
||||||
|
if strings.HasPrefix(argname, "/") {
|
||||||
|
fullname = filepath.Join(argname, protoname+".pb")
|
||||||
|
} else {
|
||||||
|
homeDir, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("ConfigLoad() UserHomeDir() err", err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
fullname = filepath.Join(homeDir, ".config", argname, protoname+".pb")
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := loadFile(fullname)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("LoadPB()", fullname, err)
|
||||||
|
// set pb.Filename that was attempted
|
||||||
|
return fullname, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal()
|
||||||
|
if err = proto.Unmarshal(data, pb); err != nil {
|
||||||
|
log.Warn("LoadPB() file", fullname)
|
||||||
|
log.Warn("LoadPB() Unmarshal() err", err)
|
||||||
|
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 {
|
if data, err = loadFile(fullname); err != nil {
|
||||||
log.Warn("config file failed to load", err)
|
log.Warn("config file failed to load", err)
|
||||||
// something went wrong loading the file
|
// set pb.Filename that was attempted
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't even bother with Marshal()
|
// don't even bother with Marshal()
|
||||||
if data == nil {
|
if data == nil {
|
||||||
|
log.Warn("ConfigLoad() config file was empty", fullname)
|
||||||
return ErrEmpty // file is empty
|
return ErrEmpty // file is empty
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal()
|
// Unmarshal()
|
||||||
if err = prototext.Unmarshal(data, pb); err != nil {
|
if err = prototext.Unmarshal(data, pb); err != nil {
|
||||||
|
log.Warn("ConfigLoad() file", fullname)
|
||||||
|
log.Warn("ConfigLoad() Unmarshal() err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// set pb.Filename if it is there in the .proto file
|
if fn, ok := GetFilename(pb); ok {
|
||||||
|
if fn != fullname {
|
||||||
|
log.Info("config.ConfigLoad() new filename:", fullname)
|
||||||
SetFilename(pb, fullname)
|
SetFilename(pb, fullname)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Infof("ConfigLoad() arg=%s, proto=%s\n", argname, protoname)
|
if os.Getenv("CONFIG_VERBOSE") == "true" {
|
||||||
|
log.Infof("ConfigLoad() %s len()=%d\n", fullname, len(data))
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// don't even bother with Marshal()
|
||||||
|
if data == nil {
|
||||||
|
log.Warn("ConfigLoad() config file was empty", fullname)
|
||||||
|
return ErrEmpty // file is empty
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal()
|
||||||
|
if err = protojson.Unmarshal(data, pb); err != nil {
|
||||||
|
log.Warn("ConfigLoad() file", fullname)
|
||||||
|
log.Warn("ConfigLoad() Unmarshal() err", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fn, ok := GetFilename(pb); ok {
|
||||||
|
if fn != fullname {
|
||||||
|
log.Info("config.ConfigLoad() new filename:", fullname)
|
||||||
|
SetFilename(pb, fullname)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Getenv("CONFIG_VERBOSE") == "true" {
|
||||||
|
log.Infof("ConfigLoad() %s len()=%d\n", fullname, len(data))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
98
save.go
98
save.go
|
@ -4,8 +4,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
"google.golang.org/protobuf/encoding/prototext"
|
"google.golang.org/protobuf/encoding/prototext"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
@ -13,23 +15,25 @@ import (
|
||||||
var ErrProtoFilename error = log.Errorf("proto does not have Filename")
|
var ErrProtoFilename error = log.Errorf("proto does not have Filename")
|
||||||
|
|
||||||
func ConfigSave(pb proto.Message) error {
|
func ConfigSave(pb proto.Message) error {
|
||||||
// get pb.Filename if it is there in the .proto file
|
// log.Infof("ConfigSave() filename=%s %d\n", fullname, len(s))
|
||||||
fullname, ok := GetFilename(pb)
|
return saveTEXT(pb, "")
|
||||||
if !ok {
|
}
|
||||||
return ErrProtoFilename
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unmarshal()
|
func SavePB(pb proto.Message, fullname string) error {
|
||||||
data, err := prototext.Marshal(pb)
|
if !strings.HasSuffix(fullname, ".pb") {
|
||||||
if err != nil {
|
// todo: append .text here?
|
||||||
return err
|
return log.Errorf("%s needs to end in '.pb'", fullname)
|
||||||
}
|
}
|
||||||
|
|
||||||
dir, name := filepath.Split(fullname)
|
dir, name := filepath.Split(fullname)
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return fmt.Errorf("filename was blank")
|
return fmt.Errorf("filename was blank")
|
||||||
}
|
}
|
||||||
err = os.MkdirAll(dir, os.ModePerm)
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := proto.Marshal(pb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -38,8 +42,80 @@ func ConfigSave(pb proto.Message) error {
|
||||||
return configWrite(fullname, data)
|
return configWrite(fullname, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func configWrite(fullname string, data []byte) error {
|
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
|
||||||
|
}
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
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(header+s))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 == "" {
|
||||||
|
return fmt.Errorf("filename was blank")
|
||||||
|
}
|
||||||
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
defer cfgfile.Close()
|
defer cfgfile.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue