pathlib

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: Apache-2.0 Imports: 6 Imported by: 35

README

pathlib

Build Status GitHub release (latest by date) Codecov GitHub go.mod Go version

Inspired by Python's pathlib, made better by Golang.

pathlib is an "object-oriented" package for manipulating filesystem path objects. It takes many cues from Python's pathlib, although it does not strictly adhere to its design philosophy. It provides a simple, intuitive, easy, and abstracted interface for dealing with many different types of filesystems.

pathlib is currently in the alpha stage of development, meaning the API is subject to change. However, the current state of the project is already proving to be highly useful.

Examples

OsFs

Beacuse pathlib treats afero filesystems as first-class citizens, you can instantiate a Path object with the filesystem of your choosing.

Code
package main

import (
	"fmt"
	"os"

	"github.com/chigopher/pathlib"
	"github.com/spf13/afero"
)

func main() {
	// Create a path on your regular OS filesystem
	path := pathlib.NewPathAfero("/home/ltclipp", afero.NewOsFs())

	subdirs, err := path.ReadDir()
	if err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}

	for _, dir := range subdirs {
		fmt.Println(dir.Name())
	}
}
Output
[ltclipp@landon-virtualbox examples]$ go build .
[ltclipp@landon-virtualbox examples]$ ./examples | tail
Music
Pictures
Public
Templates
Videos
git
go
mockery_test
snap
software
In-memory FS
Code
package main

import (
	"fmt"
	"os"

	"github.com/chigopher/pathlib"
	"github.com/spf13/afero"
)

func main() {
	// Create a path using an in-memory filesystem
	path := pathlib.NewPathAfero("/", afero.NewMemMapFs())
	hello := path.Join("hello_world.txt")
	hello.WriteFile([]byte("hello world!"), 0o644)

	subpaths, err := path.ReadDir()
	if err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}

	for _, subpath := range subpaths {
		fmt.Printf("Name: %s Mode: %o Size: %d\n", subpath.Name(), subpath.Mode(), subpath.Size())
	}

	bytes, _ := hello.ReadFile()
	fmt.Println(string(bytes))
}
Output
[ltclipp@landon-virtualbox examples]$ go build
[ltclipp@landon-virtualbox examples]$ ./examples 
Name: hello_world.txt Mode: 644 Size: 12
hello world!

Frequently Asked Questions

Why pathlib and not filepath?

filepath is a package that is tightly coupled to the OS filesystem APIs and also is not written in an object-oriented way. pathlib uses afero under the hood for its abstracted filesystem interface, which allows you to represent a vast array of different filesystems (e.g. SFTP, HTTP, in-memory, and of course OS filesystems) using the same Path object.

Why not use afero directly?

You certainly could, however afero does not represent a filesystem object in an object-oriented way. It is only object-oriented with respect to the filesystem itself. pathlib is simply a thin layer on top of afero that provides the filesystem-object-orientation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDoesNotImplement = errors.Errorf("doesn't implement required interface")
)

Functions

This section is empty.

Types

type Path

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

Path is an object that represents a path

func Glob

func Glob(fs afero.Fs, pattern string) ([]*Path, error)

Glob returns all of the path objects matched by the given pattern inside of the afero filesystem.

func NewPath

func NewPath(path string) *Path

NewPath returns a new OS path

func NewPathAfero

func NewPathAfero(path string, fs afero.Fs) *Path

NewPathAfero returns a Path object with the given Afero object

func (*Path) Afero

func (p *Path) Afero() afero.Afero

Afero returns the internal afero object.

func (*Path) Chmod

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

Chmod changes the file mode of the given path

func (*Path) Chtimes

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

Chtimes changes the modification and access time of the given path.

func (*Path) Equals

func (p *Path) Equals(other *Path) (bool, error)

Equals returns whether or not the path pointed to by other has the same resolved filepath as self.

func (*Path) Exists

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

Exists returns whether the path exists

func (*Path) GetLatest

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

GetLatest returns the file or directory that has the most recent mtime. Only works if this path is a directory and it exists. If the directory is empty, the returned Path object will be nil.

func (*Path) Glob

func (p *Path) Glob(pattern string) ([]*Path, error)

Glob returns all matches of pattern relative to this object's path.

func (*Path) IsAbsolute

func (p *Path) IsAbsolute() bool

IsAbsolute returns whether or not the path is an absolute path. This is determined by checking if the path starts with a slash.

func (*Path) IsDir

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

IsDir returns whether the path is a directory

func (*Path) IsFile

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

IsFile returns true if the given path is a file.

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

IsSymlink returns true if the given path is a symlink.

func (*Path) Join

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

Join joins the current object's path with the given elements and returns the resulting Path object.

func (*Path) Mkdir

func (p *Path) Mkdir(perm os.FileMode) error

Mkdir makes the current dir. If the parents don't exist, an error is returned.

func (*Path) MkdirAll

func (p *Path) MkdirAll(perm os.FileMode) error

MkdirAll makes all of the directories up to, and including, the given path.

func (*Path) Mtime added in v0.1.0

func (p *Path) Mtime() (time.Time, error)

Mtime returns the modification time of the given path.

func (*Path) Name

func (p *Path) Name() string

Name returns the string representing the final path component

func (*Path) Parent

func (p *Path) Parent() *Path

Parent returns the Path object of the parent directory

func (*Path) Path

func (p *Path) Path() string

Path returns the string representation of the path

func (*Path) ReadDir

func (p *Path) ReadDir() ([]*Path, error)

ReadDir reads the current path and returns a list of the corresponding Path objects.

func (*Path) ReadFile

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

ReadFile reads the given path and returns the data. If the file doesn't exist or is a directory, an error is returned.

func (*Path) RelativeTo

func (p *Path) RelativeTo(other *Path) (*Path, error)

RelativeTo computes a relative version of path to the other path. For instance, if the object is /path/to/foo.txt and you provide /path/ as the argment, the returned Path object will represent to/foo.txt.

func (*Path) Remove

func (p *Path) Remove() error

Remove deletes/unlinks/destroys the given path

func (*Path) RemoveAll

func (p *Path) RemoveAll() error

RemoveAll removes the given path and all of its children.

func (*Path) Rename

func (p *Path) Rename(target string) (*Path, error)

Rename this path to the given target and return the corresponding Path object.

func (*Path) RenamePath

func (p *Path) RenamePath(target *Path) (*Path, error)

RenamePath is the same as Rename except target is a Path object

func (*Path) Resolve

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

Resolve resolves the path to a real path

func (*Path) Stat

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

Stat returns the os.FileInfo of the given path

func (p *Path) Symlink(target *Path) error

Symlink symlinks to the target location

func (*Path) WriteFile

func (p *Path) WriteFile(data []byte, perm os.FileMode) error

WriteFile writes the given data to the path (if possible). If the file exists, the file is truncated. If the file is a directory, or the path doesn't exist, an error is returned.

Jump to

Keyboard shortcuts

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