-
Notifications
You must be signed in to change notification settings - Fork 99
/
version.go
42 lines (33 loc) · 1.2 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package utils
import (
"regexp"
"strings"
"github.com/Masterminds/semver/v3"
)
var versionRx = regexp.MustCompile(`(\d+\.\d+\.\d+)(\-[^\+]+)*(\+.+)*`)
var versionWithDashesRx = regexp.MustCompile(`(\d+\-\d+\-\d+)(\-[^\+]+)*(\+.+)*`)
// IsAboveMinVersion uses semver to check if `version` is >= minVersion
func IsAboveMinVersion(version, minVersion string) bool {
version = formatVersionTag(version)
v, err := semver.NewVersion(version)
if err != nil {
return false
}
c, err := semver.NewConstraint(">= " + minVersion)
if err != nil {
return false
}
return c.Check(v)
}
// formatVersionTag checks if the version tag uses dashes in lieu of periods, and replaces the first two dashes if so.
func formatVersionTag(versionTag string) string {
if versionWithDashesRx.FindString(versionTag) != "" {
versionTag = strings.Replace(versionTag, "-", ".", 2)
}
// Return versionTag if it matches with versionRx regex, or "".
return versionRx.FindString(versionTag)
}