sys

package
v2.0.37+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2019 License: MIT Imports: 17 Imported by: 1

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 homedir expansion

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 with &Opt{"follow", false}. Paths are distinct.

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 with &Opt{"follow", 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 with &Opt{"follow", false}. Paths are distinct.

func Base

func Base(src string) (result string)

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

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 on with &Opt{"follow", true}

func CopyFile

func CopyFile(src, dst string, opts ...*opt.Opt) (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. Doesn't follow links by default but can be turned on with &Opt{"follow", true}

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

func Exists

func Exists(src string) bool

Exists return true if the given path exists

func Files

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

Files returns all files from the given target path, sorted by filename

func Home

func Home() (result string, err error)

Home returns the absolute home directory for the current user

func IsDarwin

func IsDarwin() (result bool)

IsDarwin returns true if the OS is OSX

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 IsLinux

func IsLinux() (result bool)

IsLinux returns true if the OS is Linux

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 directory

func MD5

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

MD5 returns the md5 of the given file

func MkdirP

func MkdirP(target 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 Move

func Move(src, dst 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

func Paths

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

Paths returns all directories/files from the given target path, sorted by filename

func Pwd

func Pwd() (pwd string)

Pwd returns the current working directory

func ReadDir

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

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

func ReadFile

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

ReadFile returns the entire file as a string

func ReadLines

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

ReadLines returns a new slice of string representing lines

func ReadYaml

func ReadYaml(target string) (obj map[string]interface{}, err error)

ReadYaml reads the target file and returns a map[string]interface{} data structure representing the yaml read in.

func Remove

func Remove(target string) 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) error

RemoveAll removes the target path and any children it contains. It removes everything it can but returns the first error it encounters. If the target path does not exist nil is returned

func SharedDir

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

SharedDir returns the dir portion that two paths share

func Size

func Size(src string) (size int64)

Size returns the size of the file in bytes

func SlicePath

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

SlicePath provides a ruby like slice function for path manipulation

func Symlink(oldname, newname string) error

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

func SymlinkTarget

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

SymlinkTarget follows the symlink to get the path for the target

func Touch

func Touch(target 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 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 with &Opt{"follow", false}

func WriteFile

func WriteFile(target string, data []byte, perms ...uint32) (err error)

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

func WriteFileA

func WriteFileA(target string, data string, perms ...uint32) (err error)

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

func WriteLines

func WriteLines(target 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, dest 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 WriteYaml

func WriteYaml(target string, obj interface{}, perms ...uint32) (err error)

WriteYaml converts the given obj interface{} into yaml then writes to disk with default permissions. Expects obj to be a structure that github.com/ghodss/yaml understands

Types

type FileInfo

type FileInfo struct {
	// contains filtered or unexported fields
}

FileInfo wraps the os.FileInfo interface and provide additional helper functions

func Lstat

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

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

func (*FileInfo) AbsPath

func (info *FileInfo) AbsPath() (path string, err error)

AbsPath returns the absolute path for this 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) Mode

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

Mode implements os.FileInfo and returns bits of the file

func (*FileInfo) ModeTime

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

ModeTime implements os.FileInfo and is the modification time 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) Path

func (info *FileInfo) Path() string

Path returns the absolute path for this 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) Sys

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

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

type WalkFunc

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

WalkFunc works the same as the filepath.WalkFunc

Jump to

Keyboard shortcuts

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