this is cool and all but the tables don't render properly
This commit is contained in:
63
version/consts.go
Normal file
63
version/consts.go
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* BSD 3-Clause License
|
||||
* Copyright (c) 2024, NetFire™ <https://netfire.com/>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
/*
|
||||
These variables are automatically handled by the build script.
|
||||
|
||||
DO NOT MODIFY THESE VARIABLES.
|
||||
Refer to /build.sh for how these are generated at build time and populated.
|
||||
*/
|
||||
var (
|
||||
sourceControl string = "git"
|
||||
repoUri string = "(unknown)"
|
||||
version string = "(unknown)"
|
||||
commitHash string
|
||||
commitShort string
|
||||
numCommitsAfterTag string
|
||||
isDirty string
|
||||
buildTime string
|
||||
buildUser string
|
||||
buildSudoUser string
|
||||
buildHost string
|
||||
)
|
||||
|
||||
var (
|
||||
patchRe *regexp.Regexp = regexp.MustCompile(`^(?P<patch>[0-9+])(?P<pre>-[0-9A-Za-z.-]+)?(?P<build>\+[0-9A-Za-z.-]+)?$`)
|
||||
patchReIsolated *regexp.Regexp = regexp.MustCompile(`^([0-9]+)(?:[-+](.*)?)?$`)
|
||||
)
|
||||
|
||||
// Ver is populated by main() from the build script and used in other places.
|
||||
var Ver *BuildInfo
|
||||
180
version/funcs.go
Normal file
180
version/funcs.go
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* BSD 3-Clause License
|
||||
* Copyright (c) 2024, NetFire™ <https://netfire.com/>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
`fmt`
|
||||
`runtime`
|
||||
`strconv`
|
||||
`strings`
|
||||
`time`
|
||||
|
||||
`golang.org/x/mod/semver`
|
||||
)
|
||||
|
||||
// Version returns the build information. See build.sh.
|
||||
func Version() (b *BuildInfo, err error) {
|
||||
|
||||
var n int
|
||||
var s string
|
||||
var sb strings.Builder
|
||||
var ok bool
|
||||
var canonical string
|
||||
var build strings.Builder
|
||||
// Why a map?
|
||||
// I forget but I had a reason for it once upon a time.
|
||||
// If you remove it and this func breaks, now you know why.
|
||||
// TODO: how much of this can be replaced by (runtime/debug).ReadBuildInfo()?
|
||||
var raw map[string]string = map[string]string{
|
||||
"repoUri": repoUri,
|
||||
"sourceControl": sourceControl,
|
||||
"tag": version,
|
||||
"hash": commitHash,
|
||||
"shortHash": commitShort,
|
||||
"postTagCommits": numCommitsAfterTag,
|
||||
"dirty": isDirty,
|
||||
"time": buildTime,
|
||||
"user": buildUser,
|
||||
"sudoUser": buildSudoUser,
|
||||
"host": buildHost,
|
||||
}
|
||||
var i BuildInfo = BuildInfo{
|
||||
SourceControl: raw["sourceControl"],
|
||||
GoVersion: runtime.Version(),
|
||||
RepoURI: raw["repoUri"],
|
||||
TagVersion: raw["tag"],
|
||||
// PostTagCommits: 0,
|
||||
CommitHash: raw["hash"],
|
||||
CommitId: raw["shortHash"],
|
||||
BuildUser: raw["user"],
|
||||
RealBuildUser: raw["sudoUser"],
|
||||
// BuildTime: time.Time{},
|
||||
BuildHost: raw["host"],
|
||||
Dirty: false,
|
||||
isDefined: false,
|
||||
raw: raw,
|
||||
}
|
||||
|
||||
if s, ok = raw["postTagCommits"]; ok && strings.TrimSpace(s) != "" {
|
||||
if n, err = strconv.Atoi(s); err == nil {
|
||||
i.PostTagCommits = uint(n)
|
||||
}
|
||||
}
|
||||
|
||||
if s, ok = raw["time"]; ok && strings.TrimSpace(s) != "" {
|
||||
if n, err = strconv.Atoi(s); err == nil {
|
||||
i.BuildTime = time.Unix(int64(n), 0).UTC()
|
||||
}
|
||||
}
|
||||
|
||||
switch strings.ToLower(raw["dirty"]) {
|
||||
case "1":
|
||||
i.Dirty = true
|
||||
case "0", "":
|
||||
i.Dirty = false
|
||||
}
|
||||
|
||||
// Build the short form. We use this for both BuildInfo.short and BuildInfo.verSplit.
|
||||
if i.TagVersion == "" {
|
||||
sb.WriteString(i.SourceControl)
|
||||
} else {
|
||||
sb.WriteString(i.TagVersion)
|
||||
}
|
||||
/*
|
||||
Now the mess. In order to conform to SemVer 2.0 (the spec this code targets):
|
||||
|
||||
1.) MAJOR.
|
||||
2.) MINOR.
|
||||
3.) PATCH
|
||||
4.) -PRERELEASE (OPTIONAL)
|
||||
(git commit, if building against a commit made past 1-3. Always included if untagged.)
|
||||
5.) +BUILDINFO (OPTIONAL)
|
||||
("+x[.y]", where x is # of commits past 4, or tag commit if 4 is empty. 0 is valid.
|
||||
y is optional, and is the string "dirty" if it is a "dirty" build - that is, uncommitted/unstaged changes.
|
||||
if x and y would be 0 and empty, respectively, then 5 is not included.)
|
||||
|
||||
1-3 are already written, or the source control software used if not.
|
||||
|
||||
Technically 4 and 5 are only included if 3 is present. We force patch to 0 if it's a tagged release and patch isn't present --
|
||||
so this is not relevant.
|
||||
*/
|
||||
// PRERELEASE
|
||||
if i.TagVersion == "" || i.PostTagCommits > 0 {
|
||||
// We use the full commit hash for git versions, short identifier for tagged releases.
|
||||
if i.TagVersion == "" {
|
||||
i.Pre = i.CommitHash
|
||||
} else {
|
||||
i.Pre = i.CommitId
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("-%v", i.Pre))
|
||||
}
|
||||
// BUILD
|
||||
if i.PostTagCommits > 0 || i.Dirty {
|
||||
build.WriteString(strconv.Itoa(int(i.PostTagCommits)))
|
||||
if i.Dirty {
|
||||
build.WriteString(".dirty")
|
||||
}
|
||||
i.Build = build.String()
|
||||
sb.WriteString(fmt.Sprintf("+%v", i.Build))
|
||||
}
|
||||
|
||||
i.short = sb.String()
|
||||
if semver.IsValid(i.short) {
|
||||
// DON'T DO THIS. It strips the prerelease and build info.
|
||||
// i.short = semver.Canonical(i.short)
|
||||
// Do this instead.
|
||||
canonical = semver.Canonical(i.short)
|
||||
// Numeric versions...
|
||||
if n, err = strconv.Atoi(strings.TrimPrefix(semver.Major(canonical), "v")); err != nil {
|
||||
err = nil
|
||||
} else {
|
||||
i.Major = uint(n)
|
||||
}
|
||||
if n, err = strconv.Atoi(strings.Split(semver.MajorMinor(canonical), ".")[1]); err != nil {
|
||||
err = nil
|
||||
} else {
|
||||
i.Minor = uint(n)
|
||||
}
|
||||
if n, err = strconv.Atoi(patchReIsolated.FindStringSubmatch(strings.Split(canonical, ".")[2])[1]); err != nil {
|
||||
err = nil
|
||||
} else {
|
||||
i.Patch = uint(n)
|
||||
}
|
||||
// The other tag assignments were performed above.
|
||||
}
|
||||
// The default is 0 for the numerics, so no big deal.
|
||||
|
||||
i.isDefined = true
|
||||
|
||||
b = &i
|
||||
|
||||
return
|
||||
}
|
||||
133
version/funcs_buildinfo.go
Normal file
133
version/funcs_buildinfo.go
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* BSD 3-Clause License
|
||||
* Copyright (c) 2024, NetFire™ <https://netfire.com/>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
`fmt`
|
||||
`strings`
|
||||
|
||||
`golang.org/x/mod/semver`
|
||||
)
|
||||
|
||||
// Detail returns a multiline string containing every possible piece of information we collect.
|
||||
func (b *BuildInfo) Detail() (ver string) {
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString(fmt.Sprintf("%v\n\n", b.short))
|
||||
sb.WriteString(fmt.Sprintf("====\nSource Control: %v\nRepo URI: %v\n", b.SourceControl, b.RepoURI))
|
||||
if b.TagVersion != "" {
|
||||
if b.PostTagCommits > 0 {
|
||||
sb.WriteString(fmt.Sprintf("Version Base: %v\nCommit Hash: %v\n", b.TagVersion, b.CommitHash))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("Version: %v\n", b.TagVersion))
|
||||
}
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("Version: (Unversioned)\nCommit Hash: %v\n", b.CommitHash))
|
||||
}
|
||||
|
||||
// Post-commits
|
||||
if b.TagVersion != "" {
|
||||
sb.WriteString(fmt.Sprintf("# of Commits Since %v: %v\n", b.TagVersion, b.PostTagCommits))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("# of Commits Since %v: %v\n", b.CommitId, b.PostTagCommits))
|
||||
}
|
||||
|
||||
sb.WriteString("Uncommitted/Unstaged Changes: ")
|
||||
if b.Dirty {
|
||||
sb.WriteString("yes (dirty/monkeypatched build)\n")
|
||||
} else {
|
||||
sb.WriteString("no (clean build)\n")
|
||||
}
|
||||
|
||||
if b.TagVersion != "" {
|
||||
sb.WriteString(
|
||||
fmt.Sprintf(
|
||||
"====\nMajor: %v\nMinor: %v\nPatch: %v\n",
|
||||
b.Major, b.Minor, b.Patch,
|
||||
),
|
||||
)
|
||||
}
|
||||
sb.WriteString("====\n")
|
||||
sb.WriteString(b.Meta())
|
||||
|
||||
ver = sb.String()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Short returns a uniquely identifiable version string.
|
||||
func (b *BuildInfo) Short() (ver string) {
|
||||
|
||||
ver = b.short
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Meta returns the build/compile-time info.
|
||||
func (b *BuildInfo) Meta() (meta string) {
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
if b.RealBuildUser != b.BuildUser && b.RealBuildUser != "" {
|
||||
sb.WriteString(fmt.Sprintf("Real Build User: %v\n", b.RealBuildUser))
|
||||
sb.WriteString(fmt.Sprintf("Sudo Build User: %v\n", b.BuildUser))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("Build User: %v\n", b.BuildUser))
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("Build Time: %v\nBuild Host: %v\nGo Version: %v\n", b.BuildTime, b.BuildHost, b.GoVersion))
|
||||
|
||||
meta = sb.String()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getReMap gets a regex map of map[pattern]match.
|
||||
func (b *BuildInfo) getReMap() (matches map[string]string) {
|
||||
|
||||
var s string = b.Short()
|
||||
var sections []string
|
||||
|
||||
if !semver.IsValid(s) {
|
||||
return
|
||||
}
|
||||
|
||||
sections = strings.Split(s, ".")
|
||||
|
||||
// The split should contain everything in the third element.
|
||||
// Or, if using a "simplified" semver, the last element.
|
||||
matches = make(map[string]string)
|
||||
for idx, str := range patchRe.FindStringSubmatch(sections[len(sections)-1]) {
|
||||
matches[patchRe.SubexpNames()[idx]] = str
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
85
version/types.go
Normal file
85
version/types.go
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* BSD 3-Clause License
|
||||
* Copyright (c) 2024, NetFire™ <https://netfire.com/>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
`time`
|
||||
)
|
||||
|
||||
// BuildInfo contains nativized version information.
|
||||
type BuildInfo struct {
|
||||
// RepoURI is where the source is from.
|
||||
RepoURI string
|
||||
// GoVersion is the version of the Go compiler used.
|
||||
GoVersion string
|
||||
// TagVersion is the most recent tag name on the current branch.
|
||||
TagVersion string
|
||||
// PostTagCommits is the number of commits after BuildInfo.TagVersion's commit on the current branch.
|
||||
PostTagCommits uint
|
||||
// CommitHash is the full commit hash.
|
||||
CommitHash string
|
||||
// CommitId is the "short" version of BuildInfo.CommitHash.
|
||||
CommitId string
|
||||
// BuildUser is the user the program was compiled under.
|
||||
BuildUser string
|
||||
// If compiled under sudo, BuildInfo.RealBuildUser is the user that called sudo.
|
||||
RealBuildUser string
|
||||
// BuildTime is the time and date of the program's build time.
|
||||
BuildTime time.Time
|
||||
// BuildHost is the host the binary was compiled on.
|
||||
BuildHost string
|
||||
// Dirty specifies if the source was "dirty" (uncommitted/unstaged etc. files) at the time of compilation.
|
||||
Dirty bool
|
||||
// SourceControl is the source control version used. Only relevant if not a "clean" build or untagged.
|
||||
SourceControl string
|
||||
// Major is the major version, expressed as an uint per spec.
|
||||
Major uint
|
||||
// Minor is the minor version, expressed as an uint per spec.
|
||||
Minor uint
|
||||
// Patch is the patch version, expressed as an uint per spec.
|
||||
Patch uint
|
||||
// Pre
|
||||
Pre string
|
||||
// Build
|
||||
Build string
|
||||
// isDefined specifies if this version was retrieved from the built-in values.
|
||||
isDefined bool
|
||||
// raw is the raw variable values.
|
||||
raw map[string]string
|
||||
/*
|
||||
verSplit is a slice of []string{Major, Minor, Patch, PreRelease, Build}
|
||||
|
||||
If using an actual point release, PreRelease and Build are probably blank.
|
||||
*/
|
||||
verSplit [5]string
|
||||
// short is the condensed version of verSplit.
|
||||
short string
|
||||
}
|
||||
Reference in New Issue
Block a user