pathlib

package module
v0.0.0-...-97a55a3 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2019 License: MIT Imports: 8 Imported by: 0

README

Go Report Card license

pathlib

A golang path library, it is easy to use. Similar to Python patblib.

该项目目前处于积极发展期😀,欢迎 issues

Installation

go get -u github.com/small-tk/pathlib

Enjoy


package main

import "github.com/small-tk/pathlib"

func main (){
	p := New("test.txt")

	fmt.Println(p.Absolute())
	fmt.Println(p.Cwd())
	fmt.Println(p.Parent())
	fmt.Println(p.Touch())

	fmt.Println(p.Unlink())
	fmt.Println(p.MkDir(os.ModePerm, true))
	fmt.Println(p.RmDir())
	fmt.Println(p.Open())
	fmt.Println(p.Chmod(os.ModePerm))
	fmt.Println(p.Chmod(os.ModePerm))

	fmt.Println(p.Exists())
	fmt.Println(p.IsDir())
	fmt.Println(p.IsFile())
	fmt.Println(p.IsAbs())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileRO

type FileRO interface {
	io.ReadCloser
	io.Seeker
	io.ReaderAt
}

FileRO represents the opened file for reading.

type FileRW

type FileRW interface {
	FileRO
	io.Writer
}

FileRW represents the opened file for reading and writing.

type MockPath

type MockPath struct {
	Fs   afero.Fs
	Path string
}

func (*MockPath) Absolute

func (p *MockPath) Absolute() (Path, error)

func (*MockPath) Chmod

func (p *MockPath) Chmod(mode os.FileMode) error

func (*MockPath) Cwd

func (p *MockPath) Cwd() (Path, error)

func (*MockPath) Exists

func (p *MockPath) Exists() bool

func (*MockPath) IsAbs

func (p *MockPath) IsAbs() bool

func (*MockPath) IsDir

func (p *MockPath) IsDir() bool

func (*MockPath) IsFile

func (p *MockPath) IsFile() bool

func (*MockPath) JoinPath

func (p *MockPath) JoinPath(elem ...string) Path

func (*MockPath) MkDir

func (p *MockPath) MkDir(mode os.FileMode, parents bool) error

func (*MockPath) Open

func (p *MockPath) Open() (FileRO, error)

func (*MockPath) OpenRW

func (p *MockPath) OpenRW(flag int, mode os.FileMode) (FileRW, error)

func (*MockPath) Parent

func (p *MockPath) Parent() (Path, error)

func (*MockPath) ReadBytes

func (p *MockPath) ReadBytes() ([]byte, error)

func (*MockPath) ReadText

func (p *MockPath) ReadText() (string, error)

func (*MockPath) Rename

func (p *MockPath) Rename(target Path) error

func (*MockPath) RmDir

func (p *MockPath) RmDir() error

func (*MockPath) String

func (p *MockPath) String() string

func (*MockPath) Touch

func (p *MockPath) Touch() error
func (p *MockPath) Unlink() error

func (*MockPath) WriteBytes

func (p *MockPath) WriteBytes(data []byte) error

func (*MockPath) WriteText

func (p *MockPath) WriteText(text string) error

type OsPath

type OsPath struct {
	Path string
}

OsPath

func (*OsPath) Absolute

func (p *OsPath) Absolute() (Path, error)

Absolute Returns an absolute representation of path.

func (*OsPath) Chmod

func (p *OsPath) Chmod(mode os.FileMode) error

Chmod changes the mode of the named file to mode.

func (*OsPath) Cwd

func (p *OsPath) Cwd() (Path, error)

Cwd Return a new path pointing to the current working directory.

func (*OsPath) Exists

func (p *OsPath) Exists() bool

Exists reports current path parent exists.

func (*OsPath) IsAbs

func (p *OsPath) IsAbs() bool

IsAbs reports whether the path is absolute.

func (*OsPath) IsDir

func (p *OsPath) IsDir() bool

IsDir reports Whether this path is a directory.

func (*OsPath) IsFile

func (p *OsPath) IsFile() bool

IsFile reports Whether this path is a regular file.

func (*OsPath) JoinPath

func (p *OsPath) JoinPath(elem ...string) Path

JoinPath Returns a new path, Combine current path with one or several arguments

func (*OsPath) MkDir

func (p *OsPath) MkDir(mode os.FileMode, parents bool) (err error)

MkDir Create a new directory at this given path.

func (*OsPath) Open

func (p *OsPath) Open() (FileRO, error)

Open opens the named file for reading.

func (*OsPath) OpenRW

func (p *OsPath) OpenRW(flag int, mode os.FileMode) (FileRW, error)

Open opens the named file for reading and writing.

func (*OsPath) Parent

func (p *OsPath) Parent() (Path, error)

Parent Return a new path for current path parent.

func (*OsPath) ReadBytes

func (p *OsPath) ReadBytes() ([]byte, error)

ReadBytes reads the file named by filename and returns the contents.

func (*OsPath) ReadText

func (p *OsPath) ReadText() (string, error)

ReadText reads the file and returns the contents.

func (*OsPath) Rename

func (p *OsPath) Rename(target Path) error

Rename renames (moves) the file or directory to target.

func (*OsPath) RmDir

func (p *OsPath) RmDir() error

RmDir Remove this directory. The directory must be empty.

func (*OsPath) String

func (p *OsPath) String() string

String returns the file path represented by string.

func (*OsPath) Touch

func (p *OsPath) Touch() error

Touch Create creates the named file with mode 0666 (before umask), regardless of whether it exists.

func (p *OsPath) Unlink() error

Unlink Remove this file or link.

func (*OsPath) WriteBytes

func (p *OsPath) WriteBytes(data []byte) error

WriteBytes writes a byte slice to the file.

func (*OsPath) WriteText

func (p *OsPath) WriteText(text string) error

WriteText writes a text to the file.

type Path

type Path interface {
	// Path operations
	Absolute() (Path, error)
	Cwd() (Path, error)
	Parent() (Path, error)
	JoinPath(elem ...string) Path
	String() string

	// File and directory operations
	Touch() error
	Unlink() error
	RmDir() error
	MkDir(mode os.FileMode, parents bool) error
	Open() (FileRO, error)
	OpenRW(flag int, mode os.FileMode) (FileRW, error)
	Chmod(mode os.FileMode) error
	Rename(target Path) error
	Exists() bool

	// Path testing
	IsDir() bool
	IsFile() bool
	IsAbs() bool

	// File operations
	ReadBytes() ([]byte, error)
	ReadText() (string, error)
	WriteBytes(data []byte) error
	WriteText(text string) error
}

func New

func New(path string) Path

New Returns a new path.

func NewMock

func NewMock(fs afero.Fs, path string) Path

Jump to

Keyboard shortcuts

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