Files
go_goutils/tplx/sprigx/funcs_tpl_os.go
brent saner 07e0e587fa v1.16.4
ADDED:
* math, time functions to tplx/sprigx
FIXED:
* logging not initializing properly on some BSDs
2026-01-30 06:35:23 -05:00

100 lines
1.6 KiB
Go

package sprigx
import (
`os`
`os/user`
`strconv`
`strings`
)
/*
osGroupById returns os/user.LookupGroupId.
Can accept either a string (`"1000"`) or any
numeric type (`1000`, `-1000`, `1000.0`, `MyCustomType(1000)`, etc.)
*/
func osGroupById(gid any) (g *user.Group, err error) {
var i int
var NaN bool
var gidStr string
if i, NaN, err = toPosInt(gid); err != nil {
if NaN {
err = nil
if gidStr, err = toString(gid); err != nil {
return
}
} else {
return
}
} else {
gidStr = strconv.Itoa(i)
}
g, err = user.LookupGroupId(gidStr)
return
}
/*
osFQDN (tries to) return the FQDN of this host.
Currently it just calls os.Hostname() but may be extended to "try harder" in the future.
*/
func osFQDN() (fqdn string, err error) {
fqdn, err = os.Hostname()
return
}
/*
osHost returns the system's "host shortname".
Currently it just calls os.Hostname() and takes the first
"host label" (as RFCs refer to it), but it may be extended
in the future.
*/
func osHost() (hostNm string, err error) {
hostNm, err = os.Hostname()
if hostNm == "" {
return
}
hostNm = strings.Split(hostNm, ".")[0]
return
}
/*
osUserById returns an os/user.LookupId.
Can accept either a string (`"1000"`) or any
numeric type (`1000`, `-1000`, `1000.0`, `MyCustomType(1000)`, etc.)
*/
func osUserById(uid any) (u *user.User, err error) {
var i int
var NaN bool
var uidStr string
if i, NaN, err = toPosInt(uid); err != nil {
if NaN {
err = nil
if uidStr, err = toString(uid); err != nil {
return
}
} else {
return
}
} else {
uidStr = strconv.Itoa(i)
}
u, err = user.LookupId(uidStr)
return
}