summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backup.go1
-rw-r--r--changed.go2
-rw-r--r--humanFormat.go113
-rw-r--r--load.go70
-rw-r--r--save.go30
5 files changed, 27 insertions, 189 deletions
diff --git a/backup.go b/backup.go
index 8d149c9..de87880 100644
--- a/backup.go
+++ b/backup.go
@@ -31,7 +31,6 @@ func backupFiles(srcDir string, destDir string) error {
continue
}
- // log.Println("backing up file", entry.Name())
srcPath := filepath.Join(srcDir, entry.Name())
destPath := filepath.Join(destDir, entry.Name())
diff --git a/changed.go b/changed.go
index 32159cd..c5ede96 100644
--- a/changed.go
+++ b/changed.go
@@ -50,10 +50,8 @@ func IsDir(dirname string) bool {
// notsure if this is a thing anymore. don't care much either
func Path(filename string) string {
- // log.Log(INFO, "Path() START filename =", filename)
if runtime.GOOS == "windows" {
filename = strings.Replace(filename, "/", "\\", -1)
}
- // log.Log(INFO, "Path() END filename =", filename)
return filename
}
diff --git a/humanFormat.go b/humanFormat.go
deleted file mode 100644
index e501490..0000000
--- a/humanFormat.go
+++ /dev/null
@@ -1,113 +0,0 @@
-package config
-
-import (
- "fmt"
- "time"
-)
-
-/*
- etimef := func(e *forgepb.Set) string {
- etime := e.Etime.AsTime()
- s := etime.Format("2006/01/02 15:04")
- if strings.HasPrefix(s, "1970/") {
- // just show a blank if it's not set
- return ""
- }
- return s
- }
- t.AddStringFunc("etime", etimef)
-*/
-
-/*
- ctimef := func(p *forgepb.Set) string {
- ctime := p.Ctime.AsTime()
- return ctime.Format("2006/01/02 15:04")
- }
-}
-*/
-
-// This isn't for the marketing department
-// so this isn't going to use 'MiB' and 'GiB'
-func HumanFormatBytes(b int) string {
- if b < 2000 {
- return fmt.Sprintf("%d B", b)
- }
-
- kb := int(b / 1024)
- if kb < 2000 {
- return fmt.Sprintf("%d KB", kb)
- }
-
- mb := int(b / (1024 * 1024))
- if mb < 2000 {
- return fmt.Sprintf("%d MB", mb)
- }
-
- gb := int(b / (1024 * 1024 * 1024))
- if gb < 2000 {
- return fmt.Sprintf("%d GB", gb)
- }
-
- tb := int(b / (1024 * 1024 * 1024 * 1024))
- return fmt.Sprintf("%d TB", tb)
-}
-
-func FormatDuration(d time.Duration) string {
- result := ""
-
- // check if it's more than a year
- years := int(d.Hours()) / (24 * 365)
- if years > 0 {
- result += fmt.Sprintf("%dy", years)
- return result
- }
-
- // check if it's more than a day
- days := int(d.Hours()) / 24
- if days > 0 {
- result += fmt.Sprintf("%dd", days)
- return result
- }
-
- // check if it's more than an hour
- hours := int(d.Hours()) % 24
- if hours > 0 {
- result += fmt.Sprintf("%dh", hours)
- return result
- }
-
- // check if it's more than a minute
- minutes := int(d.Minutes()) % 60
- if minutes > 0 {
- result += fmt.Sprintf("%dm", minutes)
- return result
- }
-
- // check if it's more than a second
- seconds := int(d.Seconds()) % 60
- if seconds > 0 {
- result += fmt.Sprintf("%ds", seconds)
- return result
- }
-
- // report in milliseconds
- ms := int(d.Milliseconds())
- if ms > 100 {
- // todo: print .3s, etc ?
- }
- if ms > 0 {
- result += fmt.Sprintf("%dms", ms)
- return result
- }
-
- // report in milliseconds
- mc := int(d.Microseconds())
- if mc > 0 {
- result += fmt.Sprintf("%dmc", mc)
- return result
- }
-
- ns := int(d.Nanoseconds())
- result += fmt.Sprintf("%dns", ns)
- return result
-}
diff --git a/load.go b/load.go
index 160bd0e..bf1182e 100644
--- a/load.go
+++ b/load.go
@@ -5,12 +5,12 @@ package config
import (
"errors"
+ "fmt"
"os"
"path/filepath"
"strings"
"go.wit.com/lib/protobuf/filepb"
- "go.wit.com/log"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
@@ -22,8 +22,8 @@ func Load(argname string) ([]byte, string) {
}
*/
-var ErrEmpty error = log.Errorf("config file was empty")
-var ErrMarshal error = log.Errorf("protobuf parse error")
+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/<argname>
@@ -34,7 +34,6 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
var err error
homeDir, err := os.UserHomeDir()
if err != nil {
- log.Printf("ConfigLoad() UserHomeDir() err=%v\n", err)
return err
}
@@ -50,7 +49,6 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
if errors.Is(err, ErrEmpty) {
return ErrEmpty
}
- log.Info("ConfigLoad() error", fullname, err)
return err
}
@@ -61,20 +59,26 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
fulljson := fullname + ".json"
if err := loadJSON(pb, fulljson); err == nil {
return nil
- } else {
- log.Info("Config file load failed:", fulljson, err)
}
}
}
-
- log.Info("Config file load failed:", fullname, err)
return ErrMarshal
}
+// loads from the users .cache dir
+// todo: err handling
+func LoadCache(pb proto.Message, argname string, protoname string) error {
+ cacheDir, _ := os.UserCacheDir()
+ fullpath := filepath.Join(cacheDir, ".config", argname)
+ os.MkdirAll(fullpath, os.ModePerm)
+ fullname := filepath.Join(fullpath, protoname+".pb")
+ SetFilename(pb, fullname)
+ return Load(pb)
+}
+
func Load(pb proto.Message) error {
fullname, err := GetFilename(pb)
if err != nil {
- log.Info("'Filename' is not in: =", fullname, err)
return err
}
// text is supposed to be "easy". Don't verify 'version'
@@ -87,8 +91,7 @@ func Load(pb proto.Message) error {
var worked bool
ver, err := GetString(pb, "version")
if err != nil {
- log.Info("'Version' is not in: =", fullname, err)
- return err
+ return errors.Join(err, errors.New("'Version' is not in: "+fullname))
}
// maybe don't really verify .json files (?)
// doing it for now anyway. maybe just return an error
@@ -104,21 +107,21 @@ func Load(pb proto.Message) error {
if pberr != nil {
return pberr
}
- log.Info("your version :", ver)
- log.Info("pb version is:", pbver)
- log.Info("pb uuid is:", pbuuid)
+ err = errors.Join(err, errors.New("your version:"+ver))
+ err = errors.Join(err, errors.New("pb version is:"+pbver))
+ err = errors.Join(err, errors.New("pb uuid is:"+pbuuid))
return err
}
worked = true
}
if !worked {
- return log.Errorf("unknown filetype %s", fullname)
+ return fmt.Errorf("unknown filetype %s", fullname)
}
newver, _ := GetString(pb, "version")
if ver != newver {
- log.Printf("VERSION '%s' != '%s'\n", ver, newver)
- log.Info("Your protobuf file is old and can not be loaded")
- log.Info("You must delete or convert the file", fullname)
+ fmt.Printf("VERSION '%s' != '%s'\n", ver, newver)
+ fmt.Println("Your protobuf file is old and can not be loaded")
+ fmt.Println("You must delete or convert the file", fullname)
// probably should ALWAYS PANIC HERE
panic("protobuf version mismatch")
}
@@ -136,19 +139,17 @@ func LoadFile(pb proto.Message, fullname string) error {
return loadPB(pb, fullname)
}
- return log.Errorf("unknown filetype %s", fullname)
+ return fmt.Errorf("unknown filetype %s", fullname)
}
func loadPB(pb proto.Message, fullname string) error {
data, err := loadFile(fullname)
if err != nil {
- log.Warn("LoadPB()", fullname, err)
// set pb.Filename that was attempted
return err
}
if err = proto.Unmarshal(data, pb); err != nil {
- log.Warn("LoadPB() error Unmarshal() ", fullname, err)
return err
}
@@ -162,7 +163,6 @@ func LoadPB(pb proto.Message, argname string, protoname string) (string, error)
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
- log.Warn("ConfigLoad() UserHomeDir() err", err)
return "", err
}
@@ -171,15 +171,11 @@ func LoadPB(pb proto.Message, argname string, protoname string) (string, error)
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
}
@@ -191,34 +187,24 @@ func loadTEXT(pb proto.Message, fullname string) error {
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
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 ErrMarshal
}
if fn, err := GetFilename(pb); err != nil {
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
}
@@ -228,33 +214,24 @@ 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 ErrMarshal
}
if fn, err := GetFilename(pb); err != nil {
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
}
@@ -285,7 +262,6 @@ func loadFile(fullname string) ([]byte, error) {
return nil, err
}
if err != nil {
- // log.Info("open config file :", err)
return nil, err
}
if len(data) == 0 {
diff --git a/save.go b/save.go
index 72d383e..37a457e 100644
--- a/save.go
+++ b/save.go
@@ -6,16 +6,14 @@ import (
"path/filepath"
"strings"
- "go.wit.com/log"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)
-var ErrProtoFilename error = log.Errorf("proto does not have Filename")
+var ErrProtoFilename error = fmt.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, "")
}
@@ -43,7 +41,7 @@ func SavePB(pb proto.Message, fullname string) error {
func saveProto(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)
+ return fmt.Errorf("%s needs to end in '.pb'", fullname)
}
dir, name := filepath.Split(fullname)
@@ -59,7 +57,6 @@ func saveProto(pb proto.Message, fullname string) error {
return err
}
- log.Infof("pb.Save() %s (%s)\n", fullname, HumanFormatBytes(len(data)))
return configWrite(fullname, data)
}
@@ -92,7 +89,7 @@ func saveTEXT(pb proto.Message, header string) error {
}
if !strings.HasSuffix(fullname, ".text") {
// todo: append .text here?
- return log.Errorf("not .text file: %s", fullname)
+ return fmt.Errorf("not .text file: %s", fullname)
}
dir, name := filepath.Split(fullname)
@@ -109,7 +106,6 @@ func saveTEXT(pb proto.Message, header string) error {
if err != nil {
return err
}
- log.Infof("pb.Save() %s (%s)\n", fullname, HumanFormatBytes(len(data)))
return nil
}
@@ -121,7 +117,7 @@ func saveJSON(pb proto.Message) error {
}
if !strings.HasSuffix(fullname, ".text") {
// todo: append .text here?
- return log.Errorf("not .text file: %s", fullname)
+ return fmt.Errorf("not .text file: %s", fullname)
}
dir, name := filepath.Split(fullname)
@@ -140,7 +136,6 @@ func saveJSON(pb proto.Message) error {
if err != nil {
return err
}
- log.Infof("pb.Save() %s (%s)\n", fullname, HumanFormatBytes(len(data)))
return nil
}
@@ -148,25 +143,8 @@ 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 {
- log.Warn("open config file :", err)
return err
}
_, err = cfgfile.Write(data)
return err
}
-
-/*
-func (e *Events) Save() {
- var fullname string
- base, _ := filepath.Split(argv.Config)
- fullname = filepath.Join(base, "events.pb")
-
- data, err := e.Marshal()
- if err != nil {
- log.Info("proto.Marshal() failed", err)
- return
- }
- log.Info("proto.Marshal() worked len", len(data))
- configWrite(fullname, data)
-}
-*/