|
| 1 | +package terminal |
| 2 | + |
| 3 | +// Copyright (c) 2013-2017 The btcsuite developers |
| 4 | +// Copyright (c) 2015-2016 The Decred developers |
| 5 | +// Heavily inspired by https://github.com/btcsuite/btcd/blob/master/version.go |
| 6 | +// Copyright (C) 2015-2022 The Lightning Network Developers |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "fmt" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// Commit stores the current commit hash of this build, this should be set |
| 15 | +// using the -ldflags during compilation. |
| 16 | +var Commit string |
| 17 | + |
| 18 | +// semanticAlphabet |
| 19 | +const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" |
| 20 | + |
| 21 | +// These constants define the application version and follow the semantic |
| 22 | +// versioning 2.0.0 spec (http://semver.org/). |
| 23 | +const ( |
| 24 | + appMajor uint = 0 |
| 25 | + appMinor uint = 6 |
| 26 | + appPatch uint = 4 |
| 27 | + |
| 28 | + // appPreRelease MUST only contain characters from semanticAlphabet per |
| 29 | + // the semantic versioning spec. |
| 30 | + appPreRelease = "alpha" |
| 31 | +) |
| 32 | + |
| 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) |
| 38 | + |
| 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) |
| 44 | + if preRelease != "" { |
| 45 | + version = fmt.Sprintf("%s-%s", version, preRelease) |
| 46 | + } |
| 47 | + |
| 48 | + // Append commit hash of current build to version. |
| 49 | + version = fmt.Sprintf("%s commit=%s", version, Commit) |
| 50 | + |
| 51 | + return version |
| 52 | +} |
| 53 | + |
| 54 | +// normalizeVerString returns the passed string stripped of all characters |
| 55 | +// which are not valid according to the semantic versioning guidelines for |
| 56 | +// pre-release version and build metadata strings. In particular they MUST |
| 57 | +// only contain characters in semanticAlphabet. |
| 58 | +func normalizeVerString(str string) string { |
| 59 | + var result bytes.Buffer |
| 60 | + for _, r := range str { |
| 61 | + if strings.ContainsRune(semanticAlphabet, r) { |
| 62 | + result.WriteRune(r) |
| 63 | + } |
| 64 | + } |
| 65 | + return result.String() |
| 66 | +} |
0 commit comments