@@ -3,6 +3,7 @@ package buildinfo
3
3
import (
4
4
"fmt"
5
5
"runtime/debug"
6
+ "strings"
6
7
"sync"
7
8
"time"
8
9
24
25
tag string
25
26
)
26
27
28
+ const (
29
+ // develPrefix is prefixed to developer versions of the application.
30
+ develPrefix = "v0.0.0-devel"
31
+ )
32
+
27
33
// Version returns the semantic version of the build.
28
34
// Use golang.org/x/mod/semver to compare versions.
29
35
func Version () string {
@@ -35,7 +41,7 @@ func Version() string {
35
41
if tag == "" {
36
42
// This occurs when the tag hasn't been injected,
37
43
// like when using "go run".
38
- version = "v0.0.0-devel" + revision
44
+ version = develPrefix + revision
39
45
return
40
46
}
41
47
version = "v" + tag
@@ -48,6 +54,34 @@ func Version() string {
48
54
return version
49
55
}
50
56
57
+ // VersionsMatch compares the two versions. It assumes the versions match if
58
+ // the major and the minor versions are equivalent. Patch versions are
59
+ // disregarded. If it detects that either version is a developer build it
60
+ // returns true.
61
+ func VersionsMatch (v1 , v2 string ) bool {
62
+ // Developer versions are disregarded...hopefully they know what they are
63
+ // doing.
64
+ if strings .HasPrefix (v1 , develPrefix ) || strings .HasPrefix (v2 , develPrefix ) {
65
+ return true
66
+ }
67
+
68
+ v1Toks := strings .Split (v1 , "." )
69
+ v2Toks := strings .Split (v2 , "." )
70
+
71
+ // Versions should be formatted as "<major>.<minor>.<patch>".
72
+ // We assume malformed versions are evidence of a bug and return false.
73
+ if len (v1Toks ) < 3 || len (v2Toks ) < 3 {
74
+ return false
75
+ }
76
+
77
+ // Slice off the patch suffix. Patch versions should be non-breaking
78
+ // changes.
79
+ v1MajorMinor := strings .Join (v1Toks [:2 ], "." )
80
+ v2MajorMinor := strings .Join (v2Toks [:2 ], "." )
81
+
82
+ return v1MajorMinor == v2MajorMinor
83
+ }
84
+
51
85
// ExternalURL returns a URL referencing the current Coder version.
52
86
// For production builds, this will link directly to a release.
53
87
// For development builds, this will link to a commit.
0 commit comments