3131 GoVersion string
3232)
3333
34- // semanticAlphabet is the set of characters that are permitted for use in an
35- // AppPreRelease .
36- const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-. "
34+ // versionFieldsAlphabet is the set of characters that are permitted for use in
35+ // a version string field .
36+ const versionFieldsAlphabet = "0123456789abcdefghijklmnopqrstuvwxyz "
3737
3838// These constants define the application version and follow the semantic
3939// versioning 2.0.0 spec (http://semver.org/).
@@ -47,9 +47,21 @@ const (
4747 // AppPatch defines the application patch for this binary.
4848 AppPatch uint = 2
4949
50- // AppPreRelease MUST only contain characters from semanticAlphabet
51- // per the semantic versioning spec.
52- AppPreRelease = "alpha"
50+ // AppStatus defines the release status of this binary (e.g. beta).
51+ AppStatus = "alpha"
52+
53+ // AppPreRelease defines the pre-release version of this binary.
54+ // It MUST only contain characters from the semantic versioning spec.
55+ AppPreRelease = ""
56+
57+ // GitTagIncludeStatus indicates whether the status should be included
58+ // in the git tag name.
59+ //
60+ // Including the app version status in the git tag may be problematic
61+ // for golang projects when importing them as dependencies. We therefore
62+ // include this flag to allow toggling the status on and off in a
63+ // standardised way across our projects.
64+ GitTagIncludeStatus = false
5365
5466 // defaultAgentName is the default name of the software that is added as
5567 // the first part of the user agent string.
@@ -66,8 +78,10 @@ var agentName = defaultAgentName
6678// the software tapd is bundled in (for example LiT). This function panics if
6779// the agent name contains characters outside of the allowed semantic alphabet.
6880func SetAgentName (newAgentName string ) {
81+ agentNameAlphabet := versionFieldsAlphabet + "-. "
82+
6983 for _ , r := range newAgentName {
70- if ! strings .ContainsRune (semanticAlphabet , r ) {
84+ if ! strings .ContainsRune (agentNameAlphabet , r ) {
7185 panic (fmt .Errorf ("rune: %v is not in the semantic " +
7286 "alphabet" , r ))
7387 }
@@ -81,7 +95,7 @@ func SetAgentName(newAgentName string) {
8195func UserAgent (initiator string ) string {
8296 // We'll only allow "safe" characters in the initiator portion of the
8397 // user agent string and spaces only if surrounded by other characters.
84- initiatorAlphabet := semanticAlphabet + ". "
98+ initiatorAlphabet := versionFieldsAlphabet + "- . "
8599 cleanInitiator := normalizeVerString (
86100 strings .TrimSpace (initiator ), initiatorAlphabet ,
87101 )
@@ -104,12 +118,22 @@ func UserAgent(initiator string) string {
104118}
105119
106120func init () {
121+ // Assert that AppStatus is valid according to the semantic versioning
122+ // guidelines for pre-release version and build metadata strings. In
123+ // particular, it MUST only contain characters in versionFieldsAlphabet.
124+ for _ , r := range AppStatus {
125+ if ! strings .ContainsRune (versionFieldsAlphabet , r ) {
126+ panic (fmt .Errorf ("rune: %v is not in the semantic " +
127+ "alphabet" , r ))
128+ }
129+ }
130+
107131 // Assert that AppPreRelease is valid according to the semantic
108132 // versioning guidelines for pre-release version and build metadata
109- // strings. In particular it MUST only contain characters in
110- // semanticAlphabet .
133+ // strings. In particular, it MUST only contain characters in
134+ // versionFieldsAlphabet .
111135 for _ , r := range AppPreRelease {
112- if ! strings .ContainsRune (semanticAlphabet , r ) {
136+ if ! strings .ContainsRune (versionFieldsAlphabet , r ) {
113137 panic (fmt .Errorf ("rune: %v is not in the semantic " +
114138 "alphabet" , r ))
115139 }
@@ -162,12 +186,28 @@ func semanticVersion() string {
162186 // Start with the major, minor, and patch versions.
163187 version := fmt .Sprintf ("%d.%d.%d" , AppMajor , AppMinor , AppPatch )
164188
165- // Append pre-release version if there is one. The hyphen called for
166- // by the semantic versioning spec is automatically appended and should
167- // not be contained in the pre-release string. The pre-release version
189+ // If defined, we will now sanitise the release status string. The
190+ // hyphen called for by the semantic versioning spec is automatically
191+ // appended and should not be contained in the status string. The status
168192 // is not appended if it contains invalid characters.
169- preRelease := normalizeVerString (AppPreRelease , semanticAlphabet )
170- if preRelease != "" {
193+ appStatus := normalizeVerString (AppStatus , versionFieldsAlphabet )
194+
195+ // If defined, we will now sanitise the pre-release version string. The
196+ // hyphen called for by the semantic versioning spec is automatically
197+ // appended and should not be contained in the pre-release string.
198+ // The pre-release version is not appended if it contains invalid
199+ // characters.
200+ preRelease := normalizeVerString (AppPreRelease , versionFieldsAlphabet )
201+
202+ // Append any status and pre-release strings to the version string.
203+ switch {
204+ case appStatus != "" && preRelease != "" :
205+ version = fmt .Sprintf (
206+ "%s-%s.%s" , version , appStatus , preRelease ,
207+ )
208+ case appStatus != "" :
209+ version = fmt .Sprintf ("%s-%s" , version , appStatus )
210+ case preRelease != "" :
171211 version = fmt .Sprintf ("%s-%s" , version , preRelease )
172212 }
173213
0 commit comments