1 // Copyright (c) 2013 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
14 const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
16 // These constants define the application version and follow the semantic
17 // versioning 2.0.0 spec (http://semver.org/).
23 // appPreRelease MUST only contain characters from semanticAlphabet
24 // per the semantic versioning spec.
25 appPreRelease = "beta"
28 // appBuild is defined as a variable so it can be overridden during the build
29 // process with '-ldflags "-X main.appBuild foo' if needed. It MUST only
30 // contain characters from semanticAlphabet per the semantic versioning spec.
33 // version returns the application version as a properly formed string per the
34 // semantic versioning 2.0.0 spec (http://semver.org/).
35 func version() string {
36 // Start with the major, minor, and patch versions.
37 version := fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch)
39 // Append pre-release version if there is one. The hyphen called for
40 // by the semantic versioning spec is automatically appended and should
41 // not be contained in the pre-release string. The pre-release version
42 // is not appended if it contains invalid characters.
43 preRelease := normalizeVerString(appPreRelease)
45 version = fmt.Sprintf("%s-%s", version, preRelease)
48 // Append build metadata if there is any. The plus called for
49 // by the semantic versioning spec is automatically appended and should
50 // not be contained in the build metadata string. The build metadata
51 // string is not appended if it contains invalid characters.
52 build := normalizeVerString(appBuild)
54 version = fmt.Sprintf("%s+%s", version, build)
60 // normalizeVerString returns the passed string stripped of all characters which
61 // are not valid according to the semantic versioning guidelines for pre-release
62 // version and build metadata strings. In particular they MUST only contain
63 // characters in semanticAlphabet.
64 func normalizeVerString(str string) string {
65 var result bytes.Buffer
66 for _, r := range str {
67 if strings.ContainsRune(semanticAlphabet, r) {
68 // Ignoring the error here since it can only fail if
69 // the the system is out of memory and there are much
70 // bigger issues at that point.
71 _, _ = result.WriteRune(r)
74 return result.String()