pathlib

package module
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: MIT Imports: 9 Imported by: 5

README

Go Report Card license

pathlib

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

Installation

go get -u github.com/smurfless1/pathlib

But why?

I have a large python codebase that I'm porting to golang, and plainly I missed having a familiar interface to filesystem paths.

I saw someone had started a project to provide a similar implementaiton, but my QA feelers went very, very red when I missed tests, interfaces, mocks, etc.


package main

import "github.com/smurfless1/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

Overview

Package pathlib is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockPath

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

MockPath is a mock of Path interface

func NewMockPath

func NewMockPath(ctrl *gomock.Controller) *MockPath

NewMockPath creates a new mock instance

func (*MockPath) Absolute

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

Absolute mocks base method

func (*MockPath) Chmod

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

Chmod mocks base method

func (*MockPath) Cwd

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

Cwd mocks base method

func (*MockPath) EXPECT

func (m *MockPath) EXPECT() *MockPathMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockPath) Exists

func (m *MockPath) Exists() bool

Exists mocks base method

func (*MockPath) ExpandUser

func (m *MockPath) ExpandUser() Path

ExpandUser mocks base method

func (*MockPath) IsAbsolute

func (m *MockPath) IsAbsolute() bool

IsAbsolute mocks base method

func (*MockPath) IsDir

func (m *MockPath) IsDir() bool

IsDir mocks base method

func (*MockPath) IsFile

func (m *MockPath) IsFile() bool

IsFile mocks base method

func (*MockPath) IsRelativeTo added in v0.1.12

func (m *MockPath) IsRelativeTo(value Path) bool

IsRelativeTo mocks base method

func (*MockPath) JoinPath

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

JoinPath mocks base method

func (*MockPath) MkDir

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

MkDir mocks base method

func (*MockPath) Name added in v0.1.9

func (m *MockPath) Name() string

Name mocks base method

func (*MockPath) Open

func (m *MockPath) Open() ([]byte, error)

Open mocks base method

func (*MockPath) Parent

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

Parent mocks base method

func (*MockPath) Parts

func (m *MockPath) Parts() []string

Parts mocks base method

func (*MockPath) RmDir

func (m *MockPath) RmDir() error

RmDir mocks base method

func (*MockPath) Set added in v0.1.6

func (m *MockPath) Set(value string)

Set mocks base method

func (*MockPath) Stem added in v0.1.9

func (m *MockPath) Stem() string

Stem mocks base method

func (*MockPath) String

func (m *MockPath) String() string

String mocks base method

func (*MockPath) Suffix added in v0.1.9

func (m *MockPath) Suffix() string

Suffix mocks base method

func (*MockPath) Touch

func (m *MockPath) Touch() error

Touch mocks base method

func (m *MockPath) Unlink() error

Unlink mocks base method

func (*MockPath) WithSuffix added in v0.1.9

func (m *MockPath) WithSuffix(value string) Path

WithSuffix mocks base method

type MockPathMockRecorder

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

MockPathMockRecorder is the mock recorder for MockPath

func (*MockPathMockRecorder) Absolute

func (mr *MockPathMockRecorder) Absolute() *gomock.Call

Absolute indicates an expected call of Absolute

func (*MockPathMockRecorder) Chmod

func (mr *MockPathMockRecorder) Chmod(mode interface{}) *gomock.Call

Chmod indicates an expected call of Chmod

func (*MockPathMockRecorder) Cwd

func (mr *MockPathMockRecorder) Cwd() *gomock.Call

Cwd indicates an expected call of Cwd

func (*MockPathMockRecorder) Exists

func (mr *MockPathMockRecorder) Exists() *gomock.Call

Exists indicates an expected call of Exists

func (*MockPathMockRecorder) ExpandUser

func (mr *MockPathMockRecorder) ExpandUser() *gomock.Call

ExpandUser indicates an expected call of ExpandUser

func (*MockPathMockRecorder) IsAbsolute

func (mr *MockPathMockRecorder) IsAbsolute() *gomock.Call

IsAbsolute indicates an expected call of IsAbsolute

