-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
172 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package version | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ErrAheadLatest = fmt.Errorf("local version is ahead of the latest release") | ||
ErrBehindLatest = fmt.Errorf("local version is out of date.") | ||
) | ||
|
||
func UpgradeDetect() error { | ||
localVersion, err := ParseVersion(Version) | ||
if err != nil { | ||
return fmt.Errorf("Failed to parse local version: %s err: %v", Version, err) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.TODO()) | ||
defer cancel() | ||
releaseTagName, downloadUrl, err := ReleaseVersion(ctx) | ||
if err != nil { | ||
return fmt.Errorf("Failed to get latest release: %v", err) | ||
} | ||
remoteVersion, err := ParseVersion(strings.TrimPrefix(releaseTagName, "v")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
compare := CompareVersions(localVersion, remoteVersion) | ||
switch { | ||
case compare < 0: | ||
return fmt.Errorf("%w Current verion: %q.\n 🎉 New verion: %q from %q", ErrBehindLatest, Version, releaseTagName, downloadUrl) | ||
case compare > 0: | ||
return ErrAheadLatest | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
// SemanticVersion represents a semantic version in the format "major.minor.patch". | ||
type SemanticVersion struct { | ||
Major int | ||
Minor int | ||
Patch int | ||
} | ||
|
||
// ParseVersion parses a version string in the format "major.minor.patch" into a SemVer struct. | ||
func ParseVersion(versionStr string) (SemanticVersion, error) { | ||
parts := strings.Split(versionStr, ".") | ||
if len(parts) != 3 { | ||
return SemanticVersion{}, fmt.Errorf("invalid version format") | ||
} | ||
|
||
major, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return SemanticVersion{}, fmt.Errorf("invalid verision.major format") | ||
} | ||
minor, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return SemanticVersion{}, fmt.Errorf("invalid version.minor format") | ||
} | ||
patch, err := strconv.Atoi(parts[2]) | ||
if err != nil { | ||
return SemanticVersion{}, fmt.Errorf("invalid version.patch format") | ||
} | ||
|
||
return SemanticVersion{Major: major, Minor: minor, Patch: patch}, nil | ||
} | ||
|
||
// CompareVersions compares two semantic versions. | ||
func CompareVersions(v1, v2 SemanticVersion) int { | ||
if v1.Major != v2.Major { | ||
return v1.Major - v2.Major | ||
} | ||
if v1.Minor != v2.Minor { | ||
return v1.Minor - v2.Minor | ||
} | ||
return v1.Patch - v2.Patch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package version | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"kyanos/common" | ||
"strings" | ||
"time" | ||
|
||
"github.com/google/go-github/v68/github" | ||
) | ||
|
||
var ( | ||
// String -X "kyanos/version.Version={{.Version}}" | ||
Version string | ||
// BuildTime -X "kyanos/version.CommitID={{.Commit}}" | ||
BuildTime string | ||
// CommitID -X "kyanos/version.BuildTime={{.Date}}" | ||
CommitID string | ||
) | ||
|
||
const unknown = "<unknown>" | ||
|
||
func GetVersion() string { | ||
if Version != "" { | ||
return Version | ||
} | ||
return unknown | ||
} | ||
|
||
func GetBuildTime() string { | ||
if BuildTime != "" { | ||
return BuildTime | ||
} | ||
return unknown | ||
} | ||
|
||
func GetCommitID() string { | ||
if CommitID != "" { | ||
return CommitID | ||
} | ||
return unknown | ||
} | ||
|
||
func ReleaseVersion(ctx context.Context) (string, string, error) { | ||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) | ||
defer cancel() | ||
|
||
client := github.NewClient(nil) | ||
releases, _, err := client.Repositories.GetLatestRelease(ctx, "hengyoush", "kyanos") | ||
if err != nil { | ||
return "", "", err | ||
} | ||
arch, err := common.UnameMachine() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
kyanosAsset := fmt.Sprintf("kyanos_%s_linux_%s.tar.gz", strings.TrimPrefix(*releases.TagName, "v"), arch) | ||
for _, asset := range releases.Assets { | ||
if *asset.Name == kyanosAsset { | ||
return *releases.TagName, *asset.BrowserDownloadURL, nil | ||
} | ||
} | ||
return *releases.TagName, "", fmt.Errorf("no asset found for %s in github release", kyanosAsset) | ||
} |