paths

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2024 License: GPL-3.0 Imports: 7 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpandHome

func ExpandHome(path *string) (err error)

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.

func GetFirst added in v1.2.0

func GetFirst(paths []string) (content []byte, isDir, ok bool)

GetFirst is the file equivalent of envs.GetFirst.

It iterates through paths, normalizing them along the way (so abstracted paths such as ~/foo/bar.txt and relative paths such as bar/baz.txt will still work), and returns the content of the first found existing file. If the first found path is a directory, content will be nil but isDir will be true (as will ok).

If no path exists, ok will be false.

As always, results are not guaranteed due to permissions, etc. potentially returning an inaccurate result.

This is a thin wrapper around GetFirstWithRef.

func GetFirstWithRef added in v1.2.0

func GetFirstWithRef(paths []string) (content []byte, isDir, ok bool, idx int)

GetFirstWithRef is the file equivalent of envs.GetFirstWithRef.

It behaves exactly like GetFirst, but with an additional returned value, idx, which specifies the index in paths in which a path was found.

As always, results are not guaranteed due to permissions, etc. potentially returning an inaccurate result.

func MakeDirIfNotExist

func MakeDirIfNotExist(path string) (err error)

MakeDirIfNotExist will create a directory at a given path if it doesn't exist.

See also the documentation for RealPath.

This is a bit more sane option than os.MkdirAll as it will normalize paths a little better.

func RealPath

func RealPath(path *string) (err error)

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 path syntax/string itself is not supported on the runtime OS. This can be done via:

if errors.Is(err, fs.ErrInvalid) {...}

func RealPathExists

func RealPathExists(path *string) (exists bool, err error)

RealPathExists is like RealPath, but will also return a boolean as to whether the path actually exists or not.

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 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 via:

if errors.Is(err, fs.ErrPermission) {...}

See also the documentation for RealPath.

In those cases, it may be preferable to use RealPathExistsStat and checking stat for nil.

func RealPathExistsStat

func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error)

RealPathExistsStat is like RealPathExists except it will also return the os.FileInfo for the path (assuming it exists).

If stat is nil, it is highly recommended to check err via the methods suggested in the documentation for RealPath and RealPathExists.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL