update some stuff for paths, add env mappings
This commit is contained in:
153
paths/func.go
153
paths/func.go
@@ -20,108 +20,155 @@ package paths
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
`fmt`
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
`runtime`
|
||||
// "strconv"
|
||||
"strings"
|
||||
|
||||
// "syscall"
|
||||
)
|
||||
|
||||
var err error
|
||||
/*
|
||||
ExpandHome will take a tilde(~)-prefixed path and resolve it to the actual path in-place.
|
||||
Note that it only works for current user; the syntax ~someotheruser/foo/bar is currently unsupported.
|
||||
*/
|
||||
func ExpandHome(path *string) (err error) {
|
||||
|
||||
var usr *user.User
|
||||
|
||||
func ExpandHome(path *string) error {
|
||||
// Props to this guy.
|
||||
// https://stackoverflow.com/a/43578461/733214
|
||||
if len(*path) == 0 {
|
||||
return errors.New("empty path")
|
||||
err = errors.New("empty path")
|
||||
return
|
||||
} else if (*path)[0] != '~' {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// E(ffective)UID (e.g. chown'd user for SUID)
|
||||
/*
|
||||
uid := strconv.Itoa(syscall.Geteuid())
|
||||
usr, err := user.LookupId(euid)
|
||||
uid := strconv.Itoa(syscall.Geteuid())
|
||||
usr, err := user.LookupId(euid)
|
||||
*/
|
||||
// R(real)UID (invoking user)
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
// (Real)UID (invoking user)
|
||||
if usr, err = user.Current(); err != nil {
|
||||
return err
|
||||
}
|
||||
*path = filepath.Join(usr.HomeDir, (*path)[1:])
|
||||
return nil
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetPathEnv() ([]string, error) {
|
||||
paths := []string{}
|
||||
for _, p := range strings.Split(os.Getenv("PATH"), ":") {
|
||||
// GetPathEnv returns a slice of the PATH variable's items.
|
||||
func GetPathEnv() (paths []string, err error) {
|
||||
|
||||
var pathVar string = "PATH"
|
||||
var sep string = ":"
|
||||
|
||||
paths = make([]string, 0)
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
pathVar = "Path"
|
||||
sep = ";"
|
||||
}
|
||||
|
||||
for _, p := range strings.Split(os.Getenv(pathVar), sep) {
|
||||
if err = RealPath(&p); err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
paths = append(paths, p)
|
||||
}
|
||||
return paths, nil
|
||||
return
|
||||
}
|
||||
|
||||
func MakeDirIfNotExist(path *string) error {
|
||||
exists, stat, err := RealPathExistsStat(path)
|
||||
if err != nil {
|
||||
// MakeDirIfNotExist will create a directory at a given path if it doesn't exist.
|
||||
func MakeDirIfNotExist(path string) (err error) {
|
||||
|
||||
var stat os.FileInfo
|
||||
var exists bool
|
||||
var locPath string = path
|
||||
|
||||
if exists, stat, err = RealPathExistsStat(&locPath); err != nil {
|
||||
if !exists {
|
||||
// This, at least as of golang 1.15, uses the user's umask.
|
||||
// It does not actually create a dir with 0777.
|
||||
// It's up to the caller to do an os.Chmod() on the path after, if desired.
|
||||
os.MkdirAll(*path, 0777)
|
||||
return nil
|
||||
if err = os.MkdirAll(locPath, 0777); err != nil {
|
||||
return
|
||||
}
|
||||
err = nil
|
||||
return
|
||||
} else {
|
||||
return err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// So it exists, but it probably isn't a dir.
|
||||
if !stat.Mode().IsDir() {
|
||||
return errors.New(fmt.Sprintf("path %v exists but is not a directory", *path))
|
||||
err = errors.New(fmt.Sprintf("path %v exists but is not a directory", locPath))
|
||||
return
|
||||
}
|
||||
|
||||
// This should probably never happen. Probably.
|
||||
return errors.New("undefined behaviour")
|
||||
err = errors.New("undefined")
|
||||
return
|
||||
}
|
||||
|
||||
func RealPath(path *string) error {
|
||||
err := ExpandHome(path)
|
||||
if err != nil {
|
||||
return err
|
||||
// RealPath will transform a given path into the very best guess for an absolute path in-place.
|
||||
func RealPath(path *string) (err error) {
|
||||
|
||||
if err = ExpandHome(path); err != nil {
|
||||
return
|
||||
}
|
||||
*path, err = filepath.Abs(*path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
if *path, err = filepath.Abs(*path); err != nil {
|
||||
return
|
||||
}
|
||||
return nil
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func RealPathExists(path *string) (bool, error) {
|
||||
// I know it's hacky, but we use the bool as a sort of proto-state-machine thing.
|
||||
// If err != nil and bool is true, the error occurred during path absolution.
|
||||
// If err != nil and bool is false, the path does not exist.
|
||||
err := RealPath(path)
|
||||
if err != nil {
|
||||
return true, err
|
||||
/*
|
||||
RealPathExists is like RealPath, but will also return a boolean as to whether the path
|
||||
actually exists or not.
|
||||
|
||||
It's hacky, but the "exists" bool along with err is a sort of proto-state-machine.
|
||||
If err != nil and bool is true, the error occurred during path absolution.
|
||||
If err != nil and bool is false, the path does not exist.
|
||||
*/
|
||||
func RealPathExists(path *string) (exists bool, err error) {
|
||||
|
||||
if err = RealPath(path); err != nil {
|
||||
exists = true
|
||||
return
|
||||
}
|
||||
if _, err := os.Stat(*path); err != nil {
|
||||
return false, err
|
||||
|
||||
if _, err = os.Stat(*path); err != nil {
|
||||
return
|
||||
}
|
||||
return true, nil
|
||||
|
||||
exists = true
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func RealPathExistsStat(path *string) (bool, os.FileInfo, error) {
|
||||
// Same deal as RealPathExists.
|
||||
// If err != nil and bool is true, the error occurred during path absolution.
|
||||
// If err != nil and bool is false, the path does not exist.
|
||||
err := RealPath(path)
|
||||
if err != nil {
|
||||
return true, nil, err
|
||||
// RealPathExistsStat is like RealPathExists except it will also return the os.FileInfo for the path (assuming it exists).
|
||||
func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error) {
|
||||
|
||||
// See the comments for RealPathExists for details on this.
|
||||
if err = RealPath(path); err != nil {
|
||||
exists = true
|
||||
return
|
||||
}
|
||||
stat, err := os.Stat(*path)
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
|
||||
if stat, err = os.Stat(*path); err != nil {
|
||||
return
|
||||
}
|
||||
return true, stat, nil
|
||||
|
||||
exists = true
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user