SemVer Support for Versions

This commit is contained in:
Justin Hammond 2021-09-30 17:11:11 +08:00
parent 6ba2d6e2ba
commit ec53e6fe78
6 changed files with 43 additions and 25 deletions

2
build.sh Executable file
View file

@ -0,0 +1,2 @@
#!/bin/sh
go build -ldflags="-X 'main.VersionSummary=`git-semver`'"

1
go.mod
View file

@ -7,6 +7,7 @@ require (
github.com/Fishwaldo/go-logadapter v0.0.2
github.com/Fishwaldo/go-taskmanager v0.0.0-20210918045401-1ed25dc708fa
github.com/adrianmo/go-nmea v1.4.0
github.com/blang/semver/v4 v4.0.0
github.com/godbus/dbus/v5 v5.0.4
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4
github.com/labstack/echo/v4 v4.6.0

2
go.sum
View file

@ -72,6 +72,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=

View file

@ -16,6 +16,7 @@ import (
"strings"
"errors"
"github.com/blang/semver/v4"
// "github.com/pkg/errors"
)
@ -105,19 +106,19 @@ func extractToFile(buf []byte, filename, target string) error {
}
// DownloadLatestStableRelease downloads the latest stable released version of
// restic and saves it to target. It returns the version string for the newest
// CarTracker and saves it to target. It returns the version string for the newest
// version. The function printf is used to print progress information.
func DownloadLatestStableRelease(ctx context.Context, target, currentVersion string) (version string, err error) {
func DownloadLatestStableRelease(ctx context.Context, target string, currentVersion semver.Version) (version semver.Version, err error) {
fmt.Printf("find latest release of restic at GitHub\n")
fmt.Printf("find latest release of CarTracker at GitHub\n")
rel, err := GitHubLatestRelease(ctx, "Fishwaldo", "CarTracker")
if err != nil {
return "", err
return semver.Version{}, err
}
if rel.Version == currentVersion {
fmt.Printf("restic is up to date\n")
if rel.Version.LTE(currentVersion) {
fmt.Printf("CarTracker is up to date\n")
return currentVersion, nil
}
@ -125,7 +126,7 @@ func DownloadLatestStableRelease(ctx context.Context, target, currentVersion str
_, sha256sums, err := getGithubDataFile(ctx, rel.Assets, "SHA256SUMS")
if err != nil {
return "", err
return currentVersion, err
}
// _, sig, err := getGithubDataFile(ctx, rel.Assets, "SHA256SUMS.asc", printf)
@ -152,30 +153,30 @@ func DownloadLatestStableRelease(ctx context.Context, target, currentVersion str
suffix := fmt.Sprintf("%s_%s.%s", runtime.GOOS, runtime.GOARCH, ext)
downloadFilename, buf, err := getGithubDataFile(ctx, rel.Assets, suffix)
if err != nil {
return "", err
return currentVersion, err
}
fmt.Printf("downloaded %v\n", downloadFilename)
wantHash, err := findHash(sha256sums, downloadFilename)
if err != nil {
return "", err
return currentVersion, err
}
gotHash := sha256.Sum256(buf)
if !bytes.Equal(wantHash, gotHash[:]) {
return "", fmt.Errorf("SHA256 hash mismatch, want hash %02x, got %02x", wantHash, gotHash)
return currentVersion, fmt.Errorf("SHA256 hash mismatch, want hash %02x, got %02x", wantHash, gotHash)
}
err = extractToFile(buf, downloadFilename, target)
if err != nil {
return "", err
return currentVersion, err
}
return rel.Version, nil
}
func DoUpdate(version string) (err error) {
func DoUpdate(version semver.Version) (err error) {
file, err := os.Executable()
if err != nil {
return errors.New("unable to find executable")
@ -200,11 +201,11 @@ func DoUpdate(version string) (err error) {
v, err := DownloadLatestStableRelease(context.Background(), file, version)
if err != nil {
return fmt.Errorf("unable to update restic: %v", err)
return fmt.Errorf("unable to update CarTracker: %v", err)
}
if v != version {
fmt.Printf("successfully updated restic to version %v\n", v)
if v.GT(version) {
fmt.Printf("successfully updated CarTracker to version %v\n", v)
}
return nil
}

View file

@ -11,6 +11,7 @@ import (
"errors"
// "github.com/pkg/errors"
"golang.org/x/net/context/ctxhttp"
"github.com/blang/semver/v4"
)
// Release collects data about a single release on GitHub.
@ -22,7 +23,7 @@ type Release struct {
PublishedAt time.Time `json:"published_at"`
Assets []Asset `json:"assets"`
Version string `json:"-"` // set manually in the code
Version semver.Version `json:"-"` // set manually in the code
}
// Asset is a file uploaded and attached to a release.
@ -106,7 +107,9 @@ func GitHubLatestRelease(ctx context.Context, owner, repo string) (Release, erro
return Release{}, fmt.Errorf("tag name %q is invalid, does not start with 'v'", release.TagName)
}
release.Version = release.TagName[1:]
if release.Version, err = semver.ParseTolerant(release.TagName[1:]); err != nil {
return Release{}, fmt.Errorf("can't Parse Release Version From Github: %s", release.TagName[1:])
}
return release, nil
}

25
main.go
View file

@ -19,14 +19,11 @@ import (
"github.com/Fishwaldo/CarTracker/internal/update"
"github.com/Fishwaldo/CarTracker/internal/web"
"github.com/Fishwaldo/go-logadapter/loggers/logrus"
"github.com/blang/semver/v4"
)
var (
Version = "0.0.0"
GitCommit = "none"
GitBranch = "unknown"
GitState = "unknown"
GitSummary = "unknown"
VersionSummary = "0.0.0"
)
func init() {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
@ -35,13 +32,25 @@ func init() {
}
func main() {
fmt.Printf("Starting CarTracker %s - %s/%s (%s) - %s\n", Version, GitBranch, GitCommit, GitState, GitSummary)
version, err := semver.ParseTolerant(VersionSummary)
if err != nil {
version, _ = semver.Make("0.0.0")
}
/* construct a version */
versionstring := version.FinalizeVersion()
if len(version.Pre) > 0 {
versionstring = fmt.Sprintf("%s-%s", versionstring, version.Pre[0].VersionStr)
if len(version.Build) > 0 {
versionstring = fmt.Sprintf("%s-%s", versionstring, version.Build[0])
}
}
fmt.Printf("Starting CarTracker Version %s\n", versionstring)
logger := logrus.LogrusDefaultLogger()
//dbus.DBUS.Start(logger)
err := update.DoUpdate(Version)
err = update.DoUpdate(version)
if err != nil {
fmt.Printf("Error: %s", err)
}