Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
2bf9323203
|
|||
|
7cba7d1117
|
|||
|
ecea194c0f
|
|||
|
008ed531a2
|
|||
|
cf67bec392
|
|||
|
0e194a07f4
|
@@ -1,11 +1,11 @@
|
|||||||
package envs
|
package envs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
`regexp`
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Compiled regex patterns.
|
// Compiled regex patterns.
|
||||||
var (
|
var (
|
||||||
reMaybeInt *regexp.Regexp = regexp.MustCompilePOSIX(`^(?P<sign>\+|-)[0-9]+$`)
|
reMaybeInt *regexp.Regexp = regexp.MustCompile(`^(?P<sign>\+|-)[0-9]+$`)
|
||||||
reMaybeFloat *regexp.Regexp = regexp.MustCompilePOSIX(`(?P<sign>\+|-)?[0-9]+\.[0-9]+$`)
|
reMaybeFloat *regexp.Regexp = regexp.MustCompile(`(?P<sign>\+|-)?[0-9]+\.[0-9]+$`)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -118,3 +118,17 @@ func GetPidEnvMapNative(pid uint32) (envMap map[string]interface{}, err error) {
|
|||||||
|
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ func envListToMap(envs []string) (envMap map[string]string) {
|
|||||||
envMap = make(map[string]string, 0)
|
envMap = make(map[string]string, 0)
|
||||||
|
|
||||||
for _, ev := range envs {
|
for _, ev := range envs {
|
||||||
kv = strings.SplitAfterN(ev, "=", 2)
|
kv = strings.SplitN(ev, "=", 2)
|
||||||
// I *think* SplitAfterN does this for me, but...
|
// I *think* SplitN does this for me, but...
|
||||||
if len(kv) == 1 {
|
if len(kv) == 1 {
|
||||||
kv = append(kv, "")
|
kv = append(kv, "")
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,8 @@ func nativizeEnvMap(stringMap map[string]string) (envMap map[string]interface{})
|
|||||||
var pathVar string = internal.GetPathEnvName()
|
var pathVar string = internal.GetPathEnvName()
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
envMap = make(map[string]interface{}, 0)
|
||||||
|
|
||||||
for k, v := range stringMap {
|
for k, v := range stringMap {
|
||||||
|
|
||||||
// Check for PATH/Path - we handle this uniquely.
|
// Check for PATH/Path - we handle this uniquely.
|
||||||
|
|||||||
@@ -20,19 +20,18 @@ package paths
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
`fmt`
|
"fmt"
|
||||||
`io/fs`
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
`strings`
|
"strings"
|
||||||
|
|
||||||
// "syscall"
|
// "syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ExpandHome will take a tilde(~)-prefixed path and resolve it to the actual path in-place.
|
ExpandHome will take a tilde(~)-prefixed path and resolve it to the actual path in-place.
|
||||||
"Nested" user paths (~someuser/somechroot/~someotheruser) are not supported as home directories are expected to be absolute paths.
|
"Nested" user paths (~someuser/somechroot/~someotheruser) are not supported as home directories are expected to be absolute paths.
|
||||||
*/
|
*/
|
||||||
func ExpandHome(path *string) (err error) {
|
func ExpandHome(path *string) (err error) {
|
||||||
|
|
||||||
@@ -62,7 +61,7 @@ func ExpandHome(path *string) (err error) {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
// K but do it smarter.
|
// 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 {
|
if len(unameSplit) != 2 {
|
||||||
unameSplit = append(unameSplit, "")
|
unameSplit = append(unameSplit, "")
|
||||||
}
|
}
|
||||||
@@ -84,9 +83,9 @@ func ExpandHome(path *string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
MakeDirIfNotExist will create a directory at a given path if it doesn't exist.
|
MakeDirIfNotExist will create a directory at a given path if it doesn't exist.
|
||||||
|
|
||||||
See also the documentation for RealPath.
|
See also the documentation for RealPath.
|
||||||
*/
|
*/
|
||||||
func MakeDirIfNotExist(path string) (err error) {
|
func MakeDirIfNotExist(path string) (err error) {
|
||||||
|
|
||||||
@@ -113,6 +112,8 @@ func MakeDirIfNotExist(path string) (err error) {
|
|||||||
if !stat.Mode().IsDir() {
|
if !stat.Mode().IsDir() {
|
||||||
err = errors.New(fmt.Sprintf("path %v exists but is not a directory", locPath))
|
err = errors.New(fmt.Sprintf("path %v exists but is not a directory", locPath))
|
||||||
return
|
return
|
||||||
|
} else {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// This should probably never happen. Probably.
|
// This should probably never happen. Probably.
|
||||||
@@ -121,12 +122,12 @@ func MakeDirIfNotExist(path string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
RealPath will transform a given path into the very best guess for an absolute path in-place.
|
RealPath will transform a given path into the very best guess for an absolute path in-place.
|
||||||
|
|
||||||
It is recommended to check err (if not nil) for an invalid path error. If this is true, the
|
It is recommended to check err (if not nil) for an invalid path error. If this is true, the
|
||||||
path syntax/string itself is not supported on the runtime OS. This can be done via:
|
path syntax/string itself is not supported on the runtime OS. This can be done via:
|
||||||
|
|
||||||
if errors.Is(err, fs.ErrInvalid) {...}
|
if errors.Is(err, fs.ErrInvalid) {...}
|
||||||
*/
|
*/
|
||||||
func RealPath(path *string) (err error) {
|
func RealPath(path *string) (err error) {
|
||||||
|
|
||||||
@@ -142,20 +143,20 @@ func RealPath(path *string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
RealPathExists is like RealPath, but will also return a boolean as to whether the path
|
RealPathExists is like RealPath, but will also return a boolean as to whether the path
|
||||||
actually exists or not.
|
actually exists or not.
|
||||||
|
|
||||||
Note that err *may* be os.ErrPermission/fs.ErrPermission, in which case the exists value
|
Note that err *may* be os.ErrPermission/fs.ErrPermission, in which case the exists value
|
||||||
cannot be trusted as a permission error occurred when trying to stat the path - if the
|
cannot be trusted as a permission error occurred when trying to stat the path - if the
|
||||||
calling user/process does not have read permission on e.g. a parent directory, then
|
calling user/process does not have read permission on e.g. a parent directory, then
|
||||||
exists may be false but the path may actually exist. This condition can be checked via
|
exists may be false but the path may actually exist. This condition can be checked via
|
||||||
via:
|
via:
|
||||||
|
|
||||||
if errors.Is(err, fs.ErrPermission) {...}
|
if errors.Is(err, fs.ErrPermission) {...}
|
||||||
|
|
||||||
See also the documentation for RealPath.
|
See also the documentation for RealPath.
|
||||||
|
|
||||||
In those cases, it may be preferable to use RealPathExistsStat and checking stat for nil.
|
In those cases, it may be preferable to use RealPathExistsStat and checking stat for nil.
|
||||||
*/
|
*/
|
||||||
func RealPathExists(path *string) (exists bool, err error) {
|
func RealPathExists(path *string) (exists bool, err error) {
|
||||||
|
|
||||||
@@ -164,7 +165,7 @@ func RealPathExists(path *string) (exists bool, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if _, err = os.Stat(*path); err != nil {
|
if _, err = os.Stat(*path); err != nil {
|
||||||
if !errors.Is(err, fs.ErrNotExist) {
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -176,11 +177,11 @@ func RealPathExists(path *string) (exists bool, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
RealPathExistsStat is like RealPathExists except it will also return the os.FileInfo
|
RealPathExistsStat is like RealPathExists except it will also return the os.FileInfo
|
||||||
for the path (assuming it exists).
|
for the path (assuming it exists).
|
||||||
|
|
||||||
If stat is nil, it is highly recommended to check err via the methods suggested
|
If stat is nil, it is highly recommended to check err via the methods suggested
|
||||||
in the documentation for RealPath and RealPathExists.
|
in the documentation for RealPath and RealPathExists.
|
||||||
*/
|
*/
|
||||||
func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) {
|
func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user