1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 21:29:02 +08:00
nali/internal/repo/version.go
Zhiyong(Johnny) Zhao f2bec94681 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;
2023-09-28 16:31:29 -04:00

63 lines
1.2 KiB
Go

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
}