func (*MockPathMockRecorder) IsDir

func (mr *MockPathMockRecorder) IsDir() *gomock.Call

IsDir indicates an expected call of IsDir

func (*MockPathMockRecorder) IsFile

func (mr *MockPathMockRecorder) IsFile() *gomock.Call

IsFile indicates an expected call of IsFile

func (*MockPathMockRecorder) IsRelativeTo added in v0.1.12

func (mr *MockPathMockRecorder) IsRelativeTo(value interface{}) *gomock.Call

IsRelativeTo indicates an expected call of IsRelativeTo

func (*MockPathMockRecorder) JoinPath

func (mr *MockPathMockRecorder) JoinPath(elem ...interface{}) *gomock.Call

JoinPath indicates an expected call of JoinPath

func (*MockPathMockRecorder) MkDir

func (mr *MockPathMockRecorder) MkDir(mode, parents interface{}) *gomock.Call

MkDir indicates an expected call of MkDir

func (*MockPathMockRecorder) Name added in v0.1.9

func (mr *MockPathMockRecorder) Name() *gomock.Call

Name indicates an expected call of Name

func (*MockPathMockRecorder) Open

func (mr *MockPathMockRecorder) Open() *gomock.Call

Open indicates an expected call of Open

func (*MockPathMockRecorder) Parent

func (mr *MockPathMockRecorder) Parent() *gomock.Call

Parent indicates an expected call of Parent

func (*MockPathMockRecorder) Parts

func (mr *MockPathMockRecorder) Parts() *gomock.Call

Parts indicates an expected call of Parts

func (*MockPathMockRecorder) RmDir

func (mr *MockPathMockRecorder) RmDir() *gomock.Call

RmDir indicates an expected call of RmDir

func (*MockPathMockRecorder) Set added in v0.1.6

func (mr *MockPathMockRecorder) Set(value interface{}) *gomock.Call

Set indicates an expected call of Set

func (*MockPathMockRecorder) Stem added in v0.1.9

func (mr *MockPathMockRecorder) Stem() *gomock.Call

Stem indicates an expected call of Stem

func (*MockPathMockRecorder) String

func (mr *MockPathMockRecorder) String() *gomock.Call

String indicates an expected call of String

func (*MockPathMockRecorder) Suffix added in v0.1.9

func (mr *MockPathMockRecorder) Suffix() *gomock.Call

Suffix indicates an expected call of Suffix

func (*MockPathMockRecorder) Touch

func (mr *MockPathMockRecorder) Touch() *gomock.Call

Touch indicates an expected call of Touch

func (mr *MockPathMockRecorder) Unlink() *gomock.Call

Unlink indicates an expected call of Unlink

func (*MockPathMockRecorder) WithSuffix added in v0.1.9

func (mr *MockPathMockRecorder) WithSuffix(value interface{}) *gomock.Call

WithSuffix indicates an expected call of WithSuffix

type Path

type Path interface {
	// Parts get the list of path components
	Parts() []string
	// Absolute Returns an absolute representation of path.
	Absolute() (Path, error)
	// Cwd Return a new path pointing to the current working directory.
	Cwd() (Path, error)
	// Parent Return a new path for current path parent.
	Parent() (Path, error)
	// Touch Create creates the named file with mode 0666 (before umask), regardless of whether it exists.
	Touch() error
	// RmDir Remove this directory.
	RmDir() error
	// Unlink Remove this file or link.
	Unlink() error
	// MkDir Create a new directory at this given path.
	MkDir(mode os.FileMode, parents bool) (err error)
	// Open Reads the file named by filename and returns the contents.
	Open() ([]byte, error)
	// Chmod changes the mode of the named file to mode.
	Chmod(mode os.FileMode) error
	// JoinPath Returns a new path, Combine current path with one or several arguments
	JoinPath(elem ...string) Path
	// Exists reports current path parent exists.
	Exists() bool
	// IsAbsolute reports whether the path is absolute.
	IsAbsolute() bool
	// IsFile reports whether this path is a regular file.
	IsFile() bool
	// IsDir reports whether this path is a directory.
	IsDir() bool
	// IsLink returns if a path is a symlink.
	// The second return value is the resolved path of the symlink, or the original path
	// The error and bool indicate which value you get back, but the path is usable in either case
	// It CAN point to a file that no longer exists (broken link) so do your checks.
	IsLink() (bool, Path, error)
	// ExpandUser returns a copy of this path with ~ expanded
	ExpandUser() Path
	// String Get the string version of the path
	String() string
	// Set explicitly replaces the current value
	Set(value string)
	// Name returns the final segment of the path, which is the name of the file or directory
	Name() string
	// Stem returns the name without the suffix
	Stem() string
	// Suffix returns the suffix of the name, including the dot
	Suffix() string
	// WithSuffix returns a new path with the suffix replaced
	WithSuffix(value string) Path
	// IsRelativeTo returns whether a path is relative to another path
	IsRelativeTo(value Path) bool
}

