ADDED:
* envs.MustEnv(), envs.MustEnvNoBlank()

UPDATED:
* dep mods, Go version
This commit is contained in:
brent saner
2026-07-09 19:29:21 -04:00
parent d248d72b5a
commit 8e70ffac38
9 changed files with 857 additions and 41 deletions
-30
View File
@@ -1,30 +0,0 @@
macOS:
HOME
LOGNAME
OLDPWD
PATH
PWD
SHELL
SHLVL
SSH_CLIENT
SSH_CONNECTION
SSH_TTY
TERM
TMPDIR
USER
_
e.g.
HOME=/Users/bts
LOGNAME=bts
OLDPWD=/Users/bts
PATH=/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
PWD=/Users/bts
SHELL=/bin/zsh
SHLVL=1
SSH_CLIENT=192.168.2.99 39690 22
SSH_CONNECTION=192.168.2.99 39690 192.168.2.148 22
SSH_TTY=/dev/ttys000
TERM=xterm-256color
TMPDIR=/var/folders/qv/bm6dlyd94x5fs6qpkfpj0jsr0000gq/T/
USER=bts
_=/usr/bin/env
+78
View File
@@ -0,0 +1,78 @@
- func and struct to return segregated system-level env vars vs. user env vars (mostly usable on windows) (see .TODO.go.UNFINISHED and ITERATION/EXPECTED DEFAULTS below)
- - for envs, provide helpers: UsernmEnv and PasswdEnv
-- each takes an argument, envVar, that specifies an env var name to lookup via envs.HasEnv.
-- returns (val string, ok bool, err error)
-- for PasswdEnv:
--- param `ask bool`; if envVar doesn't exist and `ask` is true, prompt user for password (https://pkg.go.dev/golang.org/x/term#ReadPassword)
--- param `prompt []byte`; if empty, use generic "enter password (will not echo back)", otherwise use `prompt` as prompt (only prompted if `ask` == true)
--- param `confirm bool`; if true, prompt for user confirmation
-- for UsernmEnv:
--- param `ask int`;
if > 0, always prompt user (using `prompt`) if EnvVar doesn't exist.
if == 0, use current username if `current == true` and prompt for confirmation/override (if `current == false`, always prompt).
if < 0, never prompt and fallback to current username if `current == true`, return empty/not ok if `current == false`.
--- param `current bool`; if envVar doesn't exist and `current` is true,
--- param `prompt []byte`; if empty, use generic "enter username" (use fmt.Scanln), otherwise use `prompt` as prompt (only prompted if `ask` >= 0)
#################################
## ITERATION/EXPECTED DEFAULTS ##
#################################
macOS:
HOME
LOGNAME
OLDPWD
PATH
PWD
SHELL
SHLVL
SSH_CLIENT
SSH_CONNECTION
SSH_TTY
TERM
TMPDIR
USER
_
e.g.
HOME=/Users/bts
LOGNAME=bts
OLDPWD=/Users/bts
PATH=/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
PWD=/Users/bts
SHELL=/bin/zsh
SHLVL=1
SSH_CLIENT=192.168.2.99 39690 22
SSH_CONNECTION=192.168.2.99 39690 192.168.2.148 22
SSH_TTY=/dev/ttys000
TERM=xterm-256color
TMPDIR=/var/folders/qv/bm6dlyd94x5fs6qpkfpj0jsr0000gq/T/
USER=bts
_=/usr/bin/env
Windows:
https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/shell/csidl.md
https://learn.microsoft.com/en-us/windows/deployment/usmt/usmt-recognized-environment-variables
https://learn.microsoft.com/en-us/windows/win32/procthread/environment-variables
https://cdn.callback.com/help/CXH/RefDoc/html/T_callback_ShellBoost_Core_WindowsShell_CSIDL.htm
https://ss64.com/ps/syntax-env.html
https://ss64.com/nt/syntax-variables.html
Linux:
https://specifications.freedesktop.org/basedir/latest/
https://ss64.com/bash/syntax-variables.html
environ(7)?
Might need to do an LFS install and run `env` there.
get list of standard envs in addn to POSIX (see below)
macOS + Linux:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap08.html
enumerate default bash and zsh env vars on each, and switch on $SHELL.
macOS:
https://developer.apple.com/documentation/xcode/environment-variable-reference (XCode only?)
https://ss64.com/mac/syntax-variables.html
needs `env` in clean environment
+24
View File
@@ -332,6 +332,30 @@ func InterpolateString(s *string) (err error) {
return
}
// MustEnv wraps [GetEnvErr], but will panic on non-nil err.
func MustEnv(key string) (value string) {
var err error
if value, err = GetEnvErr(key); err != nil {
panic(err)
}
return
}
// MustEnvNoBlank wraps [GetEnvErrNoBlank], but will panic on non-nil err.
func MustEnvNoBlank(key string, ignoreWhitespace bool) (value string) {
var err error
if value, err = GetEnvErrNoBlank(key, ignoreWhitespace); err != nil {
panic(err)
}
return
}
// interpolateMap is used by [Interpolate] for maps. v should be a [reflect.Value] of a map.
func interpolateMap(v reflect.Value) (err error) {