1
0
mirror of https://github.com/go-task/task.git synced 2025-05-15 22:26:28 +02:00
task/internal/version/version.go

68 lines
1.5 KiB
Go
Raw Normal View History

package version
import (
2025-04-05 23:09:27 +01:00
_ "embed"
"runtime/debug"
2025-04-05 23:09:27 +01:00
"strings"
)
var (
2025-04-05 23:09:27 +01:00
//go:embed version.txt
version string
commit string
dirty bool
)
func init() {
2025-04-05 23:09:27 +01:00
version = strings.TrimSpace(version)
// Attempt to get build info from the Go runtime. We only use this if not
// built from a tagged version.
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version == "(devel)" {
commit = getCommit(info)
dirty = getDirty(info)
}
}
func getDirty(info *debug.BuildInfo) bool {
for _, setting := range info.Settings {
if setting.Key == "vcs.modified" {
return setting.Value == "true"
}
2025-04-05 23:09:27 +01:00
}
return false
}
func getCommit(info *debug.BuildInfo) string {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value[:7]
}
}
2025-04-05 23:09:27 +01:00
return ""
}
2025-04-05 23:09:27 +01:00
// GetVersion returns the version of Task. By default, this is retrieved from
// the embedded version.txt file which is kept up-to-date by our release script.
// However, it can also be overridden at build time using:
// -ldflags="-X 'github.com/go-task/task/v3/internal/version.version=vX.X.X'".
func GetVersion() string {
return version
}
2025-04-05 23:09:27 +01:00
// GetVersionWithBuildInfo is the same as [GetVersion], but it also includes
// the commit hash and dirty status if available. This will only work when built
// within inside of a Git checkout.
func GetVersionWithBuildInfo() string {
var buildInfo string
if commit != "" {
buildInfo += commit
}
if dirty {
buildInfo += "-dirty"
}
if buildInfo != "" {
return version + "-" + buildInfo
}
return version
}