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)
}