version: make all exported funcs compile-time constant or lazy

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2023-02-10 18:07:37 -08:00
committed by Dave Anderson
parent 8b2ae47c31
commit 70a2929a12
9 changed files with 240 additions and 183 deletions

View File

@@ -11,7 +11,7 @@ import (
"runtime"
"strconv"
"tailscale.com/syncs"
"tailscale.com/types/lazy"
"tailscale.com/util/lineread"
)
@@ -31,22 +31,20 @@ const (
WDMyCloud = Distro("wdmycloud")
)
var distroAtomic syncs.AtomicValue[Distro]
var distro lazy.SyncValue[Distro]
// Get returns the current distro, or the empty string if unknown.
func Get() Distro {
if d, ok := distroAtomic.LoadOk(); ok {
return d
}
var d Distro
switch runtime.GOOS {
case "linux":
d = linuxDistro()
case "freebsd":
d = freebsdDistro()
}
distroAtomic.Store(d) // even if empty
return d
return distro.Get(func() Distro {
switch runtime.GOOS {
case "linux":
return linuxDistro()
case "freebsd":
return freebsdDistro()
default:
return Distro("")
}
})
}
func have(file string) bool {
@@ -99,7 +97,7 @@ func freebsdDistro() Distro {
return ""
}
var dsmVersion syncs.AtomicValue[int]
var dsmVersion lazy.SyncValue[int]
// DSMVersion reports the Synology DSM major version.
//
@@ -108,33 +106,28 @@ func DSMVersion() int {
if runtime.GOOS != "linux" {
return 0
}
if Get() != Synology {
return 0
}
if v, ok := dsmVersion.LoadOk(); ok && v != 0 {
return v
}
// This is set when running as a package:
v, _ := strconv.Atoi(os.Getenv("SYNOPKG_DSM_VERSION_MAJOR"))
if v != 0 {
dsmVersion.Store(v)
return v
}
// But when run from the command line, we have to read it from the file:
lineread.File("/etc/VERSION", func(line []byte) error {
line = bytes.TrimSpace(line)
if string(line) == `majorversion="7"` {
v = 7
return io.EOF
return dsmVersion.Get(func() int {
if Get() != Synology {
return 0
}
if string(line) == `majorversion="6"` {
v = 6
return io.EOF
// This is set when running as a package:
v, _ := strconv.Atoi(os.Getenv("SYNOPKG_DSM_VERSION_MAJOR"))
if v != 0 {
return v
}
return nil
// But when run from the command line, we have to read it from the file:
lineread.File("/etc/VERSION", func(line []byte) error {
line = bytes.TrimSpace(line)
if string(line) == `majorversion="7"` {
v = 7
return io.EOF
}
if string(line) == `majorversion="6"` {
v = 6
return io.EOF
}
return nil
})
return v
})
if v != 0 {
dsmVersion.Store(v)
}
return v
}