pathlib

package module
v0.0.2 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 including pre-releases) GitHub go.mod Go version Codecov

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)

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

func (*Path) Chmod

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

func (*Path) Chtimes

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

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)
func (p *Path) IsSymlink() (bool, error)

func (*Path) Join

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

func (*Path) Mkdir

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

func (*Path) MkdirAll

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

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() ([]os.FileInfo, error)

func (*Path) ReadFile

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

func (*Path) RelativeTo

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

func (*Path) Remove

func (p *Path) Remove() error

func (*Path) RemoveAll

func (p *Path) RemoveAll() error

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)
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

Jump to

Keyboard shortcuts

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