mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-16 03:31:39 +00:00
version: unify and optimize the various not-version funcs
Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
parent
0fd2f71a1e
commit
59c254579e
@ -4,107 +4,35 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
tailscaleroot "tailscale.com"
|
tailscaleroot "tailscale.com"
|
||||||
"tailscale.com/syncs"
|
|
||||||
"tailscale.com/tailcfg"
|
"tailscale.com/tailcfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsMobile reports whether this is a mobile client build.
|
// IsMobile reports whether this is a mobile client build.
|
||||||
func IsMobile() bool {
|
func IsMobile() bool { return isMobile }
|
||||||
return runtime.GOOS == "android" || runtime.GOOS == "ios"
|
|
||||||
}
|
|
||||||
|
|
||||||
// OS returns runtime.GOOS, except instead of returning "darwin" it
|
// OS returns runtime.GOOS, except instead of returning "darwin" it
|
||||||
// returns "iOS" or "macOS".
|
// returns "iOS" or "macOS".
|
||||||
func OS() string {
|
func OS() string { return legacyOS }
|
||||||
if runtime.GOOS == "ios" {
|
|
||||||
return "iOS"
|
|
||||||
}
|
|
||||||
if runtime.GOOS == "darwin" {
|
|
||||||
return "macOS"
|
|
||||||
}
|
|
||||||
return runtime.GOOS
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsSandboxedMacOS reports whether this process is a sandboxed macOS
|
// IsSandboxedMacOS reports whether this process is a sandboxed macOS
|
||||||
// process (either the app or the extension). It is true for the Mac App Store
|
// process (either the app or the extension). It is true for the Mac App Store
|
||||||
// and macsys (System Extension) version on macOS, and false for
|
// and macsys (System Extension) version on macOS, and false for
|
||||||
// tailscaled-on-macOS.
|
// tailscaled-on-macOS.
|
||||||
func IsSandboxedMacOS() bool {
|
func IsSandboxedMacOS() bool { return isSandboxedMacOS }
|
||||||
if runtime.GOOS != "darwin" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if IsMacSysExt() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
exe, _ := os.Executable()
|
|
||||||
return strings.HasSuffix(exe, "/Contents/MacOS/Tailscale") || strings.HasSuffix(exe, "/Contents/MacOS/IPNExtension")
|
|
||||||
}
|
|
||||||
|
|
||||||
var isMacSysExt syncs.AtomicValue[bool]
|
|
||||||
|
|
||||||
// IsMacSysExt whether this binary is from the standalone "System
|
// IsMacSysExt whether this binary is from the standalone "System
|
||||||
// Extension" (a.k.a. "macsys") version of Tailscale for macOS.
|
// Extension" (a.k.a. "macsys") version of Tailscale for macOS.
|
||||||
func IsMacSysExt() bool {
|
func IsMacSysExt() bool { return isMacSysExt }
|
||||||
if runtime.GOOS != "darwin" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if b, ok := isMacSysExt.LoadOk(); ok {
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
exe, err := os.Executable()
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
v := filepath.Base(exe) == "io.tailscale.ipn.macsys.network-extension"
|
|
||||||
isMacSysExt.Store(v)
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsWindowsGUI reports whether the current process is the Windows GUI.
|
// IsWindowsGUI reports whether the current process is the Windows GUI.
|
||||||
func IsWindowsGUI() bool {
|
func IsWindowsGUI() bool { return isWindowsGUI }
|
||||||
if runtime.GOOS != "windows" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
exe, _ := os.Executable()
|
|
||||||
exe = filepath.Base(exe)
|
|
||||||
return strings.EqualFold(exe, "tailscale-ipn.exe") || strings.EqualFold(exe, "tailscale-ipn")
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
isUnstableOnce sync.Once
|
|
||||||
isUnstableBuild bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// IsUnstableBuild reports whether this is an unstable build.
|
// IsUnstableBuild reports whether this is an unstable build.
|
||||||
// That is, whether its minor version number is odd.
|
// That is, whether its minor version number is odd.
|
||||||
func IsUnstableBuild() bool {
|
func IsUnstableBuild() bool { return isUnstable }
|
||||||
isUnstableOnce.Do(initUnstable)
|
|
||||||
return isUnstableBuild
|
|
||||||
}
|
|
||||||
|
|
||||||
func initUnstable() {
|
|
||||||
_, rest, ok := strings.Cut(Short, ".")
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
minorStr, _, ok := strings.Cut(rest, ".")
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
minor, err := strconv.Atoi(minorStr)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
isUnstableBuild = minor%2 == 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Meta is a JSON-serializable type that contains all the version
|
// Meta is a JSON-serializable type that contains all the version
|
||||||
// information.
|
// information.
|
||||||
|
@ -5,34 +5,97 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
tailscaleroot "tailscale.com"
|
tailscaleroot "tailscale.com"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Long is a full version number for this build, of the form
|
var (
|
||||||
// "x.y.z-commithash" for builds stamped in the usual way (see
|
// Long is a full version number for this build, of the form
|
||||||
// build_dist.sh in the root) or, for binaries built by hand with the
|
// "x.y.z-commithash" for builds stamped in the usual way (see build_dist.sh
|
||||||
// go tool, it's of the form "1.23.0-dev20220316-t29837428937{,-dirty}"
|
// in the root) or, for binaries built by hand with the go tool, it's of the
|
||||||
// where "1.23.0" comes from ../VERSION.txt and the part after dev
|
// form "1.23.0-dev20220316-t29837428937{,-dirty}" where "1.23.0" comes from
|
||||||
// is YYYYMMDD of the commit time, and the part after -t is the commit
|
// ../VERSION.txt and the part after dev is YYYYMMDD of the commit time, and
|
||||||
// hash. The dirty suffix is whether there are uncommitted changes.
|
// the part after -t is the commit hash. The dirty suffix is whether there
|
||||||
var Long = ""
|
// are uncommitted changes.
|
||||||
|
Long string
|
||||||
|
|
||||||
// Short is a short version number for this build, of the form
|
// Short is a short version number for this build, of the form
|
||||||
// "x.y.z" for builds stamped in the usual way (see
|
// "x.y.z" for builds stamped in the usual way (see
|
||||||
// build_dist.sh in the root) or, for binaries built by hand with the
|
// build_dist.sh in the root) or, for binaries built by hand with the
|
||||||
// go tool, it's like Long's dev form, but ending at the date part,
|
// go tool, it's like Long's dev form, but ending at the date part,
|
||||||
// of the form "1.23.0-dev20220316".
|
// of the form "1.23.0-dev20220316".
|
||||||
var Short = ""
|
Short string
|
||||||
|
|
||||||
|
// GitCommit, if non-empty, is the git commit of the
|
||||||
|
// github.com/tailscale/tailscale repository at which Tailscale was
|
||||||
|
// built. Its format is the one returned by `git describe --always
|
||||||
|
// --exclude "*" --dirty --abbrev=200`.
|
||||||
|
GitCommit string
|
||||||
|
|
||||||
|
// GitDirty is whether Go stamped the binary as having dirty version
|
||||||
|
// control changes in the working directory (debug.ReadBuildInfo
|
||||||
|
// setting "vcs.modified" was true).
|
||||||
|
GitDirty bool
|
||||||
|
|
||||||
|
// ExtraGitCommit, if non-empty, is the git commit of a "supplemental"
|
||||||
|
// repository at which Tailscale was built. Its format is the same as
|
||||||
|
// gitCommit.
|
||||||
|
//
|
||||||
|
// ExtraGitCommit is used to track the source revision when the main
|
||||||
|
// Tailscale repository is integrated into and built from another
|
||||||
|
// repository (for example, Tailscale's proprietary code, or the
|
||||||
|
// Android OSS repository). Together, GitCommit and ExtraGitCommit
|
||||||
|
// exactly describe what repositories and commits were used in a
|
||||||
|
// build.
|
||||||
|
ExtraGitCommit = ""
|
||||||
|
|
||||||
|
// isUnstable is whether the current build appears to be an unstable, i.e. with
|
||||||
|
// an odd minor version number.
|
||||||
|
isUnstable bool
|
||||||
|
|
||||||
|
// legacyOS is runtime.GOOS, except on apple devices where it's either "iOS" or
|
||||||
|
// "macOS" (with that exact case).
|
||||||
|
//
|
||||||
|
// This used to be a thing because Go reported both macOS and iOS as "darwin"
|
||||||
|
// and we needed to tell them apart. But then Go learned GOOS=ios and
|
||||||
|
// GOOS=darwin as separate things, but we're still stuck with this function
|
||||||
|
// because of the odd casing we picked, which has ossified into databases.
|
||||||
|
legacyOS string
|
||||||
|
|
||||||
|
// isMobile is whether the current build is for a mobile device.
|
||||||
|
isMobile bool
|
||||||
|
|
||||||
|
// isSandboxedMacOS is whether the current binary is any binary in the mac store
|
||||||
|
// or standalone sysext mac apps.
|
||||||
|
isSandboxedMacOS bool
|
||||||
|
|
||||||
|
// isMacSysExt is whether the current binary is the mac system extension binary.
|
||||||
|
isMacSysExt bool
|
||||||
|
|
||||||
|
// isWindowsGUI is whether the current binary is the Windows GUI binary.
|
||||||
|
isWindowsGUI bool
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
initVersion()
|
||||||
|
initUnstable()
|
||||||
|
initMiscTraits()
|
||||||
|
}
|
||||||
|
|
||||||
|
func initVersion() {
|
||||||
if Long != "" && Short != "" {
|
if Long != "" && Short != "" {
|
||||||
// Built in the recommended way, using build_dist.sh.
|
// Built in the recommended way, using build_dist.sh.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise, make approximate version info using Go 1.18's built-in git
|
||||||
|
// stamping.
|
||||||
bi, ok := debug.ReadBuildInfo()
|
bi, ok := debug.ReadBuildInfo()
|
||||||
if !ok {
|
if !ok {
|
||||||
Long = strings.TrimSpace(tailscaleroot.Version) + "-ERR-BuildInfo"
|
Long = strings.TrimSpace(tailscaleroot.Version) + "-ERR-BuildInfo"
|
||||||
@ -67,25 +130,38 @@ func init() {
|
|||||||
Long = Short + "-t" + commitHashAbbrev + dirty
|
Long = Short + "-t" + commitHashAbbrev + dirty
|
||||||
}
|
}
|
||||||
|
|
||||||
// GitCommit, if non-empty, is the git commit of the
|
func initUnstable() {
|
||||||
// github.com/tailscale/tailscale repository at which Tailscale was
|
_, rest, ok := strings.Cut(Short, ".")
|
||||||
// built. Its format is the one returned by `git describe --always
|
if !ok {
|
||||||
// --exclude "*" --dirty --abbrev=200`.
|
return
|
||||||
var GitCommit = ""
|
}
|
||||||
|
minorStr, _, ok := strings.Cut(rest, ".")
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
minor, err := strconv.Atoi(minorStr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
isUnstable = minor%2 == 1
|
||||||
|
}
|
||||||
|
|
||||||
// GitDirty is whether Go stamped the binary as having dirty version
|
func initMiscTraits() {
|
||||||
// control changes in the working directory (debug.ReadBuildInfo
|
exe, _ := os.Executable()
|
||||||
// setting "vcs.modified" was true).
|
base := filepath.Base(exe)
|
||||||
var GitDirty bool
|
|
||||||
|
|
||||||
// ExtraGitCommit, if non-empty, is the git commit of a "supplemental"
|
legacyOS = runtime.GOOS
|
||||||
// repository at which Tailscale was built. Its format is the same as
|
switch runtime.GOOS {
|
||||||
// gitCommit.
|
case "darwin":
|
||||||
//
|
legacyOS = "macOS"
|
||||||
// ExtraGitCommit is used to track the source revision when the main
|
isMacSysExt = strings.HasPrefix(base, "io.tailscale.ipn.macsys.network-extension")
|
||||||
// Tailscale repository is integrated into and built from another
|
isSandboxedMacOS = isMacSysExt || strings.HasSuffix(exe, "/Contents/MacOS/Tailscale") || strings.HasSuffix(exe, "/Contents/MacOS/IPNExtension")
|
||||||
// repository (for example, Tailscale's proprietary code, or the
|
case "ios":
|
||||||
// Android OSS repository). Together, GitCommit and ExtraGitCommit
|
legacyOS = "iOS"
|
||||||
// exactly describe what repositories and commits were used in a
|
isMobile = true
|
||||||
// build.
|
case "android":
|
||||||
var ExtraGitCommit = ""
|
isMobile = true
|
||||||
|
case "windows":
|
||||||
|
isWindowsGUI = strings.EqualFold(base, "tailscale-ipn.exe") || strings.EqualFold(base, "tailscale-ipn")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user