1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-09-16 17:50:44 +02:00

cli: use gobuild version handling (#2770)

This commit is contained in:
Kristoffer Dalby 2025-09-12 11:47:31 +02:00 committed by GitHub
parent ee0ef396a2
commit 3950f8f171
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 89 additions and 15 deletions

View File

@ -23,10 +23,6 @@ builds:
- linux_arm64 - linux_arm64
flags: flags:
- -mod=readonly - -mod=readonly
ldflags:
- -s -w
- -X github.com/juanfont/headscale/hscontrol/types.Version={{ .Version }}
- -X github.com/juanfont/headscale/hscontrol/types.GitCommitHash={{ .Commit }}
tags: tags:
- ts2019 - ts2019

View File

@ -71,19 +71,20 @@ func initConfig() {
disableUpdateCheck := viper.GetBool("disable_check_updates") disableUpdateCheck := viper.GetBool("disable_check_updates")
if !disableUpdateCheck && !machineOutput { if !disableUpdateCheck && !machineOutput {
versionInfo := types.GetVersionInfo()
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
types.Version != "dev" { !versionInfo.Dirty {
githubTag := &latest.GithubTag{ githubTag := &latest.GithubTag{
Owner: "juanfont", Owner: "juanfont",
Repository: "headscale", Repository: "headscale",
} }
res, err := latest.Check(githubTag, types.Version) res, err := latest.Check(githubTag, versionInfo.Version)
if err == nil && res.Outdated { if err == nil && res.Outdated {
//nolint //nolint
log.Warn().Msgf( log.Warn().Msgf(
"An updated version of Headscale has been found (%s vs. your current %s). Check it out https://github.com/juanfont/headscale/releases\n", "An updated version of Headscale has been found (%s vs. your current %s). Check it out https://github.com/juanfont/headscale/releases\n",
res.Current, res.Current,
types.Version, versionInfo.Version,
) )
} }
} }

View File

@ -7,6 +7,7 @@ import (
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)
versionCmd.Flags().StringP("output", "o", "", "Output format. Empty for human-readable, 'json', 'json-line' or 'yaml'")
} }
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
@ -15,9 +16,9 @@ var versionCmd = &cobra.Command{
Long: "The version of headscale.", Long: "The version of headscale.",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output") output, _ := cmd.Flags().GetString("output")
SuccessOutput(map[string]string{
"version": types.Version, info := types.GetVersionInfo()
"commit": types.GitCommitHash,
}, types.Version, output) SuccessOutput(info, info.String(), output)
}, },
} }

View File

@ -511,7 +511,8 @@ func (h *Headscale) Serve() error {
spew.Dump(h.cfg) spew.Dump(h.cfg)
} }
log.Info().Str("version", types.Version).Str("commit", types.GitCommitHash).Msg("Starting Headscale") versionInfo := types.GetVersionInfo()
log.Info().Str("version", versionInfo.Version).Str("commit", versionInfo.Commit).Msg("Starting Headscale")
log.Info(). log.Info().
Str("minimum_version", capver.TailscaleVersion(capver.MinSupportedCapabilityVersion)). Str("minimum_version", capver.TailscaleVersion(capver.MinSupportedCapabilityVersion)).
Msg("Clients with a lower minimum version will be rejected") Msg("Clients with a lower minimum version will be rejected")

View File

@ -1,6 +1,81 @@
package types package types
var ( import (
Version = "dev" "fmt"
GitCommitHash = "dev" "runtime"
"runtime/debug"
"strings"
"sync"
) )
type GoInfo struct {
Version string `json:"version"`
OS string `json:"os"`
Arch string `json:"arch"`
}
type VersionInfo struct {
Version string `json:"version"`
Commit string `json:"commit"`
BuildTime string `json:"buildTime"`
Go GoInfo `json:"go"`
Dirty bool `json:"dirty"`
}
func (v *VersionInfo) String() string {
var sb strings.Builder
version := v.Version
if v.Dirty && !strings.Contains(version, "dirty") {
version += "-dirty"
}
sb.WriteString(fmt.Sprintf("headscale version %s\n", version))
sb.WriteString(fmt.Sprintf("commit: %s\n", v.Commit))
sb.WriteString(fmt.Sprintf("build time: %s\n", v.BuildTime))
sb.WriteString(fmt.Sprintf("built with: %s %s/%s\n", v.Go.Version, v.Go.OS, v.Go.Arch))
return sb.String()
}
var buildInfo = sync.OnceValues(func() (*debug.BuildInfo, bool) {
return debug.ReadBuildInfo()
})
var GetVersionInfo = sync.OnceValue(func() *VersionInfo {
info := &VersionInfo{
Version: "dev",
Commit: "unknown",
BuildTime: "unknown",
Go: GoInfo{
Version: runtime.Version(),
OS: runtime.GOOS,
Arch: runtime.GOARCH,
},
Dirty: false,
}
buildInfo, ok := buildInfo()
if !ok {
return info
}
// Extract version from module path or main version
if buildInfo.Main.Version != "" && buildInfo.Main.Version != "(devel)" {
info.Version = buildInfo.Main.Version
}
// Extract build settings
for _, setting := range buildInfo.Settings {
switch setting.Key {
case "vcs.revision":
info.Commit = setting.Value
case "vcs.modified":
info.Dirty = setting.Value == "true"
case "vcs.time":
info.BuildTime = setting.Value
}
}
return info
})