1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 13:19:02 +08:00

fix: fix self-update bugs

1. added -v for update command, only update binary when -v is assigned;
2. removed the reference to semver library, implemented repo version compare functions;
3. edited release-aur-git rule in go.yml, won't replace the hyphen to dot in release version;
This commit is contained in:
Zhiyong(Johnny) Zhao 2023-09-28 16:09:40 -04:00
parent 43a308056f
commit f2bec94681
6 changed files with 91 additions and 17 deletions

View File

@ -38,7 +38,7 @@ jobs:
- name: Get version
id: version
run: echo ::set-output name=version::$(git describe --long --tags | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g')
run: echo ::set-output name=version::$(git describe --long --tags | sed 's/^v//;s/\([^-]*-g\)/r\1/;')
- name: Publish AUR package nali-go-git
uses: zu1k/aur-publish-action@master

View File

@ -12,15 +12,18 @@ import (
// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update [--db dbs]",
Short: "update qqwry, zxipv6wry, ip2region ip database and cdn",
Long: `update qqwry, zxipv6wry, ip2region ip database and cdn. Use commas to separate`,
Example: "nali update --db qqwry,cdn",
Use: "update [--db dbs -v]",
Short: "update qqwry, zxipv6wry, ip2region ip database and cdn, update nali to latest version if -v",
Long: `update qqwry, zxipv6wry, ip2region ip database and cdn. Use commas to separate. update nali to latest version if -v`,
Example: "nali update --db qqwry,cdn -v",
Run: func(cmd *cobra.Command, args []string) {
DBs, _ := cmd.Flags().GetString("db")
if err := repo.UpdateRepo(); err != nil {
log.Printf("update nali to latest version failed: %v \n", err)
version, _ := cmd.Flags().GetBool("v")
if version {
if err := repo.UpdateRepo(); err != nil {
log.Printf("update nali to latest version failed: %v \n", err)
}
}
var DBNameArray []string
@ -33,5 +36,6 @@ var updateCmd = &cobra.Command{
func init() {
updateCmd.PersistentFlags().String("db", "", "choose db you want to update")
updateCmd.PersistentFlags().Bool("v", false, "decide whether to update the nali version")
rootCmd.AddCommand(updateCmd)
}

1
go.mod
View File

@ -3,7 +3,6 @@ module github.com/zu1k/nali
go 1.19
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/adrg/xdg v0.4.0
github.com/fatih/color v1.15.0
github.com/google/go-github/v55 v55.0.0

2
go.sum
View File

@ -38,8 +38,6 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=

View File

@ -13,7 +13,6 @@ import (
"github.com/zu1k/nali/internal/constant"
"github.com/Masterminds/semver/v3"
"github.com/google/go-github/v55/github"
)
@ -153,13 +152,25 @@ func update(asset io.Reader, cmdPath string) error {
}
func canUpdate(rel *github.RepositoryRelease) bool {
if constant.Version != "unknown version" {
latest, _ := semver.NewVersion(rel.GetTagName())
cur, _ := semver.NewVersion(constant.Version)
return latest.GreaterThan(cur)
// unknown version means that the user compiled it manually instead of downloading it from the release,
// in which case we don't take the liberty of updating it to a potentially older version.
if constant.Version == "unknown version" {
return false
}
return false
latest, err := parseVersion(rel.GetTagName())
if err != nil {
log.Printf("failed to parse latest version: %v, err: %v \n", rel.GetTagName(), err)
return false
}
cur, err := parseVersion(constant.Version)
if err != nil {
log.Printf("failed to parse current version: %v, err: %v \n", constant.Version, err)
return false
}
return latest.GreaterThan(cur)
}
func canWriteDir(path string) bool {

62
internal/repo/version.go Normal file
View File

@ -0,0 +1,62 @@
package repo
import (
"fmt"
"strconv"
"strings"
)
type Version struct {
Major int
Minor int
Patch int
}
func parseVersion(vStr string) (*Version, error) {
vStr = strings.TrimPrefix(vStr, "v")
// split by hyphen to remove pre-release info, then split by dot to get version info
parts := strings.Split(strings.Split(vStr, "-")[0], ".")
if len(parts) < 3 {
return nil, fmt.Errorf("invalid version format: %s", vStr)
}
major, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
minor, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
patch, err := strconv.Atoi(parts[2])
if err != nil {
return nil, err
}
return &Version{Major: major, Minor: minor, Patch: patch}, nil
}
func (v *Version) Equal(other *Version) bool {
return v.compare(other) == 0
}
func (v *Version) GreaterThan(other *Version) bool {
return v.compare(other) > 0
}
func (v *Version) LessThan(other *Version) bool {
return v.compare(other) < 0
}
func (v *Version) compare(other *Version) int {
if v.Major != other.Major {
return v.Major - other.Major
}
if v.Minor != other.Minor {
return v.Minor - other.Minor
}
return v.Patch - other.Patch
}