Compare commits
No commits in common. "master" and "v0.0.1" have entirely different histories.
26
changed.go
26
changed.go
|
@ -1,26 +0,0 @@
|
|||
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]
|
||||
}
|
|
@ -1,67 +0,0 @@
|
|||
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,10 +7,8 @@ import (
|
|||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.wit.com/log"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/encoding/prototext"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
@ -28,131 +26,34 @@ var ErrEmpty error = log.Errorf("file was empty")
|
|||
// - []byte : the contents of the file
|
||||
// - error on read
|
||||
func ConfigLoad(pb proto.Message, argname string, protoname string) error {
|
||||
var data []byte
|
||||
var fullname string
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Warn("ConfigLoad() UserHomeDir() err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
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 {
|
||||
log.Warn("config file failed to load", err)
|
||||
// set pb.Filename that was attempted
|
||||
// something went wrong loading the file
|
||||
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 = prototext.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)
|
||||
// set pb.Filename if it is there in the .proto file
|
||||
SetFilename(pb, fullname)
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
log.Infof("ConfigLoad() arg=%s, proto=%s\n", argname, protoname)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
98
save.go
98
save.go
|
@ -4,10 +4,8 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"go.wit.com/log"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/encoding/prototext"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
@ -15,25 +13,23 @@ import (
|
|||
var ErrProtoFilename error = log.Errorf("proto does not have Filename")
|
||||
|
||||
func ConfigSave(pb proto.Message) error {
|
||||
// log.Infof("ConfigSave() filename=%s %d\n", fullname, len(s))
|
||||
return saveTEXT(pb, "")
|
||||
}
|
||||
// get pb.Filename if it is there in the .proto file
|
||||
fullname, ok := GetFilename(pb)
|
||||
if !ok {
|
||||
return ErrProtoFilename
|
||||
}
|
||||
|
||||
func SavePB(pb proto.Message, fullname string) error {
|
||||
if !strings.HasSuffix(fullname, ".pb") {
|
||||
// todo: append .text here?
|
||||
return log.Errorf("%s needs to end in '.pb'", fullname)
|
||||
// Unmarshal()
|
||||
data, err := prototext.Marshal(pb)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
err = os.MkdirAll(dir, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -42,80 +38,8 @@ func SavePB(pb proto.Message, fullname string) error {
|
|||
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, 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)
|
||||
defer cfgfile.Close()
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue