chore: move homepage override and icon cache to $BASE_DIR/data, add migration code

This commit is contained in:
yusing 2025-04-14 06:26:26 +08:00
parent d8eff90acc
commit fa16f4150a
4 changed files with 135 additions and 3 deletions

View file

@ -0,0 +1,26 @@
package migrations
import (
"errors"
"path/filepath"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/pkg"
)
var (
HomepageJSONConfigPathOld = filepath.Join(common.ConfigDir, ".homepage.json")
IconListCachePathOld = filepath.Join(common.ConfigDir, ".icon_list_cache.json")
IconCachePathOld = filepath.Join(common.ConfigDir, ".icon_cache.json")
)
func m001_move_json_data() error {
if version.IsOlderThan(pkg.Version{Major: 0, Minor: 11, Patch: 0}) {
return errors.Join(
mv(HomepageJSONConfigPathOld, common.HomepageJSONConfigPath),
mv(IconListCachePathOld, common.IconListCachePath),
mv(IconCachePathOld, common.IconCachePath),
)
}
return nil
}

21
migrations/migrate.go Normal file
View file

@ -0,0 +1,21 @@
package migrations
import (
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/pkg"
)
func RunMigrations() error {
errs := gperr.NewBuilder("migration error")
for _, migration := range migrations {
if err := migration(); err != nil {
errs.Add(err)
}
}
return errs.Error()
}
var version = pkg.GetVersion()
var migrations = []func() error{
m001_move_json_data,
}

16
migrations/utils.go Normal file
View file

@ -0,0 +1,16 @@
package migrations
import (
"os"
"path/filepath"
)
func mv(old, new string) error {
if _, err := os.Stat(old); os.IsNotExist(err) {
return nil
}
if err := os.MkdirAll(filepath.Dir(new), 0o755); err != nil {
return err
}
return os.Rename(old, new)
}

View file

@ -1,7 +1,76 @@
package pkg
var version = "unset"
import (
"fmt"
"strconv"
"strings"
)
func GetVersion() string {
return version
func GetVersion() Version {
return Version{Major: major, Minor: minor, Patch: patch}
}
func init() {
major, minor, patch = parseVersion(version)
}
type Version struct{ Major, Minor, Patch int }
func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
}
func (v Version) MarshalText() ([]byte, error) {
return []byte(v.String()), nil
}
func (v Version) IsNewerThan(other Version) bool {
if v.Major != other.Major {
return v.Major > other.Major
}
if v.Minor != other.Minor {
return v.Minor > other.Minor
}
return v.Patch > other.Patch
}
func (v Version) IsOlderThan(other Version) bool {
if v.Major != other.Major {
return v.Major < other.Major
}
if v.Minor != other.Minor {
return v.Minor < other.Minor
}
return v.Patch < other.Patch
}
func (v Version) IsEqual(other Version) bool {
return v.Major == other.Major && v.Minor == other.Minor && v.Patch == other.Patch
}
var (
version = "unset"
major, minor, patch int
)
func parseVersion(v string) (major, minor, patch int) {
v = strings.Split(v, "-")[0]
v = strings.TrimPrefix(v, "v")
parts := strings.Split(v, ".")
if len(parts) != 3 {
return
}
major, err := strconv.Atoi(parts[0])
if err != nil {
return
}
minor, err = strconv.Atoi(parts[1])
if err != nil {
return
}
patch, err = strconv.Atoi(parts[2])
if err != nil {
return
}
return
}