Skip to content

Commit

Permalink
feat: Add migration
Browse files Browse the repository at this point in the history
Signed-off-by: zu1k <i@zu1k.com>
  • Loading branch information
zu1k committed Jul 6, 2022
1 parent 2f1d164 commit 8e28030
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
54 changes: 54 additions & 0 deletions internal/migration/v4.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 8e28030

Please sign in to comment.