6 Commits

Author SHA1 Message Date
2bf9323203 fixing false undefined error 2022-08-26 02:41:13 -04:00
7cba7d1117 Adding env.HasEnv for single-value env exist logic 2022-05-29 22:52:03 -04:00
ecea194c0f fixing bug- wrong bool state check for RealPathExists. 2022-02-11 03:27:01 -05:00
008ed531a2 ...SplitN, not SplitAfterN 2021-12-18 04:46:43 -05:00
cf67bec392 fixing - need initialized map 2021-12-18 04:43:46 -05:00
0e194a07f4 d'oh. 2021-12-18 04:41:09 -05:00
4 changed files with 50 additions and 33 deletions

View File

@@ -1,11 +1,11 @@
package envs
import (
`regexp`
"regexp"
)
// Compiled regex patterns.
var (
reMaybeInt *regexp.Regexp = regexp.MustCompilePOSIX(`^(?P<sign>\+|-)[0-9]+$`)
reMaybeFloat *regexp.Regexp = regexp.MustCompilePOSIX(`(?P<sign>\+|-)?[0-9]+\.[0-9]+$`)
reMaybeInt *regexp.Regexp = regexp.MustCompile(`^(?P<sign>\+|-)[0-9]+$`)
reMaybeFloat *regexp.Regexp = regexp.MustCompile(`(?P<sign>\+|-)?[0-9]+\.[0-9]+$`)
)

View File

@@ -118,3 +118,17 @@ func GetPidEnvMapNative(pid uint32) (envMap map[string]interface{}, err error) {
return
}
/*
HasEnv is much like os.LookupEnv, but only returns a boolean for
if the environment variable key exists or not.
This is useful anywhere you may need to set a boolean in a func call
depending on the *presence* of an env var or not.
*/
func HasEnv(key string) (envIsSet bool) {
_, envIsSet = os.LookupEnv(key)
return
}

View File

@@ -16,8 +16,8 @@ func envListToMap(envs []string) (envMap map[string]string) {
envMap = make(map[string]string, 0)
for _, ev := range envs {
kv = strings.SplitAfterN(ev, "=", 2)
// I *think* SplitAfterN does this for me, but...
kv = strings.SplitN(ev, "=", 2)
// I *think* SplitN does this for me, but...
if len(kv) == 1 {
kv = append(kv, "")
}
@@ -35,6 +35,8 @@ func nativizeEnvMap(stringMap map[string]string) (envMap map[string]interface{})
var pathVar string = internal.GetPathEnvName()
var err error
envMap = make(map[string]interface{}, 0)
for k, v := range stringMap {
// Check for PATH/Path - we handle this uniquely.

View File

@@ -20,13 +20,12 @@ package paths
import (
"errors"
`fmt`
`io/fs`
"fmt"
"io/fs"
"os"
"os/user"
"path/filepath"
`strings`
"strings"
// "syscall"
)
@@ -62,7 +61,7 @@ func ExpandHome(path *string) (err error) {
}
*/
// K but do it smarter.
unameSplit = strings.SplitAfterN(*path, string(os.PathSeparator), 2)
unameSplit = strings.SplitN(*path, string(os.PathSeparator), 2)
if len(unameSplit) != 2 {
unameSplit = append(unameSplit, "")
}
@@ -113,6 +112,8 @@ func MakeDirIfNotExist(path string) (err error) {
if !stat.Mode().IsDir() {
err = errors.New(fmt.Sprintf("path %v exists but is not a directory", locPath))
return
} else {
return
}
// This should probably never happen. Probably.
@@ -164,7 +165,7 @@ func RealPathExists(path *string) (exists bool, err error) {
}
if _, err = os.Stat(*path); err != nil {
if !errors.Is(err, fs.ErrNotExist) {
if errors.Is(err, fs.ErrNotExist) {
err = nil
}
return