Path to a directory or file

func New

func New(path string) Path

New Returns a new path.

type PathImpl

type PathImpl struct {
	Path
	Value string
}

PathImpl is the real implementation of interface Path over os/filepath and fs and so on

func (PathImpl) Absolute

func (p PathImpl) Absolute() (Path, error)

Absolute Returns an absolute representation of path.

func (PathImpl) Chmod

func (p PathImpl) Chmod(mode os.FileMode) error

Chmod changes the mode of the named file to mode.

func (PathImpl) Cwd

func (p PathImpl) Cwd() (Path, error)

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

func (PathImpl) Exists

func (p PathImpl) Exists() bool

Exists reports current path parent exists.

func (PathImpl) ExpandUser

func (p PathImpl) ExpandUser() Path

ExpandUser returns a copy of this path with ~ expanded

func (PathImpl) IsAbsolute

func (p PathImpl) IsAbsolute() bool

IsAbsolute reports whether the path is absolute.

func (PathImpl) IsDir

func (p PathImpl) IsDir() bool

IsDir reports whether this path is a directory.

func (PathImpl) IsFile

func (p PathImpl) IsFile() bool

IsFile reports whether this path is a regular file.

func (p PathImpl) IsLink() (bool, Path, error)

IsLink returns if a path is a symlink. The second return value is the resolved path of the symlink, or the original path If the link is bad, you do NOT get an error, but the target path will be empty The error and bool indicate which value you get back, but the path is usable in either case

func (PathImpl) IsRelativeTo added in v0.1.10

func (p PathImpl) IsRelativeTo(value Path) bool

IsRelativeTo returns whether a path is relative to another path

func (PathImpl) JoinPath

func (p PathImpl) JoinPath(elem ...string) Path

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

func (PathImpl) MkDir

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

MkDir Create a new directory at this given path.

func (PathImpl) Name added in v0.1.9

func (p PathImpl) Name() string

Name returns the final segment of the path, which is the name of the file or directory

func (PathImpl) Open

func (p PathImpl) Open() ([]byte, error)

Open Reads the file named by filename and returns the contents.

func (PathImpl) Parent

func (p PathImpl) Parent() (Path, error)

Parent Return a new path for current path parent.

func (PathImpl) Parts

func (p PathImpl) Parts() []string

Parts get the list of path components

func (PathImpl) RmDir

func (p PathImpl) RmDir() error

RmDir Remove this directory.

func (PathImpl) Set added in v0.1.6

func (p PathImpl) Set(value string)

Set explicitly replaces the current value

func (PathImpl) Stem added in v0.1.9

func (p PathImpl) Stem() string

Stem returns the name without the suffix

func (PathImpl) String

func (p PathImpl) String() string

String conversion

func (PathImpl) Suffix added in v0.1.9

func (p PathImpl) Suffix() string

Suffix returns the suffix of the name, including the dot

func (PathImpl) Touch

func (p PathImpl) Touch() error

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

func (p PathImpl) Unlink() error

Unlink Remove this file or link.

func (PathImpl) WithSuffix added in v0.1.9

func (p PathImpl) WithSuffix(value string) Path

WithSuffix returns a new path with the suffix replaced

Jump to

Keyboard shortcuts

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