mirror of
https://github.com/mjl-/mox.git
synced 2025-07-14 18:14:38 +03:00
move config-changing code from package mox-/ to admin/
needed for upcoming changes, where (now) package admin needs to import package store. before, because package store imports mox- (for accessing the active config), that would lead to a cyclic import. package mox- keeps its active config, package admin has the higher-level config-changing functions.
This commit is contained in:
52
store/lastknown.go
Normal file
52
store/lastknown.go
Normal file
@ -0,0 +1,52 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mjl-/mox/mox-"
|
||||
"github.com/mjl-/mox/moxvar"
|
||||
"github.com/mjl-/mox/updates"
|
||||
)
|
||||
|
||||
// StoreLastKnown stores the the last known version. Future update checks compare
|
||||
// against it, or the currently running version, whichever is newer.
|
||||
func StoreLastKnown(v updates.Version) error {
|
||||
return os.WriteFile(mox.DataDirPath("lastknownversion"), []byte(v.String()), 0660)
|
||||
}
|
||||
|
||||
// LastKnown returns the last known version that has been mentioned in an update
|
||||
// email, or the current application.
|
||||
func LastKnown() (current, lastknown updates.Version, mtime time.Time, rerr error) {
|
||||
curv, curerr := updates.ParseVersion(moxvar.VersionBare)
|
||||
|
||||
p := mox.DataDirPath("lastknownversion")
|
||||
fi, _ := os.Stat(p)
|
||||
if fi != nil {
|
||||
mtime = fi.ModTime()
|
||||
}
|
||||
|
||||
vbuf, err := os.ReadFile(p)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return curv, updates.Version{}, mtime, err
|
||||
}
|
||||
|
||||
lastknown, lasterr := updates.ParseVersion(strings.TrimSpace(string(vbuf)))
|
||||
|
||||
if curerr == nil && lasterr == nil {
|
||||
if curv.After(lastknown) {
|
||||
return curv, curv, mtime, nil
|
||||
}
|
||||
return curv, lastknown, mtime, nil
|
||||
} else if curerr == nil {
|
||||
return curv, curv, mtime, nil
|
||||
} else if lasterr == nil {
|
||||
return curv, lastknown, mtime, nil
|
||||
}
|
||||
if strings.HasPrefix(moxvar.Version, "(devel)") {
|
||||
return curv, updates.Version{}, mtime, fmt.Errorf("development version")
|
||||
}
|
||||
return curv, updates.Version{}, mtime, fmt.Errorf("parsing version: %w", err)
|
||||
}
|
Reference in New Issue
Block a user