Documentation ¶
Overview ¶
Pathutil is I/O utility primary inspired by David Golden's [Path::Tiny](https://metacpan.org/pod/Path::Tiny). It is friendlier to use than path(https://golang.org/pkg/path/), filepath(https://golang.org/pkg/path/filepath/) and provides many of other functions which isn't in core libraries (like `Copy` for example)
SYNOPSIS
import ( "fmt" "github.com/JaSei/pathutil-go" ) // creating pathutil objects dir, _ := pathutil.New("/tmp") foo, _ := pathutil.New("foo.txt") subdir, _ := dir.Child("foo") bar, _ := subdir.Child("bar.txt") // stringifies as cleaned up path file, _ := pathutil.New("./foo.txt") fmt.Println(file) // "foo.txt" // reading files guts, _ := file.Slurp() lines, _ := file.Lines() // writing files bar.Spew(data) // reading directories children, _ := dir.Children() for _, child := range children { }
SEE ALSO ¶
* [Path::Tiny](https://metacpan.org/pod/Path::Tiny) for Perl
* [better files](https://github.com/pathikrit/better-files) for Scala
* [pathlib](https://docs.python.org/3/library/pathlib.html) for python
Index ¶
- type CryptoHash
- type LinesFunc
- type Path
- type PathImpl
- func (path PathImpl) Append(data string) error
- func (path PathImpl) AppendBytes(data []byte) (err error)
- func (path PathImpl) Basename() string
- func (path PathImpl) Canonpath() string
- func (path PathImpl) Chdir() (Path, error)
- func (path PathImpl) Child(childName ...string) (Path, error)
- func (path PathImpl) Children() ([]Path, error)
- func (srcPath PathImpl) CopyFile(dst string) (p Path, err error)
- func (path PathImpl) CopyFrom(reader io.Reader) (copyied int64, err error)
- func (path PathImpl) Crypto(hash crypto.Hash) (c *CryptoHash, err error)
- func (path PathImpl) Exists() bool
- func (path PathImpl) IsDir() bool
- func (path PathImpl) IsFile() bool
- func (path PathImpl) IsRegularFile() bool
- func (path PathImpl) Lines() ([]string, error)
- func (path PathImpl) LinesWalker(linesFunc LinesFunc) (err error)
- func (path PathImpl) MakePath() error
- func (path PathImpl) MakePathMode(mode os.FileMode) error
- func (path PathImpl) OpenReader() (ReadSeekCloser, error)
- func (path PathImpl) OpenWriter() (*os.File, error)
- func (path PathImpl) OpenWriterAppend() (*os.File, error)
- func (path PathImpl) Parent() Path
- func (path PathImpl) Remove() error
- func (path PathImpl) RemoveTree() error
- func (old PathImpl) Rename(new string) (Path, error)
- func (path PathImpl) Slurp() (string, error)
- func (path PathImpl) SlurpBytes() ([]byte, error)
- func (path PathImpl) Spew(content string) (err error)
- func (path PathImpl) SpewBytes(bytes []byte) (err error)
- func (path PathImpl) Stat() (os.FileInfo, error)
- func (path PathImpl) String() string
- func (path PathImpl) Visit(visitFunc VisitFunc, visitOpt VisitOpt)
- type ReadSeekCloser
- type TempOpt
- type VisitFunc
- type VisitOpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CryptoHash ¶
func (*CryptoHash) BinSum ¶
func (hash *CryptoHash) BinSum() []byte
BinSum method is like hash.Sum(nil)
func (*CryptoHash) HexSum ¶
func (hash *CryptoHash) HexSum() string
HexSum method retun hexstring representation of hash.Sum
type Path ¶
type Path interface { String() string Canonpath() string Basename() string Chdir() (Path, error) Rename(string) (Path, error) Stat() (os.FileInfo, error) IsDir() bool Exists() bool IsFile() bool IsRegularFile() bool Remove() error RemoveTree() error Visit(VisitFunc, VisitOpt) CopyFile(string) (Path, error) CopyFrom(io.Reader) (int64, error) Crypto(crypto.Hash) (*CryptoHash, error) MakePath() error MakePathMode(os.FileMode) error OpenReader() (ReadSeekCloser, error) OpenWriter() (*os.File, error) OpenWriterAppend() (*os.File, error) Slurp() (string, error) SlurpBytes() ([]byte, error) Spew(string) error SpewBytes([]byte) error Lines() ([]string, error) LinesWalker(LinesFunc) error Child(...string) (Path, error) Children() ([]Path, error) Parent() Path Append(string) error AppendBytes([]byte) error }
func Cwd ¶
Cwd create new Path from current working directory optional is possible to set subpath
for example
gitConfigPath, err := pathutil.Cwd('.git/config')
func Home ¶
Home create new Path from home directory optional is possible to set subpath
for example
initPath, err := pathutil.Home('.config/nvim/init.vim')
(internally use https://github.com/mitchellh/go-homedir library)
func New ¶
New construct Path
for example
path := New("/home/test", ".vimrc")
if you can use `Path` in `New`, you must use `.String()` method
func NewTempDir ¶
NewTempDir create temp directory
for cleanup use `defer`
tempdir, err := pathutil.NewTempDir(TempOpt{}) defer tempdir.RemoveTree()
func NewTempFile ¶
NewTempFile create temp file
for cleanup use defer
temp, err := NewTempFile(TempOpt{}) defer temp.Remove()
if you need only temp file name, you must delete file
temp, err := NewTempFile(TempFileOpt{}) temp.Remove()
type PathImpl ¶
type PathImpl struct {
// contains filtered or unexported fields
}
type PathImpl implements Path interface
func (PathImpl) AppendBytes ¶
func (PathImpl) Chdir ¶
Chdir change current working directory do the path and return old current working directory
func (PathImpl) CopyFrom ¶
CopyFrom copy stream from reader to path (file after copy close and sync)
func (PathImpl) IsFile ¶
IsFile return true is path exists and not dir (symlinks, devs, regular files)
func (PathImpl) IsRegularFile ¶
IsRegularFile return true if path is regular file (wihtout devs, symlinks, ...)
func (PathImpl) LinesWalker ¶
LinesWalker read lines in file and call LinesFunc with line parameter
for example:
lines := make([]string, 0) linesFuncError := path.LinesWalker(func(line string) { lines = append(lines, line) })
func (PathImpl) MakePath ¶
Make path create directory(ies) in path if not exists (like `mkdir -p`) with default 0777 mode if you need set mode, use `MakePathMode`
func (PathImpl) MakePathMode ¶
Make path create directory(ies) in path if not exists (like `mkdir -p`) with default given mode
func (PathImpl) OpenReader ¶
func (path PathImpl) OpenReader() (ReadSeekCloser, error)
OpenReader retun ReadSeekCloser interface
for example:
path, _ := New("/bla/bla") r, err := path.OpenReader() if err != nil { panic(err) } defer r.Close()
func (PathImpl) OpenWriter ¶
OpenWriter retun *os.File as new file (like `>>`)
for example:
path, _ := NewFilePath(FilePathOpt{}) file, err := path.OpenWriter() if err != nil { panic(err) } defer func(){ file.Close() file.Sync() }() writer.Write(some_bytes)
func (PathImpl) OpenWriterAppend ¶
OpenWriterAppend create new writer, similar as `OpenWriter` but append (like `>`)
func (PathImpl) Parent ¶
path,_ := New("foo/bar/baz"); parent := path.Parent() // foo/bar path,_ := New("foo/wible.txt"); parent := path.Parent() // foo
Returns a `Path` of corresponding to the parent directory of the original directory or file
func (PathImpl) RemoveTree ¶
Remove tree of directory(ies) include files
err := path.RemoveTree
like os.RemoveAll
func (PathImpl) SlurpBytes ¶
type TempOpt ¶
type TempOpt struct { // directory where is temp file/dir create, empty string `""` (default) means TEMPDIR (`os.TempDir`) Dir string // name beginning with prefix // if prefix includes a "*", the random string replaces the last "*". Prefix string }
TempOpt is struct for configure new tempdir or tempfile