diff --git a/go.mod b/go.mod index 03af161..aff6698 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/adrg/xdg v0.4.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/ipipdotnet/ipdb-go v1.3.3 github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220907060842-b2ba5d58e48d diff --git a/go.sum b/go.sum index cbb3d57..6d82b80 100644 --- a/go.sum +++ b/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.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 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/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= diff --git a/internal/migration/mirgration.go b/internal/migration/mirgration.go new file mode 100644 index 0000000..2d4b3c6 --- /dev/null +++ b/internal/migration/mirgration.go @@ -0,0 +1,6 @@ +package migration + +func init() { + migration2v4() + migration2v6() +} diff --git a/internal/migration/v4.go b/internal/migration/v4.go index f91d848..fd1fc82 100644 --- a/internal/migration/v4.go +++ b/internal/migration/v4.go @@ -10,7 +10,7 @@ import ( "github.com/zu1k/nali/pkg/ip2region" ) -func init() { +func migration2v4() { viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath(constant.ConfigDirPath) diff --git a/internal/migration/v6.go b/internal/migration/v6.go new file mode 100644 index 0000000..c785eef --- /dev/null +++ b/internal/migration/v6.go @@ -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) + } + } +} diff --git a/main.go b/main.go index ebf87cf..58d0664 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,11 @@ package main import ( + "github.com/zu1k/nali/internal/constant" + "github.com/zu1k/nali/cmd" "github.com/zu1k/nali/internal/config" - "github.com/zu1k/nali/internal/constant" + _ "github.com/zu1k/nali/internal/migration" )