v1.13.3
ADDED: * envs.GetEnvErr(), envs.GetEnvErrNoBlank(), envs.EnvErrNoVal This allows error-returned env vars for nonexistent/empty values.
This commit is contained in:
parent
82f58d4fbf
commit
e5f7296d2e
@ -1,20 +1,20 @@
|
||||
package envs
|
||||
|
||||
import (
|
||||
`bytes`
|
||||
`errors`
|
||||
`fmt`
|
||||
`io/ioutil`
|
||||
`os`
|
||||
`reflect`
|
||||
`strings`
|
||||
`sync`
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
`r00t2.io/goutils/multierr`
|
||||
`r00t2.io/goutils/structutils`
|
||||
`r00t2.io/sysutils/errs`
|
||||
`r00t2.io/sysutils/internal`
|
||||
`r00t2.io/sysutils/paths`
|
||||
"r00t2.io/goutils/multierr"
|
||||
"r00t2.io/goutils/structutils"
|
||||
"r00t2.io/sysutils/errs"
|
||||
"r00t2.io/sysutils/internal"
|
||||
"r00t2.io/sysutils/paths"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -45,6 +45,54 @@ func DefEnvBlank(key, fallback string) (value string) {
|
||||
return
|
||||
}
|
||||
|
||||
// GetEnvErr returns the value of key if it exists. If it does not exist, err will be an EnvErrNoVal.
|
||||
func GetEnvErr(key string) (value string, err error) {
|
||||
|
||||
var exists bool
|
||||
|
||||
if value, exists = os.LookupEnv(key); !exists {
|
||||
err = &EnvErrNoVal{
|
||||
VarName: key,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
GetEnvErrNoBlank behaves exactly like GetEnvErr with the
|
||||
additional stipulation that the value must not be empty.
|
||||
|
||||
An error for a value that is non-empty but whitespace only (e.g. VARNM="\t")
|
||||
can be returned if ignoreWhitespace == true.
|
||||
|
||||
(If it is, an EnvErrNoVal will also be returned.)
|
||||
*/
|
||||
func GetEnvErrNoBlank(key string, ignoreWhitespace bool) (value string, err error) {
|
||||
|
||||
var exists bool
|
||||
var e *EnvErrNoVal = &EnvErrNoVal{
|
||||
VarName: key,
|
||||
WasRequiredNonEmpty: true,
|
||||
IgnoreWhiteSpace: ignoreWhitespace,
|
||||
}
|
||||
|
||||
if value, exists = os.LookupEnv(key); !exists {
|
||||
err = e
|
||||
return
|
||||
} else {
|
||||
e.WasFound = true
|
||||
e.WasWhitespace = (strings.TrimSpace(value) == "") && (value != "")
|
||||
if ignoreWhitespace && e.WasWhitespace {
|
||||
err = e
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetEnvMap returns a map of all environment variables. All values are strings.
|
||||
func GetEnvMap() (envVars map[string]string) {
|
||||
|
||||
|
27
envs/funcs_enverrnoval.go
Normal file
27
envs/funcs_enverrnoval.go
Normal file
@ -0,0 +1,27 @@
|
||||
package envs
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Error conforms to a stdlib error interface.
|
||||
func (e *EnvErrNoVal) Error() (errStr string) {
|
||||
|
||||
var sb *strings.Builder = new(strings.Builder)
|
||||
|
||||
sb.WriteString("the variable '")
|
||||
sb.WriteString(e.VarName)
|
||||
sb.WriteString("' was ")
|
||||
if sb.WasFound {
|
||||
sb.WriteString("found")
|
||||
} else {
|
||||
sb.WriteString("not found")
|
||||
}
|
||||
if e.WasRequiredNonEmpty && e.WasFound {
|
||||
sb.WriteString(" but is empty and was required to be non-empty")
|
||||
}
|
||||
|
||||
errStr = sb.String()
|
||||
|
||||
return
|
||||
}
|
20
envs/types.go
Normal file
20
envs/types.go
Normal file
@ -0,0 +1,20 @@
|
||||
package envs
|
||||
|
||||
type (
|
||||
/*
|
||||
EnvErrNoVal is an error containing the variable that does not exist
|
||||
(and information surrounding the errored state).
|
||||
*/
|
||||
EnvErrNoVal struct {
|
||||
// VarName is the variable name/key name originally specified in the function call.
|
||||
VarName string `json:"var" toml:"VariableName" yaml:"Variable Name/Key" xml:"key,attr"`
|
||||
// WasFound is only used for GetEnvErrNoBlank(). It is true if the variable was found/populated.
|
||||
WasFound bool `json:"found" toml:"Found" yaml:"Found" xml:"found,attr"`
|
||||
// WasRequiredNonEmpty indicates that this error was returned in a context where a variable was required to be non-empty (e.g. via GetEnvErrNoBlank()) but was empty.
|
||||
WasRequiredNonEmpty bool `json:"reqd_non_empty" toml:"RequiredNonEmpty" yaml:"Required Non-Empty" xml:"reqNonEmpty,attr"`
|
||||
// IgnoreWhitespace is true if the value was found but its evaluation was done against a whitestripped version.
|
||||
IgnoreWhitespace bool `json:"ignore_ws" toml:"IgnoreWhitespace" yaml:"Ignore Whitespace" xml:"ignoreWhitespace,attr"`
|
||||
// WasWhitespace is true if the value was whitespace-only.
|
||||
WasWhitespace bool `json:"was_ws" toml:"WasWhitespace" yaml:"Was Whitespace Only" xml:"wasWhitespace,attr"`
|
||||
}
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user