sys

package
v1.2.9 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2023 License: MIT Imports: 22 Imported by: 0

README

sys

The sys package is intended to provide system related helper functions for Golang commonly found with other languages.

Table of Contents

Functions

Copy(src, dst string) error
// Copy copies src to dst recursively.
// 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, but it's parent directory does.
func Copy(src, dst string) error
// CopyFile copies a single file from src to dst.
// 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, but it's parent directory does.
func CopyFile(src, dst string) (err error) {

Documentation

Overview

Package sys provides os level helper functions for interacting with the system

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(target string) (result string, err error)

Abs gets the absolute path, taking into account path expansion and protocols

func AllDirs

func AllDirs(root string, opts ...*opt.Opt) (result []string, err error)

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

func AllFiles(root string, opts ...*opt.Opt) (result []string, err error)

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

func AllPaths(root string, opts ...*opt.Opt) (result []string, err error)

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

func AnyDir(src string) bool

AnyDir returns true if the given path is a directory or a symlink to a directory

func AnyFile added in v1.1.47

func AnyFile(src string) bool

AnyFile returns true if the given path is a file or a symlink to a file

func Base

func Base(src string) (result string)

Base wraps the filepath.Base but doesn't default to . when empty

func Chmod

func Chmod(path string, mode os.FileMode, opts ...*opt.Opt) (err error)

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

func Chown(path string, uid, gid int, opts ...*opt.Opt) (err error)

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

func Copy(src, dst string, opts ...*opt.Opt) (err error)

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

func CopyFile(src, dst string, opts ...*opt.Opt) (result string, err error)

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 Darwin

func Darwin() (result bool)

Darwin returns true if the OS is OSX

func Dir

func Dir(src string) (result string)

Dir wraps the filepath.Dir and trims off tailing slashes

func Dirs

func Dirs(target string) (result []string)

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

func ExecExists(target string) (ok bool)

ExecExists checks if the given executable exists on the PATH

func ExecOut

func ExecOut(str string, a ...interface{}) (out string, err error)

ExecOut executes the given command and returns the output as a string.

func ExecPath

func ExecPath(target string) (path string)

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

func Executable() (string, error)

Executable simply wraps os.Executable for convenience

func Exists

func Exists(src string) bool

Exists return true if the given path exists

func Expand

func Expand(target string) (path string, err error)

Expand the path to include the home prefix if necessary

func Files

func Files(target string) (result []string)

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

func FollowOpt(val bool) *opt.Opt

FollowOpt creates a new follow option with the given value -------------------------------------------------------------------------------------------------

func Glob

func Glob(path string, opts ...*opt.Opt) (sources []string, err error)

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

func InfoOpt(val *FileInfo) *opt.Opt

InfoOpt creates a new option info option with the given *FileInfo -------------------------------------------------------------------------------------------------

func IsDir

func IsDir(src string) bool

IsDir returns true if the given path is a directory

func IsFile

func IsFile(src string) bool

IsFile returns true if the given path is a file

func IsSymlink(src string) bool

IsSymlink returns true if the given path is a symlink

func IsSymlinkDir

func IsSymlinkDir(src string) bool

IsSymlinkDir returns true if the given symlink's target is a directory

func IsSymlinkFile

func IsSymlinkFile(src string) bool

IsSymlinkFile returns true if the given symlink's target is a file

func Linux

func Linux() (result bool)

Linux returns true if the OS is Linux

func MD5

func MD5(filename string) (result string, err error)

MD5 returns the md5 of the given file

func MkdirP

func MkdirP(dirname string, perms ...uint32) (dir string, err error)

MkdirP creates the target directory and any parent directories needed and returns the ABS path of the created directory

func Mode

func Mode(src string) (mode os.FileMode)

Mode implements os.FileInfo and returns bits of the file

func Move

func Move(src, dst string) (result string, err error)

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

func OnlyDirsOpt(val bool) *opt.Opt

OnlyDirsOpt creates a new only dirs option with the given value -------------------------------------------------------------------------------------------------

func OnlyFilesOpt

func OnlyFilesOpt(val bool) *opt.Opt

OnlyFilesOpt creates a new only dirs option with the given value -------------------------------------------------------------------------------------------------

func Paths

func Paths(target string) (result []string)

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 Pwd

func Pwd() (pwd string)

Pwd returns the current working directory

func ReadBytes

func ReadBytes(filepath string) (result []byte, err error)

ReadBytes returns the entire file as []byte

func ReadDirnames

func ReadDirnames(dirname string) (names []string, err error)

ReadDirnames reads the directory named by dirname and returns a list of directory entries sorted by filename.

func ReadLines

func ReadLines(filepath string) (result []string, err error)

ReadLines returns a new slice of string representing lines

func ReadLinesP added in v1.1.29

func ReadLinesP(reader io.Reader) (result []string)

ReadLinesP returns a new slice of string representing lines

func ReadString

func ReadString(filepath string) (result string, err error)

ReadString returns the entire file as a string

func RecurseOpt

func RecurseOpt(val bool) *opt.Opt

RecurseOpt creates a new recurse option with the given value -------------------------------------------------------------------------------------------------

func Remove

func Remove(target string) (err error)

Remove the given target file or empty directory. If there is an error it will be of type *PathError

func RemoveAll

func RemoveAll(target string) (err error)

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

func RootOpt(val bool) *opt.Opt

RootOpt creates a new root option with the given value -------------------------------------------------------------------------------------------------

func SharedDir

func SharedDir(first, second string) (result string)

SharedDir returns the dir portion that two paths share

func Shell added in v1.1.15

func Shell(str string, a ...interface{}) (out string, err error)

Shell executes the given command using a shell and returns the output as a string. Supports string interpolation like fmt.Sprintf

func Size

func Size(src string) (size int64)

Size returns the size of the file/dir in bytes

func SlicePath

func SlicePath(target string, i, j int) (result string)

SlicePath provides a ruby like slice function for path manipulation

func SplitCmd

func SplitCmd(cmd string) (slice []string)

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

func SwitchUser(uid, euid, suid, gid, egid, sgid int) (err error)

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(src, link string) error

Symlink creates newname as a symbolic link to link. If there is an error, it will be of type *LinkError.

func SymlinkTarget

func SymlinkTarget(src string) (target string, err error)

SymlinkTarget follows the symlink to get the path for the target. Will get the path regardless if the target actually exists.

func SymlinkTargetExists

func SymlinkTargetExists(src string) bool

SymlinkTargetExists returns true if the symlink's target exists

func Touch

func Touch(filepath string) (path string, err error)

Touch creates an empty text file similar to the linux touch command

func TrimExt

func TrimExt(target string) string

TrimExt removes the extension from the given target path

func TrimProtocol

func TrimProtocol(target string) string

TrimProtocol removes well known protocol prefixes

func TrimShared added in v1.1.48

func TrimShared(target, shared string) string

TrimShared returns portion of target that isn't in shared

func UserGID added in v1.1.15

func UserGID() int

UserGID is the currrent user's gid

func UserHome added in v1.1.15

func UserHome() (result string, err error)

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 UserName added in v1.1.15

func UserName() string

UserName is the currrent user's name

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 UserUID added in v1.1.15

func UserUID() int

UserUID is the currrent user's uid

func Walk

func Walk(root string, walkFn WalkFunc, opts ...*opt.Opt) (err error)

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 Windows

func Windows() (result bool)

Windows returns true if the OS is Windows

func WriteBytes

func WriteBytes(filepath string, data []byte, perms ...uint32) (err error)

WriteBytes is a pass through to ioutil.WriteBytes with default permissions

func WriteLines

func WriteLines(filepath string, lines []string, perms ...uint32) (err error)

WriteLines is a pass through to ioutil.WriteFile with default permissions

func WriteStream

func WriteStream(reader io.Reader, filepath string, perms ...uint32) (err error)

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.

func WriteString

func WriteString(filepath string, data string, perms ...uint32) (err error)

WriteString is a pass through to ioutil.WriteFile with default permissions

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

func Lstat(src string) (result *FileInfo, err error)

Lstat wraps os.Lstat to give back a FileInfo Resolves home dir and relative dir pathing into absolute paths

func ReadDir

func ReadDir(dirname string) (infos []*FileInfo, err error)

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

func (info *FileInfo) AnyDir() bool

AnyDir returns true if the given path is a directory or a symlink to a directory

func (*FileInfo) AnyFile added in v1.1.47

func (info *FileInfo) AnyFile() bool

AnyFile returns true if the given path is a file or a symlink to a file

func (*FileInfo) IsDir

func (info *FileInfo) IsDir() bool

IsDir returns true if the info is a directory

func (*FileInfo) IsFile

func (info *FileInfo) IsFile() bool

IsFile returns true if the info is a file

func (info *FileInfo) IsSymlink() bool

IsSymlink returns true if the info is a symlink

func (*FileInfo) IsSymlinkDir

func (info *FileInfo) IsSymlinkDir() bool

IsSymlinkDir returns true if the symlink's target is a directory

func (*FileInfo) IsSymlinkFile

func (info *FileInfo) IsSymlinkFile() bool

IsSymlinkFile returns true if the symlink's target is a file

func (*FileInfo) ModTime

func (info *FileInfo) ModTime() time.Time

ModTime implements os.FileInfo and is the modification time of the file

func (*FileInfo) Mode

func (info *FileInfo) Mode() os.FileMode

Mode implements os.FileInfo and returns bits of the file

func (*FileInfo) Name

func (info *FileInfo) Name() string

Name implements os.FileInfo and returns the base name of the file

func (*FileInfo) Size

func (info *FileInfo) Size() int64

Size implements os.FileInfo and returns the size of file in bytes

func (*FileInfo) SymlinkTarget

func (info *FileInfo) SymlinkTarget() (target string, err error)

SymlinkTarget follows the symlink to get the path for the target

func (*FileInfo) SymlinkTargetExists

func (info *FileInfo) SymlinkTargetExists() bool

SymlinkTargetExists returns true if the symlink's target exists

func (*FileInfo) Sys

func (info *FileInfo) Sys() interface{}

Sys implements os.FileInfo and provides access to the underlying data source

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

func KernelInfo added in v1.1.25

func KernelInfo() (kernel *Kernel, err error)

KernelInfo ...

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

func CurrentUser(cache ...bool) (u *User, err error)

CurrentUser gets the current system user. The internal user.Current() caches this result.

func LookupUserById added in v1.1.15

func LookupUserById(uid int) (u *User, err error)

LookupUserById gets the user for the given id

func (*User) IsRoot added in v1.1.15

func (u *User) IsRoot() bool

IsRoot detects if the current user has root permissions based on the user uid

type WalkFunc

type WalkFunc func(path string, info *FileInfo, err error) error

WalkFunc works the same as the filepath.WalkFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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