paths

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: GPL-2.0 Imports: 10 Imported by: 109

README

paths: a golang library to simplify handling of paths

This library aims to simplify handling of the most common operations with paths.

For example code that looked like this:

buildPath := getPathFromSomewhere() // returns string
if buildPath != "" {
	cachePath, err := filepath.Abs(filepath.Join(buildPath, "cache"))
	...
}

can be transformed to:

buildPath := getPathFromSomewhere() // returns *paths.Path
if buildPath != nil {
	cachePath, err := buildPath.Join("cache").Abs()
	...
}

most operations that usually requires a bit of convoluted system calls are now simplified, for example to check if a path is a directory:

buildPath := "/path/to/somewhere"
srcPath := filepath.Join(buildPath, "src")
if info, err := os.Stat(srcPath); err == nil && !info.IsDir() {
    os.MkdirAll(srcPath)
}

using this library can be done this way:

buildPath := paths.New("/path/to/somewhere")
srcPath := buildPath.Join("src")
if !srcPath.IsDir() {
    scrPath.MkdirAll()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MkTempFile

func MkTempFile(dir *Path, prefix string) (*os.File, error)

MkTempFile creates a new temporary file in the directory dir with a name beginning with prefix, opens the file for reading and writing, and returns the resulting *os.File. If dir is nil, MkTempFile uses the default directory for temporary files (see paths.TempDir). Multiple programs calling TempFile simultaneously will not choose the same file. The caller can use f.Name() to find the pathname of the file. It is the caller's responsibility to remove the file when no longer needed.

Types

type Path

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

Path represents a path

func Getwd

func Getwd() (*Path, error)

Getwd returns a rooted path name corresponding to the current directory.

func MkTempDir

func MkTempDir(dir, prefix string) (*Path, error)

MkTempDir creates a new temporary directory in the directory dir with a name beginning with prefix and returns the path of the new directory. If dir is the empty string, TempDir uses the default directory for temporary files

func New

func New(path ...string) *Path

New creates a new Path object. If path is the empty string then nil is returned.

func NewFromFile

func NewFromFile(file *os.File) *Path

NewFromFile creates a new Path object using the path name obtained from the File object (see os.File.Name function).

func NullPath

func NullPath() *Path

NullPath return the path to the /dev/null equivalent for the current OS

func TempDir

func TempDir() *Path

TempDir returns the default path to use for temporary files

func (*Path) Abs

func (p *Path) Abs() (*Path, error)

Abs returns the absolute path of the current Path

func (*Path) Base

func (p *Path) Base() string

Base returns the last element of path

func (*Path) Chtimes

func (p *Path) Chtimes(atime, mtime time.Time) error

Chtimes changes the access and modification times of the named file, similar to the Unix utime() or utimes() functions.

func (*Path) Clean

func (p *Path) Clean() *Path

Clean Clean returns the shortest path name equivalent to path by purely lexical processing

func (*Path) Clone

func (p *Path) Clone() *Path

Clone create a copy of the Path object

func (*Path) CopyDirTo

func (p *Path) CopyDirTo(dst *Path) error

CopyDirTo recursively copies the directory denoted by the current path to the destination path. The source directory must exist and the destination directory must NOT exist (no implicit destination name allowed). Symlinks are not copied, they will be supported in future versions.

func (*Path) CopyTo

func (p *Path) CopyTo(dst *Path) error

CopyTo copies the contents of the file named src to the file named by dst. The file will be created if it does not already exist. If the destination file exists, all it's contents will be replaced by the contents of the source file. The file mode will be copied from the source and the copied data is synced/flushed to stable storage.

func (*Path) EqualsTo

func (p *Path) EqualsTo(other *Path) bool

EqualsTo return true if both paths are equal

func (*Path) EquivalentTo

func (p *Path) EquivalentTo(other *Path) bool

EquivalentTo return true if both paths are equivalent (they points to the same file even if they are lexicographically different)

func (*Path) Exist

func (p *Path) Exist() bool

Exist return true if the file denoted by this path exists, false in any other case (also in case of error).

func (*Path) ExistCheck

func (p *Path) ExistCheck() (bool, error)

ExistCheck return true if the path exists or false if the path doesn't exists. In case the check fails false is returned together with the corresponding error.

func (*Path) Ext

func (p *Path) Ext() string

Ext returns the file name extension used by path

func (p *Path) FollowSymLink() error

FollowSymLink transforms the current path to the path pointed by the symlink if path is a symlink, otherwise it does nothing

func (*Path) HasPrefix

func (p *Path) HasPrefix(prefixes ...string) bool

HasPrefix returns true if the file name has one of the given prefixes (the Base() method is used to obtain the file name used for the comparison)

func (*Path) HasSuffix

func (p *Path) HasSuffix(suffixies ...string) bool

HasSuffix returns true if the file name has one of the given suffixies

func (*Path) IsAbs

func (p *Path) IsAbs() bool

IsAbs returns true if the Path is absolute

func (*Path) IsDir

func (p *Path) IsDir() bool

IsDir returns true if the path exists and is a directory. In all the other cases (and also in case of any error) false is returned.

func (*Path) IsDirCheck

func (p *Path) IsDirCheck() (bool, error)

IsDirCheck return true if the path exists and is a directory or false if the path exists and is not a directory. In all the other case false and the corresponding error is returned.

func (*Path) IsInsideDir

func (p *Path) IsInsideDir(dir *Path) (bool, error)

IsInsideDir returns true if the current path is inside the provided dir

func (*Path) IsNotDir

func (p *Path) IsNotDir() bool

IsNotDir returns true if the path exists and is NOT a directory. In all the other cases (and also in case of any error) false is returned.

func (*Path) Join

func (p *Path) Join(paths ...string) *Path

Join create a new Path by joining the provided paths

func (*Path) JoinPath

func (p *Path) JoinPath(paths ...*Path) *Path

JoinPath create a new Path by joining the provided paths

func (*Path) MarshalJSON

func (p *Path) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (*Path) MkTempDir

func (p *Path) MkTempDir(prefix string) (*Path, error)

MkTempDir creates a new temporary directory inside the path pointed by the Path object with a name beginning with prefix and returns the path of the new directory.

func (*Path) Mkdir

func (p *Path) Mkdir() error

Mkdir create a directory denoted by the current path

func (*Path) MkdirAll

func (p *Path) MkdirAll() error

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error

func (*Path) NotExist

func (p *Path) NotExist() bool

NotExist return true if the file denoted by this path DO NOT exists, false in any other case (also in case of error).

func (*Path) Parent

func (p *Path) Parent() *Path

Parent returns all but the last element of path, typically the path's directory or the parent directory if the path is already a directory

func (*Path) Parents

func (p *Path) Parents() []*Path

Parents returns all the parents directories of the current path. If the path is absolute it starts from the current path to the root, if the path is relative is starts from the current path to the current directory. The path should be clean for this method to work properly (no .. or . or other shortcuts). This function does not performs any check on the returned paths.

func (*Path) ReadDir

func (p *Path) ReadDir() (PathList, error)

ReadDir returns a PathList containing the content of the directory pointed by the current Path

func (*Path) ReadFile

func (p *Path) ReadFile() ([]byte, error)

ReadFile reads the file named by filename and returns the contents

func (*Path) ReadFileAsLines

func (p *Path) ReadFileAsLines() ([]string, error)

ReadFileAsLines reads the file named by filename and returns it as an array of lines. This function takes care of the newline encoding differences between different OS

func (*Path) RelTo

func (p *Path) RelTo(r *Path) (*Path, error)

RelTo returns a relative Path that is lexically equivalent to r when joined to the current Path

func (*Path) Remove

func (p *Path) Remove() error

Remove removes the named file or directory

func (*Path) RemoveAll

func (p *Path) RemoveAll() error

RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error).

