mirror of
https://github.com/zu1k/nali.git
synced 2025-01-22 13:19:02 +08:00
feat: auto migrate to new dir
Signed-off-by: zu1k <i@zu1k.com>
This commit is contained in:
parent
0062ad914e
commit
462503377c
1
go.mod
1
go.mod
@ -5,6 +5,7 @@ go 1.18
|
|||||||
require (
|
require (
|
||||||
github.com/adrg/xdg v0.4.0
|
github.com/adrg/xdg v0.4.0
|
||||||
github.com/fatih/color v1.13.0
|
github.com/fatih/color v1.13.0
|
||||||
|
github.com/google/martian v2.1.0+incompatible
|
||||||
github.com/ip2location/ip2location-go/v9 v9.4.1
|
github.com/ip2location/ip2location-go/v9 v9.4.1
|
||||||
github.com/ipipdotnet/ipdb-go v1.3.3
|
github.com/ipipdotnet/ipdb-go v1.3.3
|
||||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220907060842-b2ba5d58e48d
|
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220907060842-b2ba5d58e48d
|
||||||
|
1
go.sum
1
go.sum
@ -103,6 +103,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
|||||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||||
|
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
|
||||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||||
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||||
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||||
|
6
internal/migration/mirgration.go
Normal file
6
internal/migration/mirgration.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package migration
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
migration2v4()
|
||||||
|
migration2v6()
|
||||||
|
}
|
@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/zu1k/nali/pkg/ip2region"
|
"github.com/zu1k/nali/pkg/ip2region"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func migration2v4() {
|
||||||
viper.SetConfigName("config")
|
viper.SetConfigName("config")
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("yaml")
|
||||||
viper.AddConfigPath(constant.ConfigDirPath)
|
viper.AddConfigPath(constant.ConfigDirPath)
|
||||||
|
37
internal/migration/v6.go
Normal file
37
internal/migration/v6.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package migration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/google/martian/log"
|
||||||
|
"github.com/zu1k/nali/internal/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
const oldDefaultWorkPath = "~/.nali/"
|
||||||
|
|
||||||
|
func migration2v6() {
|
||||||
|
_, err := os.Stat(oldDefaultWorkPath)
|
||||||
|
if err == nil {
|
||||||
|
println("Old data directories are detected and will attempt to migrate automatically")
|
||||||
|
oldDefaultConfigPath := filepath.Join(oldDefaultWorkPath, "config.yaml")
|
||||||
|
stat, err := os.Stat(oldDefaultConfigPath)
|
||||||
|
if err == nil {
|
||||||
|
if stat.Mode().IsRegular() {
|
||||||
|
_ = os.Rename(oldDefaultConfigPath, filepath.Join(constant.ConfigDirPath, "config.yaml"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
files, err := os.ReadDir(oldDefaultWorkPath)
|
||||||
|
if err == nil {
|
||||||
|
for _, file := range files {
|
||||||
|
if file.Type().IsRegular() {
|
||||||
|
_ = os.Rename(filepath.Join(oldDefaultWorkPath, file.Name()), filepath.Join(constant.ConfigDirPath, file.Name()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = os.RemoveAll(oldDefaultWorkPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Auto migration failed: %s\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
4
main.go
4
main.go
@ -1,9 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/zu1k/nali/internal/constant"
|
||||||
|
|
||||||
"github.com/zu1k/nali/cmd"
|
"github.com/zu1k/nali/cmd"
|
||||||
"github.com/zu1k/nali/internal/config"
|
"github.com/zu1k/nali/internal/config"
|
||||||
"github.com/zu1k/nali/internal/constant"
|
|
||||||
_ "github.com/zu1k/nali/internal/migration"
|
_ "github.com/zu1k/nali/internal/migration"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user