Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e5f7296d2e | ||
![]() |
82f58d4fbf |
@ -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"`
|
||||
}
|
||||
)
|
@ -19,22 +19,23 @@
|
||||
package paths
|
||||
|
||||
import (
|
||||
`context`
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"path/filepath"
|
||||
`sort`
|
||||
"sort"
|
||||
"strings"
|
||||
`sync`
|
||||
`time`
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
// "syscall"
|
||||
|
||||
`github.com/djherbis/times`
|
||||
`r00t2.io/goutils/bitmask`
|
||||
"github.com/djherbis/times"
|
||||
"r00t2.io/goutils/bitmask"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -221,6 +222,68 @@ func RealPath(path *string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
RealPathJoin combines RealPath with (path).Join.
|
||||
|
||||
If dst is nil, then rootPath will be updated with the new value.
|
||||
You probably don't want that.
|
||||
*/
|
||||
func RealPathJoin(rootPath, dst *string, subPaths ...string) (err error) {
|
||||
|
||||
var newPath string
|
||||
var realDst *string
|
||||
|
||||
if err = RealPath(rootPath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if dst == nil {
|
||||
realDst = rootPath
|
||||
} else {
|
||||
realDst = dst
|
||||
}
|
||||
|
||||
newPath = path.Join(append([]string{*rootPath}, subPaths...)...)
|
||||
if err = RealPath(&newPath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
*realDst = newPath
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
RealPathJoinSys combines RealPath with (path/filepath).Join.
|
||||
|
||||
If dst is nil, then path will be updated with the new value.
|
||||
You probably don't want that.
|
||||
*/
|
||||
func RealPathJoinSys(path, dst *string, subPaths ...string) (err error) {
|
||||
|
||||
var newPath string
|
||||
var realDst *string
|
||||
|
||||
if err = RealPath(path); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if dst == nil {
|
||||
realDst = path
|
||||
} else {
|
||||
realDst = dst
|
||||
}
|
||||
|
||||
newPath = filepath.Join(append([]string{*path}, subPaths...)...)
|
||||
if err = RealPath(&newPath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
*realDst = newPath
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
RealPathExists is like RealPath, but will also return a boolean as to whether the path
|
||||
actually exists or not.
|
||||
@ -437,10 +500,10 @@ func SearchFsPathsAsync(matcher FsSearchCriteriaAsync) {
|
||||
|
||||
/*
|
||||
filterTimes checks a times.Timespec of a file using:
|
||||
* an age specified by the caller
|
||||
* an ageType bitmask for types of times to compare
|
||||
* an olderThan bool (if false, the file must be younger than)
|
||||
* an optional "now" timestamp for the age derivation.
|
||||
- an age specified by the caller
|
||||
- an ageType bitmask for types of times to compare
|
||||
- an olderThan bool (if false, the file must be younger than)
|
||||
- an optional "now" timestamp for the age derivation.
|
||||
*/
|
||||
func filterTimes(tspec times.Timespec, age *time.Duration, ageType *pathTimeType, olderThan bool, now *time.Time) (include bool) {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user