func (*Path) Rename

func (p *Path) Rename(newpath *Path) error

Rename renames (moves) the path to newpath. If newpath already exists and is not a directory, Rename replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories. If there is an error, it will be of type *os.LinkError.

func (*Path) Stat

func (p *Path) Stat() (os.FileInfo, error)

Stat returns a FileInfo describing the named file. The result is cached internally for next queries. To ensure that the cached FileInfo entry is updated just call Stat again.

func (*Path) String

func (p *Path) String() string

func (*Path) ToAbs

func (p *Path) ToAbs() error

ToAbs transofrm the current Path to the corresponding absolute path

func (*Path) Truncate

func (p *Path) Truncate() error

Truncate create an empty file named by path or if the file already exist it trucates it (delete all contents)

func (*Path) UnmarshalJSON

func (p *Path) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface

func (*Path) WriteFile

func (p *Path) WriteFile(data []byte) error

WriteFile writes data to a file named by filename. If the file does not exist, WriteFile creates it otherwise WriteFile truncates it before writing.

type PathList

type PathList []*Path

PathList is a list of Path

func NewPathList

func NewPathList(paths ...string) PathList

NewPathList creates a new PathList with the given paths

func (*PathList) Add

func (p *PathList) Add(path *Path)

Add adds a Path to the PathList

