From 8e280304bb8347c2e6f7b26a54696421c461ed01 Mon Sep 17 00:00:00 2001 From: zu1k Date: Wed, 6 Jul 2022 09:01:29 +0800 Subject: [PATCH] feat: Add migration Signed-off-by: zu1k --- go.mod | 2 +- go.sum | 2 ++ internal/migration/v4.go | 54 ++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 internal/migration/v4.go diff --git a/go.mod b/go.mod index b315bc6..933ec18 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/ip2location/ip2location-go/v9 v9.4.0 github.com/ipipdotnet/ipdb-go v1.3.1 github.com/lionsoul2014/ip2region v2.2.0-release+incompatible - github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220704023600-9376c56688c8 + github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220705082050-0f0c0cb4a5a6 github.com/oschwald/geoip2-golang v1.7.0 github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda github.com/spf13/cobra v1.5.0 diff --git a/go.sum b/go.sum index cf29a93..a0dc677 100644 --- a/go.sum +++ b/go.sum @@ -148,6 +148,8 @@ github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220624075035-53e6777ab github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220624075035-53e6777abd9d/go.mod h1:bChUKvbKVC3zL/lLLIcu6alhQaL8uWD/DA+jRdyggdI= github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220704023600-9376c56688c8 h1:aHOC8Mcbd3TiCb+L9cSbU/NdZinWuaKSoL4qD4I3MQo= github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220704023600-9376c56688c8/go.mod h1:bChUKvbKVC3zL/lLLIcu6alhQaL8uWD/DA+jRdyggdI= +github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220705082050-0f0c0cb4a5a6 h1:4lJlR7mskbInnkGSixv0l7Ew/SD+xQUqWKu0KwvkpiI= +github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220705082050-0f0c0cb4a5a6/go.mod h1:bChUKvbKVC3zL/lLLIcu6alhQaL8uWD/DA+jRdyggdI= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= diff --git a/internal/migration/v4.go b/internal/migration/v4.go new file mode 100644 index 0000000..5bfa9a5 --- /dev/null +++ b/internal/migration/v4.go @@ -0,0 +1,54 @@ +package migration + +import ( + "log" + + "github.com/spf13/viper" + "github.com/zu1k/nali/internal/constant" + "github.com/zu1k/nali/internal/db" + "github.com/zu1k/nali/pkg/cdn" + "github.com/zu1k/nali/pkg/ip2region" +) + +func init() { + viper.SetConfigName("config") + viper.SetConfigType("yaml") + viper.AddConfigPath(constant.WorkDirPath) + + err := viper.ReadInConfig() + if err != nil { + err = viper.SafeWriteConfig() + if err != nil { + panic(err) + } + } + + dbList := db.List{} + err = viper.UnmarshalKey("databases", &dbList) + if err != nil { + log.Fatalln("Config invalid:", err) + } + + needOverwrite := false + for _, adb := range dbList { + if adb.Name == "ip2region" && adb.File != "ip2region.xdb" { + needOverwrite = true + adb.File = "ip2region.xdb" + adb.DownloadUrls = ip2region.DownloadUrls + } + + if adb.Name == "cdn" && adb.Format != "cdn-yml" { + needOverwrite = true + adb.Format = "cdn-yml" + adb.DownloadUrls = cdn.DownloadUrls + } + } + + if needOverwrite { + viper.Set("databases", dbList) + err = viper.WriteConfig() + if err != nil { + log.Println(err) + } + } +} diff --git a/main.go b/main.go index e981dd3..6612d11 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "github.com/zu1k/nali/cmd" "github.com/zu1k/nali/internal/config" "github.com/zu1k/nali/internal/constant" + _ "github.com/zu1k/nali/internal/migration" ) func main() {