func (r byVersion) Less(i, j int) bool { first, err := semver.New(determineVersionFromTag(*r[i].TagName)) if err != nil { return true } second, err := semver.New(determineVersionFromTag(*r[j].TagName)) if err != nil { return false } return first.LT(*second) }
// bumpedVersion returns new bumped-up version according to given spec. func (conf Config) bumpedVersion(version string) (string, error) { if conf.Exact != "" { exact, err := semver.New(conf.Exact) if err != nil { return "", err } return exact.String(), nil } v, err := semver.Parse(version) if err != nil { return "", err } if conf.MajorDelta > 0 { v.Major = v.Major + conf.MajorDelta v.Minor = 0 v.Patch = 0 } else if conf.MinorDelta > 0 { v.Minor = v.Minor + conf.MinorDelta v.Patch = 0 } else if conf.PatchDelta > 0 { v.Patch = v.Patch + conf.PatchDelta } return v.String(), nil }
func (cfg *Config) isNewerVersion(newer string) bool { nv, err := semver.New(newer) if err != nil { log.Errorf("Bad version string on update: %v", err) return false } return nv.GT(cfg.version) }
func (g *GpgCLI) VersionAtLeast(s string) (bool, error) { min, err := semver.New(s) if err != nil { return false, err } cur, err := g.SemanticVersion() if err != nil { return false, err } return cur.GTE(*min), nil }
func (g *GpgCLI) SemanticVersion() (*semver.Version, error) { out, err := g.Version() if err != nil { return nil, err } lines := strings.Split(out, "\n") if len(lines) == 0 { return nil, errors.New("empty gpg version") } parts := strings.Fields(lines[0]) if len(parts) < 3 { return nil, fmt.Errorf("unhandled gpg version output %q full: %q", lines[0], lines) } return semver.New(parts[2]) }
func LatestAvailableVersion() semver.Version { resp, err := http.Get(GithubUrl + appReleasesUrl) if err != nil || resp.StatusCode != 200 { log.Fatal("There was an error updating imgix") } defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) release := &Release{} json.Unmarshal([]byte(body), &release) latest, _ := semver.New(strings.Replace(release.TagName, "v", "", -1)) return *latest }
// Apply applies the next available update whenever it is available, blocking // until the next update has been applied. If ApplyNext returns without an // error, that means that the current program's executable has been udpated in // place and you may want to restart. If ApplyNext returns an error, that means // that an unrecoverable error has occurred and we can't continue checking for // updates. func ApplyNext(cfg *Config) error { // Parse the semantic version var err error cfg.version, err = semver.New(cfg.CurrentVersion) if err != nil { return fmt.Errorf("Bad version string: %v", err) } if cfg.CheckInterval == 0 { cfg.CheckInterval = defaultCheckInterval log.Debugf("Defaulted CheckInterval to %v", cfg.CheckInterval) } if cfg.HTTPClient == nil { cfg.HTTPClient = defaultHTTPClient log.Debug("Defaulted HTTPClient") } update.HTTPClient = cfg.HTTPClient return cfg.loop() }
// Check latest version of git-hooks by github release // If there are new version of git-hooks, download and replace the current one func update(releases []github.RepositoryRelease) { release := releases[0] version := *release.TagName logger.Infoln("Current version is " + VERSION + ", latest version is " + version) current, err := semver.New(VERSION[1:]) if err != nil { logger.Errorln("Semver parse error ", err) return } latest, err := semver.New(version[1:]) if err != nil { logger.Errorln("Semver parse error ", err) return } // latest version if current.GTE(latest) { logger.Infoln(MESSAGES["UpdateToDate"]) return } // backward incompatible if latest.Major != current.Major { logger.Infoln(MESSAGES["Incompatible"]) return } // previous version target := fmt.Sprintf("git-hooks_%s_%s.tar.gz", runtime.GOOS, runtime.GOARCH) for _, asset := range release.Assets { if *asset.Name != target { continue } // download logger.Infoln("Downloading latest version...") tmpFileName, err := downloadFromUrl(*asset.BrowserDownloadURL) if err != nil { logger.Errorln("Fail to download", err) return } logger.Infoln("Download complete") // extract tmpFileName, err = extract(tmpFileName) if err != nil { logger.Errorln("Fail to extract tar.gz", err) return } logger.Infoln("Extract complete") // install binary in non-test environment if !isTestEnv() { err = installBinary(tmpFileName) if err != nil { logger.Errorln("Fail to install binary", err) return } } logger.Infoln("Successfully update " + NAME + " to " + version) break } }
func (c *CheckCommand) Run(request CheckRequest) ([]Version, error) { releases, err := c.github.ListReleases() if err != nil { return []Version{}, err } if len(releases) == 0 { return []Version{}, nil } var filteredReleases []github.RepositoryRelease for _, release := range releases { if request.Source.Drafts != *release.Draft { continue } if release.TagName == nil { continue } if _, err := semver.New(determineVersionFromTag(*release.TagName)); err != nil { continue } filteredReleases = append(filteredReleases, release) } sort.Sort(byVersion(filteredReleases)) if len(filteredReleases) == 0 { return []Version{}, nil } latestRelease := filteredReleases[len(filteredReleases)-1] if (request.Version == Version{}) { return []Version{ versionFromDraft(&latestRelease), }, nil } if *latestRelease.TagName == request.Version.Tag { return []Version{}, nil } upToLatest := false reversedVersions := []Version{} for _, release := range filteredReleases { if upToLatest { reversedVersions = append(reversedVersions, versionFromDraft(&release)) } else { if *release.Draft { id := *release.ID upToLatest = request.Version.ID == strconv.Itoa(id) } else { version := *release.TagName upToLatest = request.Version.Tag == version } } } return reversedVersions, nil }
func main() { v, err := semver.Parse("0.0.1-alpha.preview.222+123.github") if err != nil { fmt.Printf("Error while parsing (not valid): %q", err) } fmt.Printf("Version to string: %q\n", v) fmt.Printf("Major: %d\n", v.Major) fmt.Printf("Minor: %d\n", v.Minor) fmt.Printf("Patch: %d\n", v.Patch) // Prerelease versions if len(v.Pre) > 0 { fmt.Println("Prerelease versions:") for i, pre := range v.Pre { fmt.Printf("%d: %q\n", i, pre) } } // Build meta data if len(v.Build) > 0 { fmt.Println("Build meta data:") for i, build := range v.Build { fmt.Printf("%d: %q\n", i, build) } } // New == Parse v001, err := semver.New("0.0.1") fmt.Println("\nUse Version.Compare for comparisons (-1, 0, 1):") fmt.Printf("%q is greater than %q: Compare == %d\n", v001, v, v001.Compare(v)) fmt.Printf("%q is less than %q: Compare == %d\n", v, v001, v.Compare(v001)) fmt.Printf("%q is equal to %q: Compare == %d\n", v, v, v.Compare(v)) fmt.Println("\nUse comparison helpers returning booleans:") fmt.Printf("%q is greater than %q: %t\n", v001, v, v001.GT(v)) fmt.Printf("%q is greater than equal %q: %t\n", v001, v, v001.GTE(v)) fmt.Printf("%q is greater than equal %q: %t\n", v, v, v.GTE(v)) fmt.Printf("%q is less than %q: %t\n", v, v001, v.LT(v001)) fmt.Printf("%q is less than equal %q: %t\n", v, v001, v.LTE(v001)) fmt.Printf("%q is less than equal %q: %t\n", v, v, v.LTE(v)) fmt.Println("\nManipulate Version in place:") v.Pre[0], err = semver.NewPRVersion("beta") if err != nil { fmt.Printf("Error parsing pre release version: %q", err) } fmt.Printf("Version to string: %q\n", v) fmt.Println("\nCompare Prerelease versions:") pre1, _ := semver.NewPRVersion("123") pre2, _ := semver.NewPRVersion("alpha") pre3, _ := semver.NewPRVersion("124") fmt.Printf("%q is less than %q: Compare == %d\n", pre1, pre2, pre1.Compare(pre2)) fmt.Printf("%q is greater than %q: Compare == %d\n", pre3, pre1, pre3.Compare(pre1)) fmt.Printf("%q is equal to %q: Compare == %d\n", pre1, pre1, pre1.Compare(pre1)) fmt.Println("\nValidate versions:") v.Build[0] = "?" err = v.Validate() if err != nil { fmt.Printf("Validation failed: %s\n", err) } fmt.Println("Create valid build meta data:") b1, _ := semver.NewBuildVersion("build123") v.Build[0] = b1 fmt.Printf("Version with new build version %q\n", v) _, err = semver.NewBuildVersion("build?123") if err != nil { fmt.Printf("Create build version failed: %s\n", err) } }
func updateAvailable() bool { current, _ := semver.New(version) latest := LatestAvailableVersion() return current.Compare(latest) == -1 }