func (*PathList) AddAll

func (p *PathList) AddAll(paths PathList)

AddAll adds all Paths in the list passed as argument

func (*PathList) AddAllMissing

func (p *PathList) AddAllMissing(pathsToAdd PathList)

AddAllMissing adds all paths to the PathList excluding the paths already in the list

func (*PathList) AddIfMissing

func (p *PathList) AddIfMissing(path *Path)

AddIfMissing adds a Path to the PathList if the path is not already in the list

func (*PathList) AsStrings

func (p *PathList) AsStrings() []string

AsStrings return this path list as a string array

func (*PathList) Clone

func (p *PathList) Clone() PathList

Clone returns a copy of the current PathList

func (*PathList) Contains

func (p *PathList) Contains(pathToSearch *Path) bool

Contains check if the list contains a path that match exactly (EqualsTo) to the specified path

func (*PathList) ContainsEquivalentTo

func (p *PathList) ContainsEquivalentTo(pathToSearch *Path) bool

ContainsEquivalentTo check if the list contains a path that is equivalent (EquivalentTo) to the specified path

func (*PathList) FilterDirs

func (p *PathList) FilterDirs()

FilterDirs remove all entries except directories

func (*PathList) FilterOutDirs

func (p *PathList) FilterOutDirs()

FilterOutDirs remove all directories entries

func (*PathList) FilterOutHiddenFiles

func (p *PathList) FilterOutHiddenFiles()

FilterOutHiddenFiles remove all hidden files (files with the name starting with ".")

func (*PathList) FilterOutPrefix

func (p *PathList) FilterOutPrefix(prefixes ...string)

FilterOutPrefix remove all entries having the specified prefixes

func (*PathList) FilterOutSuffix

func (p *PathList) FilterOutSuffix(suffixies ...string)

FilterOutSuffix remove all entries having the specified suffix

func (*PathList) FilterPrefix

func (p *PathList) FilterPrefix(prefixes ...string)

FilterPrefix remove all entries not having one of the specified prefixes

func (*PathList) FilterSuffix

func (p *PathList) FilterSuffix(suffixies ...string)

FilterSuffix remove all entries not having the specified prefix

func (*PathList) Len

func (p *PathList) Len() int

func (*PathList) Less

func (p *PathList) Less(i, j int) bool

func (*PathList) Sort

func (p *PathList) Sort()

Sort sorts this pathlist

func (*PathList) Swap

func (p *PathList) Swap(i, j int)

func (*PathList) ToAbs

func (p *PathList) ToAbs() error

ToAbs calls Path.ToAbs() method on each path of the list. It stops at the first error and returns it. If all ToAbs calls are successful nil is returned.

Jump to

Keyboard shortcuts

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