Documentation ¶
Overview ¶
Package sys provides os level helper functions for interacting with the system
Index ¶
- func Abs(target string) (result string, err error)
- func AllDirs(root string, opts ...*opt.Opt) (result []string, err error)
- func AllFiles(root string, opts ...*opt.Opt) (result []string, err error)
- func AllPaths(root string, opts ...*opt.Opt) (result []string, err error)
- func AnyDir(src string) bool
- func AnyFile(src string) bool
- func Base(src string) (result string)
- func Chmod(path string, mode os.FileMode, opts ...*opt.Opt) (err error)
- func Chown(path string, uid, gid int, opts ...*opt.Opt) (err error)
- func Copy(src, dst string, opts ...*opt.Opt) (err error)
- func CopyFile(src, dst string, opts ...*opt.Opt) (result string, err error)
- func Darwin() (result bool)
- func Dir(src string) (result string)
- func Dirs(target string) (result []string)
- func DropSudo() (err error)
- func DropSudoP() (err error)
- func ExecExists(target string) (ok bool)
- func ExecOut(str string, a ...interface{}) (out string, err error)
- func ExecPath(target string) (path string)
- func Executable() (string, error)
- func Exists(src string) bool
- func Expand(target string) (path string, err error)
- func Files(target string) (result []string)
- func FollowOpt(val bool) *opt.Opt
- func Glob(path string, opts ...*opt.Opt) (sources []string, err error)
- func InfoOpt(val *FileInfo) *opt.Opt
- func IsDir(src string) bool
- func IsFile(src string) bool
- func IsSymlink(src string) bool
- func IsSymlinkDir(src string) bool
- func IsSymlinkFile(src string) bool
- func Linux() (result bool)
- func MD5(filename string) (result string, err error)
- func MkdirP(dirname string, perms ...uint32) (dir string, err error)
- func Mode(src string) (mode os.FileMode)
- func Move(src, dst string) (result string, err error)
- func OnlyDirsOpt(val bool) *opt.Opt
- func OnlyFilesOpt(val bool) *opt.Opt
- func Paths(target string) (result []string)
- func Pwd() (pwd string)
- func ReadBytes(filepath string) (result []byte, err error)
- func ReadDirnames(dirname string) (names []string, err error)
- func ReadLines(filepath string) (result []string, err error)
- func ReadLinesP(reader io.Reader) (result []string)
- func ReadString(filepath string) (result string, err error)
- func RecurseOpt(val bool) *opt.Opt
- func Remove(target string) (err error)
- func RemoveAll(target string) (err error)
- func RootOpt(val bool) *opt.Opt
- func SharedDir(first, second string) (result string)
- func Shell(str string, a ...interface{}) (out string, err error)
- func Size(src string) (size int64)
- func SlicePath(target string, i, j int) (result string)
- func SplitCmd(cmd string) (slice []string)
- func Sudo() (err error)
- func SwitchUser(uid, euid, suid, gid, egid, sgid int) (err error)
- func Symlink(src, link string) error
- func SymlinkTarget(src string) (target string, err error)
- func SymlinkTargetExists(src string) bool
- func Touch(filepath string) (path string, err error)
- func TrimExt(target string) string
- func TrimProtocol(target string) string
- func TrimShared(target, shared string) string
- func UserGID() int
- func UserHome() (result string, err error)
- func UserIsRealRoot() (root bool)
- func UserIsRoot() (root bool)
- func UserName() string
- func UserRealGID() int
- func UserRealHome() string
- func UserRealName() string
- func UserRealUID() int
- func UserUID() int
- func Walk(root string, walkFn WalkFunc, opts ...*opt.Opt) (err error)
- func Windows() (result bool)
- func WriteBytes(filepath string, data []byte, perms ...uint32) (err error)
- func WriteLines(filepath string, lines []string, perms ...uint32) (err error)
- func WriteStream(reader io.Reader, filepath string, perms ...uint32) (err error)
- func WriteString(filepath string, data string, perms ...uint32) (err error)
- type FileInfo
- func (info *FileInfo) AnyDir() bool
- func (info *FileInfo) AnyFile() bool
- func (info *FileInfo) IsDir() bool
- func (info *FileInfo) IsFile() bool
- func (info *FileInfo) IsSymlink() bool
- func (info *FileInfo) IsSymlinkDir() bool
- func (info *FileInfo) IsSymlinkFile() bool
- func (info *FileInfo) ModTime() time.Time
- func (info *FileInfo) Mode() os.FileMode
- func (info *FileInfo) Name() string
- func (info *FileInfo) Size() int64
- func (info *FileInfo) SymlinkTarget() (target string, err error)
- func (info *FileInfo) SymlinkTargetExists() bool
- func (info *FileInfo) Sys() interface{}
- type Kernel
- type User
- type WalkFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllDirs ¶
AllDirs returns a list of all dirs recursively for the given root path in a deterministic order. Follows links by default, but can be stopped by passing FollowOpt(false). Paths are distinct. The root path can be included by passing the RootOpt(true).
func AllFiles ¶
AllFiles returns a list of all files recursively for the given root path in a deterministic order. Follows links by default, but can be stopped by passing FollowOpt(false). Paths are distinct.
func AllPaths ¶
AllPaths returns a list of all paths recursively for the given root path in a deterministic order including the root path as first entry. Follows links by default, but can be stopped by passing FollowOpt(false). Paths are distinct.
func AnyDir ¶ added in v1.1.47
AnyDir returns true if the given path is a directory or a symlink to a directory
func AnyFile ¶ added in v1.1.47
AnyFile returns true if the given path is a file or a symlink to a file
func Chmod ¶
Chmod wraps os.Chmod but provides path expansion, globbing, recursion and error tracing. For each resulting path if the file is a symbolic link, it changes the mode of the link's target. Recursively apply chmod to all files and directories by passing in RecurseOpt(true) Apply chmod to only directories or files with OnlyDirsOpt(true) and/or OnlyFilesOpt(true)
func Chown ¶
Chown wraps os.Chown but provides path expansion, globbing, recursion and error tracing. For each resulting path change the numeric uid and gid. If the file is a symbolic link, it changes the uid and gid of the link's target. A uid or gid of -1 means to not change that value. Recursively apply chown to all files and directories by passing in RecurseOpt(true)
func Copy ¶
Copy copies src to dst recursively, creating destination directories as needed. * Handles globbing e.g. Copy("./*", "../") * The dst will be copied to if it is an existing directory. * The dst will be a clone of the src if it doesn't exist. * Doesn't follow links by default but can be turned by passing in FollowOpt(true) * Following links will use the link name but replace its content with the target linked to
func CopyFile ¶
CopyFile copies a single file from src to dsty, creating destination directories as needed. The dst will be copied to if it is an existing directory. The dst will be a clone of the src if it doesn't exist. Supports passing in the FileInfo object directly with FollowOpt(true) Returns the destination path for copied file
func Dirs ¶
Dirs returns all directories from the given target path, sorted by filename Doesn't include the target itself only its children nor is this recursive.
func DropSudo ¶ added in v1.1.15
func DropSudo() (err error)
DropSudo switches back to the original user under the sudo mask. Preserves the ability to raise Sudo again.
func DropSudoP ¶ added in v1.1.15
func DropSudoP() (err error)
DropSudoP switches back to the original user under the sudo mask. Does not preserve the ability to raise Sudo again.
func ExecExists ¶
ExecExists checks if the given executable exists on the PATH
func ExecPath ¶
ExecPath wraps exec.LookPath to find the path of an executable using the PATH environment variable. Returns an empty string if not found.
func Executable ¶ added in v1.1.25
Executable simply wraps os.Executable for convenience
func Files ¶
Files returns all files from the given target path, sorted by filename. Doesn't include the target itself only its children nor is this recursive.
func FollowOpt ¶
FollowOpt creates a new follow option with the given value -------------------------------------------------------------------------------------------------
func Glob ¶
Glob wraps filepath.Glob but provides path expansion, recursion and error tracing. If no sources are found an empty string slice will be returned and a nil error. Enable recursion by passing in the option RecurseOpt(true).
func InfoOpt ¶
InfoOpt creates a new option info option with the given *FileInfo -------------------------------------------------------------------------------------------------
func IsSymlinkDir ¶
IsSymlinkDir returns true if the given symlink's target is a directory
func IsSymlinkFile ¶
IsSymlinkFile returns true if the given symlink's target is a file
func MkdirP ¶
MkdirP creates the target directory and any parent directories needed and returns the ABS path of the created directory
func Move ¶
Move the src path to the dst path. If the dst already exists and is not a directory src will replace it. If there is an error it will be of type *LinkError. Wraps os.Rename but fixes the issue where dst name is required. Returns the new location
func OnlyDirsOpt ¶
OnlyDirsOpt creates a new only dirs option with the given value -------------------------------------------------------------------------------------------------
func OnlyFilesOpt ¶
OnlyFilesOpt creates a new only dirs option with the given value -------------------------------------------------------------------------------------------------
func Paths ¶
Paths returns all directories/files from the given target path, sorted by filename. Doesn't include the target itself only its children nor is this recursive.
func ReadDirnames ¶
ReadDirnames reads the directory named by dirname and returns a list of directory entries sorted by filename.
func ReadLinesP ¶ added in v1.1.29
ReadLinesP returns a new slice of string representing lines
func ReadString ¶
ReadString returns the entire file as a string
func RecurseOpt ¶
RecurseOpt creates a new recurse option with the given value -------------------------------------------------------------------------------------------------
func Remove ¶
Remove the given target file or empty directory. If there is an error it will be of type *PathError
func RemoveAll ¶
RemoveAll removes the target path and any children it contains. It will retry the operation after attempting to correct file permissions on failure. If the target path does not exist nil is returned
func RootOpt ¶ added in v1.2.5
RootOpt creates a new root option with the given value -------------------------------------------------------------------------------------------------
func Shell ¶ added in v1.1.15
Shell executes the given command using a shell and returns the output as a string. Supports string interpolation like fmt.Sprintf
func SplitCmd ¶
SplitCmd splits this cmd into substrings around spaces taking into account bash like double and single quotes. Unmatched quotes throw and error and empty quotes are removed.
func Sudo ¶ added in v1.1.15
func Sudo() (err error)
Sudo switches back to sudo root. Returns an error if not allowed
func SwitchUser ¶ added in v1.1.15
SwitchUser to the given user/group Note the bug around switching uid/gid in linux https://github.com/golang/go/issues/1435 http://timetobleed.com/5-things-you-dont-know-about-user-ids-that-will-destroy-you/ requires you drop the group before the user and use a safe solution
func Symlink ¶
Symlink creates newname as a symbolic link to link. If there is an error, it will be of type *LinkError.
func SymlinkTarget ¶
SymlinkTarget follows the symlink to get the path for the target. Will get the path regardless if the target actually exists.
func SymlinkTargetExists ¶
SymlinkTargetExists returns true if the symlink's target exists
func TrimProtocol ¶
TrimProtocol removes well known protocol prefixes
func TrimShared ¶ added in v1.1.48
TrimShared returns portion of target that isn't in shared
func UserHome ¶ added in v1.1.15
UserHome returns the absolute home directory for the current user. os.UserHomeDir is used internall because as of Go 1.9 cgo is no longer required
func UserIsRealRoot ¶ added in v1.1.15
func UserIsRealRoot() (root bool)
UserIsRealRoot detects if the current user is the root user not by SUDO
func UserIsRoot ¶ added in v1.1.15
func UserIsRoot() (root bool)
UserIsRoot detects if the current user has root permissions based on the user uid
func UserRealGID ¶ added in v1.1.15
func UserRealGID() int
UserRealGID as opposed to GID is the current user's gid behind the sudo mask
func UserRealHome ¶ added in v1.1.15
func UserRealHome() string
UserRealHome as opposed to UserHome is the current user's home behind the sudo mask
func UserRealName ¶ added in v1.1.15
func UserRealName() string
UserRealName as opposed to Name is the current user's name behind the sudo mask
func UserRealUID ¶ added in v1.1.15
func UserRealUID() int
UserRealUID as opposed to UID is the current user's uid behind the sudo mask
func Walk ¶
Walk extends the filepath.Walk to allow for it to walk symlinks by default but can be turned off by passing in FollowOpt(false)
func WriteBytes ¶
WriteBytes is a pass through to ioutil.WriteBytes with default permissions
func WriteLines ¶
WriteLines is a pass through to ioutil.WriteFile with default permissions
func WriteStream ¶
WriteStream reads from the io.Reader and writes to the given file using io.Copy thus never filling memory i.e. streaming. dest will be overwritten if it exists.
Types ¶
type FileInfo ¶
type FileInfo struct { Path string // absolute path to the file set when created Obj os.FileInfo // handle on the actual OS object to use where needed }
FileInfo wraps the os.FileInfo interface and provide additional helper functions
func Lstat ¶
Lstat wraps os.Lstat to give back a FileInfo Resolves home dir and relative dir pathing into absolute paths
func ReadDir ¶
ReadDir reads the directory named by dirname and returns a list of directory entries sorted by filename. Similar to ioutil.ReadDir but using internal FileInfo types.
func (*FileInfo) AnyDir ¶ added in v1.1.47
AnyDir returns true if the given path is a directory or a symlink to a directory
func (*FileInfo) AnyFile ¶ added in v1.1.47
AnyFile returns true if the given path is a file or a symlink to a file
func (*FileInfo) IsSymlinkDir ¶
IsSymlinkDir returns true if the symlink's target is a directory
func (*FileInfo) IsSymlinkFile ¶
IsSymlinkFile returns true if the symlink's target is a file
func (*FileInfo) SymlinkTarget ¶
SymlinkTarget follows the symlink to get the path for the target
func (*FileInfo) SymlinkTargetExists ¶
SymlinkTargetExists returns true if the symlink's target exists
type Kernel ¶ added in v1.1.25
type Kernel struct { Arch string // x86_64 Release string // 5.3.1-arch1-1-ARCH Version string // 5.3.1 }
Kernel information
type User ¶ added in v1.1.15
type User struct { UID int // Uid is the user ID, will be 0 when sudo is used. GID int // Gid is the primary group ID, will be 0 when sudo is used. Name string // Username is the login name, will be root when sudo is used. Home string // HomeDir is the path to the user's home directory (if they have one). RealUID int // Real user uid behind the sudo mask RealGID int // Real user uid behind the sudo mask RealName string // Real user name behind the sudo mask RealHome string // Real user home behind the sudo mask // contains filtered or unexported fields }
User wraps the os.User interface and provide additional helper functions
func CurrentUser ¶ added in v1.1.15
CurrentUser gets the current system user. The internal user.Current() caches this result.
func LookupUserById ¶ added in v1.1.15
LookupUserById gets the user for the given id