-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NOD-549] Add versioning support (#5)
* [NOD-495] Set required version of kaspad in go.mod + delete docker/README * [NOD-549] Update version to 0.1.0 and allow injection of appBuild * [NOD-549] Style corrections in version/version.go * [NOD-549] Move helpMsg closer to where it's needed * [NOD-549] Update comment
- Loading branch information
1 parent
33984b0
commit ad564d6
Showing
4 changed files
with
87 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// validCharacters is a list of characters valid in the appBuild string | ||
const validCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" | ||
|
||
const ( | ||
appMajor uint = 0 | ||
appMinor uint = 1 | ||
appPatch uint = 0 | ||
) | ||
|
||
// appBuild is defined as a variable so it can be overridden during the build | ||
// process with '-ldflags "-X github.com/kaspanet/dnsseeder/version.appBuild=foo"' if needed. | ||
// It MUST only contain characters from validCharacters. | ||
var appBuild string | ||
|
||
var version = "" // string used for memoization of version | ||
|
||
// Version returns the application version as a properly formed string | ||
func Version() string { | ||
if version == "" { | ||
// Start with the major, minor, and patch versions. | ||
version = fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch) | ||
|
||
// Append build metadata if there is any. | ||
// Panic if any invalid characters are encountered | ||
if appBuild != "" { | ||
checkAppBuild(appBuild) | ||
|
||
version = fmt.Sprintf("%s-%s", version, appBuild) | ||
} | ||
} | ||
|
||
return version | ||
} | ||
|
||
// checkAppBuild verifies that appBuild does not contain any characters outside of validCharacters. | ||
// In case of any invalid characters checkAppBuild panics | ||
func checkAppBuild(appBuild string) { | ||
for _, r := range appBuild { | ||
if !strings.ContainsRune(validCharacters, r) { | ||
panic(fmt.Errorf("appBuild string (%s) contains forbidden characters. Only alphanumeric characters and dashes are allowed", appBuild)) | ||
} | ||
} | ||
} |