Documentation
¶
Overview ¶
Types that expose filesystem/path functionality as methods.
Example ¶
package main import ( "fmt" "github.com/skalt/pathlib.go" ) // Syntactic sugar. func enforce(err error) { if err != nil { panic(err) } } // More syntactic sugar. func expect[T any](val T, err error) T { enforce(err) return val } func main() { dir := expect(pathlib.TempDir().Join("pathlib-example").AsDir().Make(0o777)) defer func() { expect(dir.RemoveAll()) }() onDisk := expect(dir.Stat()) fmt.Printf("Created %s with mode %s\n", dir, onDisk.Mode()) for i, subPath := range []string{"a.txt", "b.txt", "c/d.txt"} { file := dir.Join(subPath).AsFile() handle := expect(file.MakeAll(0o666, 0o777)) expect(fmt.Fprintf(handle, "%d", i)) enforce(handle.Close()) fmt.Printf("contents of %s: %q\n", file, string(expect(file.Read()))) } fmt.Printf("contents of %s:\n", dir) for _, entry := range expect(dir.Read()) { fmt.Println(" - " + entry.Name()) } }
Output: Created /tmp/pathlib-example with mode drwxr-xr-x contents of /tmp/pathlib-example/a.txt: "0" contents of /tmp/pathlib-example/b.txt: "1" contents of /tmp/pathlib-example/c/d.txt: "2" contents of /tmp/pathlib-example: - a.txt - b.txt - c
Index ¶
- type Beholder
- type Changer
- type Dir
- func (d Dir) Abs() (Dir, error)
- func (d Dir) BaseName() string
- func (d Dir) Chdir() (Dir, error)
- func (d Dir) Chmod(mode os.FileMode) error
- func (d Dir) Chown(uid int, gid int) error
- func (d Dir) Clean() Dir
- func (d Dir) Eq(other Dir) (equivalent bool)
- func (d Dir) Exists() bool
- func (d Dir) ExpandUser() (Dir, error)
- func (d Dir) Ext() string
- func (d Dir) Glob(pattern string) ([]PathStr, error)
- func (d Dir) IsAbsolute() bool
- func (d Dir) IsLocal() bool
- func (d Dir) Join(parts ...string) PathStr
- func (d Dir) Localize() (Dir, error)
- func (d Dir) Lstat() (result Info[Dir], err error)
- func (d Dir) Make(perm fs.FileMode) (result Dir, err error)
- func (d Dir) MakeAll(perm, parentPerm fs.FileMode) (result Dir, err error)
- func (d Dir) Parent() Dir
- func (d Dir) Parts() []string
- func (d Dir) Read() ([]fs.DirEntry, error)
- func (d Dir) Rel(base Dir) (Dir, error)
- func (d Dir) Remove() error
- func (d Dir) RemoveAll() (Dir, error)
- func (d Dir) Rename(newPath PathStr) (Dir, error)
- func (d Dir) Stat() (result Info[Dir], err error)
- func (d Dir) String() string
- func (d Dir) Walk(callback func(path PathStr, d fs.DirEntry, err error) error) error
- type File
- func (f File) Abs() (File, error)
- func (f File) BaseName() string
- func (f File) Chmod(mode os.FileMode) error
- func (f File) Chown(uid int, gid int) error
- func (f File) Clean() File
- func (f File) Eq(other File) bool
- func (f File) Exists() bool
- func (f File) ExpandUser() (File, error)
- func (f File) Ext() string
- func (f File) IsAbsolute() bool
- func (f File) IsLocal() bool
- func (f File) Join(parts ...string) PathStr
- func (f File) Localize() (File, error)
- func (f File) Lstat() (info Info[File], err error)
- func (f File) Make(perm fs.FileMode) (FileHandle, error)
- func (f File) MakeAll(perm, parentPerm fs.FileMode) (result FileHandle, err error)
- func (f File) Open(flag int, perm fs.FileMode) (FileHandle, error)
- func (f File) Parent() Dir
- func (f File) Parts() []string
- func (f File) Read() ([]byte, error)
- func (f File) Rel(base Dir) (File, error)
- func (f File) Remove() error
- func (f File) Rename(newPath PathStr) (File, error)
- func (f File) Stat() (Info[File], error)
- func (f File) String() string
- type FileHandle
- type Info
- type Kind
- type Maker
- type PathStr
- func (p PathStr) Abs() (PathStr, error)
- func (p PathStr) Ancestors() iter.Seq[Dir]
- func (p PathStr) AsDir() Dir
- func (p PathStr) AsFile() File
- func (p PathStr) AsSymlink() Symlink
- func (p PathStr) BaseName() string
- func (p PathStr) Chmod(mode os.FileMode) error
- func (p PathStr) Chown(uid int, gid int) error
- func (p PathStr) Clean() PathStr
- func (p PathStr) Eq(q PathStr) bool
- func (p PathStr) Exists() bool
- func (p PathStr) ExpandUser() (PathStr, error)
- func (p PathStr) Ext() string
- func (p PathStr) IsAbsolute() bool
- func (p PathStr) IsLocal() bool
- func (p PathStr) Join(segments ...string) PathStr
- func (p PathStr) Localize() (PathStr, error)
- func (p PathStr) Lstat() (Info[PathStr], error)
- func (p PathStr) Parent() Dir
- func (p PathStr) Parts() (parts []string)
- func (p PathStr) Read() (any, error)
- func (p PathStr) Rel(base Dir) (PathStr, error)
- func (p PathStr) Remove() error
- func (p PathStr) Rename(newPath PathStr) (PathStr, error)
- func (p PathStr) Stat() (Info[PathStr], error)
- func (p PathStr) String() string
- type PurePath
- type Readable
- type Remover
- type Symlink
- func (s Symlink) Abs() (Symlink, error)
- func (s Symlink) BaseName() string
- func (s Symlink) Chmod(mode os.FileMode) error
- func (s Symlink) Chown(uid int, gid int) error
- func (s Symlink) Clean() Symlink
- func (s Symlink) Eq(other Symlink) bool
- func (s Symlink) Exists() bool
- func (s Symlink) ExpandUser() (Symlink, error)
- func (s Symlink) Ext() string
- func (s Symlink) IsAbsolute() bool
- func (s Symlink) IsLocal() bool
- func (s Symlink) Join(parts ...string) PathStr
- func (s Symlink) LinkTo(target PathStr) (Symlink, error)
- func (s Symlink) Localize() (Symlink, error)
- func (s Symlink) Lstat() (result Info[Symlink], err error)
- func (s Symlink) Parent() Dir
- func (s Symlink) Parts() []string
- func (s Symlink) Read() (PathStr, error)
- func (s Symlink) Rel(base Dir) (Symlink, error)
- func (s Symlink) Remove() error
- func (s Symlink) Rename(newPath PathStr) (Symlink, error)
- func (s Symlink) Stat() (Info[Symlink], error)
- func (s Symlink) String() string
- type Transformer
- type WrongTypeOnDisk
Examples ¶
- Package
- Dir (PurePath)
- Dir.Abs
- Dir.Eq
- Dir.Glob
- Dir.Join
- Dir.Localize
- Dir.Lstat
- Dir.Walk
- File (PurePath)
- PathStr (Beholder)
- PathStr (Read)
- PathStr.Ancestors (Absolute)
- PathStr.Ancestors (Relative)
- PathStr.BaseName
- PathStr.ExpandUser
- PathStr.Ext
- PathStr.IsAbsolute
- PathStr.IsLocal
- PathStr.Join
- PathStr.Parts
- PathStr.Read
- PathStr.Rel
- PathStr.Remove
- Symlink.Lstat
- UserCacheDir
- UserConfigDir
- UserHomeDir
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Beholder ¶
type Beholder[P Kind] interface { // See [os.Stat]. Stat() (Info[P], error) // See [os.Lstat]. Lstat() (Info[P], error) // Returns true if the path exists on-disk. Exists() bool }
Behaviors for inspecting a path on-disk.
type Changer ¶
type Changer interface { // see [os.Chmod]. Chmod(fs.FileMode) error // see [os.Chown]. Chown(uid, gid int) error }
Methods that alter a filesystem object without changing its path or performing I/O.
type Dir ¶
type Dir PathStr
A string that represents a directory. The directory may or may not exist on-disk, and the string may or may not end in an os.PathSeparator.
Example (PurePath) ¶
d := pathlib.Dir("~/.config/git/..") fmt.Println("On Unix") fmt.Printf("%T(%q)\n", d, d) method := func(name string, val any) { fmt.Printf("\t.%s() => %T(%#v)\n", name, val, val) } method("Parts", d.Parts()) method("Clean", d.Clean()) method("Parent", d.Parent()) method("BaseName", d.BaseName()) method("IsAbsolute", d.IsAbsolute()) method("IsLocal", d.IsLocal()) method("Ext", d.Ext()) // method("Abs().Unwrap", d.Abs())) // method("ExpandUser().Unwrap", d.ExpandUser()))
Output: On Unix pathlib.Dir("~/.config/git/..") .Parts() => []string([]string{"~", ".config", "git", ".."}) .Clean() => pathlib.Dir("~/.config") .Parent() => pathlib.Dir("~") .BaseName() => string("..") .IsAbsolute() => bool(false) .IsLocal() => bool(true) .Ext() => string(".")
func UserCacheDir ¶
See os.UserCacheDir.
Example ¶
stash := os.Getenv("XDG_CACHE_HOME") defer func() { _ = os.Setenv("XDG_CACHE_HOME", stash) }() fmt.Println("On Unix:") { // UserCacheDir() returns $XDG_CACHE_HOME if it's set expected := "/example/.cache" if err := os.Setenv("XDG_CACHE_HOME", expected); err != nil { panic(err) } actual := expect(pathlib.UserCacheDir()) fmt.Printf("$XDG_CACHE_HOME: %q\n", expected) fmt.Printf("UserCacheDir()): %q\n", actual) } { // if $XDG_CACHE_HOME is unset, return the OS-specific default. if err := os.Unsetenv("XDG_CACHE_HOME"); err != nil { panic(err) } home := expect(pathlib.UserHomeDir()) actual := strings.Replace( expect(pathlib.UserCacheDir()).String(), home.String(), "$HOME", 1, ) fmt.Printf("$XDG_CACHE_HOME: %q\n", os.Getenv("XDG_CACHE_HOME")) fmt.Printf("UserCacheDir()): %q\n", actual) }
Output: On Unix: $XDG_CACHE_HOME: "/example/.cache" UserCacheDir()): "/example/.cache" $XDG_CACHE_HOME: "" UserCacheDir()): "$HOME/.cache"
func UserConfigDir ¶
See os.UserConfigDir.
Example ¶
stash := os.Getenv("XDG_CONFIG_HOME") home := expect(pathlib.UserHomeDir()) defer func() { _ = os.Setenv("XDG_CONFIG_HOME", stash) }() checkOutput := func(expected string) { _ = os.Setenv("XDG_CONFIG_HOME", expected) xdg_config_home := os.Getenv("XDG_CONFIG_HOME") val, err := pathlib.UserConfigDir() if err == nil { fmt.Printf( "%q => %q\n", xdg_config_home, strings.Replace(val.String(), home.String(), "$HOME", 1), ) } else { fmt.Printf("%q => Err(%q)\n", xdg_config_home, err.Error()) } } fmt.Println("On Unix") checkOutput("/foo/bar") checkOutput("") checkOutput("./my_config")
Output: On Unix "/foo/bar" => "/foo/bar" "" => "$HOME/.config" "./my_config" => Err("path in $XDG_CONFIG_HOME is relative")
func UserHomeDir ¶
See os.UserHomeDir.
Example ¶
homeDir := expect(pathlib.UserHomeDir()) fmt.Println(os.Getenv("HOME") == string(homeDir))
Output: true
func (Dir) Abs ¶
Returns an absolute path, or an error if the path cannot be made absolute. Note that there may be more than one absolute path for a given input path.
See path/filepath.Abs.
Abs implements Transformer.
Example ¶
cwd := expect(pathlib.Cwd()) roundTrip := func(d pathlib.Dir) { abs := expect(d.Abs()) rel := expect(abs.Rel(cwd)) if rel.Clean() != d.Clean() { fmt.Printf("dir=%q\nabs=%q\nrel=%q\n\n", d, abs, rel) } else { fmt.Println("true") } } roundTrip("foo/bar") roundTrip("./bar")
Output: true true
func (Dir) Clean ¶
Remove ".", "..", and repeated slashes from a path.
See path/filepath.Clean.
Clean implements Transformer.
func (Dir) Eq ¶
Returns true if the two paths represent the same path.
Eq implements Transformer.
Example ¶
cwd := expect(pathlib.Cwd()) fmt.Println(cwd.Eq(".")) fmt.Println(pathlib.Dir("/foo").Eq("/foo")) fmt.Println(cwd.Join("./relative").Eq("relative")) fmt.Println(pathlib.Dir("/a/b").Eq("b"))
Output: true true true false
func (Dir) Exists ¶
Returns true if the path exists on-disk after following symlinks.
See os.Stat, fs.ErrNotExist.
Exists implements Beholder.
func (Dir) Glob ¶
See path/filepath.Glob.
Example ¶
demoDir := pathlib. TempDir(). Join("glob-example"). AsDir() demoDir = expect(demoDir.MakeAll(0o777, 0o777)) defer func() { _, _ = demoDir.RemoveAll() }() for _, name := range []string{"x", "y", "z", "a", "b", "c"} { expect(demoDir.Join(name + ".txt").AsFile().Make(0o777)) } for _, match := range expect(demoDir.Glob("*.txt")) { fmt.Println(match) }
Output: /tmp/glob-example/a.txt /tmp/glob-example/b.txt /tmp/glob-example/c.txt /tmp/glob-example/x.txt /tmp/glob-example/y.txt /tmp/glob-example/z.txt
func (Dir) IsAbsolute ¶
Returns true if the path is absolute, false otherwise. See filepath.IsAbs for more details.
IsAbsolute implements PurePath.
func (Dir) Join ¶
Join implements PurePath.
Example ¶
result := pathlib.Dir("/tmp").Join("a/b") fmt.Printf("%s is a %T", result, result)
Output: /tmp/a/b is a pathlib.PathStr
func (Dir) Localize ¶
Localize implements Transformer.
Example ¶
fmt.Println("On Unix") fmt.Println("Localized", expect(pathlib.Dir("foo/bar/baz").Localize()))
Output: On Unix Localized foo/bar/baz
func (Dir) Lstat ¶
See os.Lstat.
Lstat implements Beholder.
Example ¶
tmpDir := expect(pathlib.TempDir(). Join("dir-lstat-example"). AsDir(). MakeAll(0o777, 0o777)) defer func() { _, _ = tmpDir.RemoveAll() }() nonExistent := tmpDir.Join("dir").AsDir() _, err := nonExistent.Lstat() if errors.Is(err, fs.ErrNotExist) { fmt.Printf("%T(%q).Lstat() => fs.ErrNotExist\n", nonExistent, nonExistent) } dir := expect(nonExistent.Make(0o777)) file := tmpDir.Join("file.txt").AsFile() expect(file.Make(0o755)) _, err = pathlib.Dir(file.String()).Lstat() if e, ok := err.(pathlib.WrongTypeOnDisk[pathlib.Dir]); ok { fmt.Println(e.Error()) } link := expect(tmpDir.Join("link").AsSymlink().LinkTo(pathlib.PathStr(dir))) disguised := pathlib.Dir(link) unmasked := expect(disguised.Stat()) // works for _, name := range []string{"a", "b", "c"} { expect(dir.Join(name).AsFile().Make(0666)) } fmt.Println(unmasked.Path()) for _, entry := range expect(unmasked.Path().Read()) { fmt.Println(entry) }
Output: pathlib.Dir("/tmp/dir-lstat-example/dir").Lstat() => fs.ErrNotExist pathlib.Dir("/tmp/dir-lstat-example/file.txt") unexpectedly has mode -rwxr-xr-x on-disk /tmp/dir-lstat-example/link - a - b - c
func (Dir) Rel ¶
Returns a relative path to the target directory, or an error if the path cannot be made relative.
See path/filepath.Rel.
Rel implements Transformer.
func (Dir) Stat ¶
Observe the directory's filesystem Info on-disk. If the info is not a directory or a symlink, Stat will return a WrongTypeOnDisk error.
See os.Stat.
Stat implements Beholder.
func (Dir) String ¶
Convenience method to cast get the untyped string representation of the path.
String implements Transformer.
func (Dir) Walk ¶
Example ¶
dir := expect(pathlib.TempDir().Join("dir-walk-example").AsDir().Make(0755)) defer func() { _, _ = dir.RemoveAll() }() _ = expect(dir.Join("foo/bar/baz").AsDir().MakeAll(0755, 0755)) _ = expect(dir.Join("a/b/c").AsDir().MakeAll(0755, 0755)) err := dir.Walk(func(path pathlib.PathStr, d fs.DirEntry, err error) error { fmt.Println(expect(path.Rel(dir))) return nil }) if err != nil { panic(err) }
Output: . a a/b a/b/c foo foo/bar foo/bar/baz
type File ¶
type File PathStr
A path that represents a file.
Example (PurePath) ¶
f := pathlib.File("~/.config/tool/../other-tool/config.toml") fmt.Println("On Unix") fmt.Printf("%T(%q)\n", f, f) method := func(name string, val any) { fmt.Printf("\t.%s() => %T(%#v)\n", name, val, val) } method("Parts", f.Parts()) method("Clean", f.Clean()) method("Parent", f.Parent()) method("BaseName", f.BaseName()) method("IsAbsolute", f.IsAbsolute()) method("IsLocal", f.IsLocal()) method("Ext", f.Ext()) // method("Abs().Unwrap", d.Abs())) // method("ExpandUser().Unwrap", d.ExpandUser()))
Output: On Unix pathlib.File("~/.config/tool/../other-tool/config.toml") .Parts() => []string([]string{"~", ".config", "tool", "..", "other-tool", "config.toml"}) .Clean() => pathlib.File("~/.config/other-tool/config.toml") .Parent() => pathlib.Dir("~/.config/other-tool") .BaseName() => string("config.toml") .IsAbsolute() => bool(false) .IsLocal() => bool(true) .Ext() => string(".toml")
func (File) Abs ¶
Returns an absolute path, or an error if the path cannot be made absolute. Note that there may be more than one absolute path for a given input path.
See path/filepath.Abs.
Abs implements Transformer.
func (File) Clean ¶
Remove ".", "..", and repeated slashes from a path.
See path/filepath.Clean.
Clean implements Transformer.
func (File) Exists ¶
Returns true if the path exists on-disk after following symlinks.
See os.Stat, fs.ErrNotExist.
Exists implements Beholder.
func (File) ExpandUser ¶
ExpandUser implements Transformer.
func (File) Lstat ¶
Observe the file info of the path on-disk. Does not follow symlinks. If the observed info is not a file or a symlink, Lstat returns a WrongTypeOnDisk error.
See os.Lstat.
OnDisk implements Beholder
func (File) Make ¶
func (f File) Make(perm fs.FileMode) (FileHandle, error)
Create the file if it doesn't exist. If it does, do nothing.
See os.Open, os.O_CREATE.
Make implements Maker.
func (File) MakeAll ¶
func (f File) MakeAll(perm, parentPerm fs.FileMode) (result FileHandle, err error)
Create the file and any missing parents. If the file exists, do nothing.
See os.Open, os.O_CREATE.
Make implements Maker.
func (File) Open ¶
See os.OpenFile.
func (File) Rel ¶
Returns a relative path to the target directory, or an error if the path cannot be made relative.
See path/filepath.Rel.
Rel implements Transformer.
func (File) Stat ¶
Observe the file info of the path on-disk. Follows symlinks. If the observed info is not a file Stat returns a WrongTypeOnDisk error.
See os.Stat.
Stat implements Beholder.
func (File) String ¶
Convenience method to cast get the untyped string representation of the path.
String implements Transformer.
type FileHandle ¶
type FileHandle interface { Path() File PurePath Beholder[File] Transformer[File] Changer Remover[File] // from *os.File Name() string Truncate(size int64) error SyscallConn() (syscall.RawConn, error) SetDeadline(deadline time.Time) error SetReadDeadline(deadline time.Time) error SetWriteDeadline(deadline time.Time) error Fd() uintptr io.Closer io.Seeker io.Reader io.Writer io.StringWriter }
An open file descriptor. Unlike an os.File, it can only represent a logical file (as in a document on-disk), never a directory.
type Info ¶
type Info[P Kind] interface { fs.FileInfo PurePath Transformer[P] Changer Remover[P] // the typed version of [fs.FileInfo.Name] Path() P }
An observation of a path on-disk, including a constant observation timestamp.
type PathStr ¶
type PathStr string
Example (Beholder) ¶
temp := expect(pathlib.TempDir().Join("path-str-beholder").AsDir().Make(0777)) defer func() { _, _ = temp.RemoveAll() }() file := temp.Join("file.txt") expect(file.AsFile().Make(0644)) rel := expect(file.Rel(temp)) fmt.Printf("OnDisk: %q %s\n", rel, expect(file.Stat()).Mode()) fmt.Printf(" Lstat: %q %s\n", rel, expect(file.Lstat()).Mode()) fmt.Printf(" Stat: %q %s\n", rel, expect(file.Stat()).Mode())
Output: OnDisk: "file.txt" -rw-r--r-- Lstat: "file.txt" -rw-r--r-- Stat: "file.txt" -rw-r--r--
Example (Read) ¶
tmpDir := expect(pathlib.TempDir().Join("example-pathStr-read").AsDir().Make(0777)) example := tmpDir.Join("example") { expect(example.AsDir().Make(0777)) expect(example.Join("foo").AsFile().Make(0644)) expect(example.Join("bar").AsDir().Make(0777)) entries := expect(example.Read()).([]fs.DirEntry) for entry := range entries { fmt.Println(entry) } enforce(example.Remove()) } { _, err := expect(example.AsFile().Make(0644)).WriteString("text") if err != nil { panic(err) } fmt.Println(expect(example.Read())) enforce(example.Remove()) } { target := tmpDir.Join("target") if _, err := expect(target.AsFile().Make(0644)).WriteString("target"); err != nil { panic(err) } expect(example.AsSymlink().LinkTo(target)) fmt.Println(expect(example.Read())) }
func (PathStr) Abs ¶
Returns an absolute path, or an error if the path cannot be made absolute. Note that there may be more than one absolute path for a given input path.
See path/filepath.Abs.
Abs implements Transformer.
func (PathStr) Ancestors ¶
experimental
Example (Absolute) ¶
for d := range pathlib.PathStr("/foo/bar/baz").Ancestors() { fmt.Println(d) }
Output: /foo/bar /foo /
Example (Relative) ¶
for d := range pathlib.PathStr("./foo/bar/baz").Ancestors() { fmt.Println(d) } // this is the same as: for d := range pathlib.PathStr("foo/bar/baz").Ancestors() { fmt.Println(d) }
Output: foo/bar foo . foo/bar foo .
func (PathStr) BaseName ¶
A wrapper around path/filepath.Base.
BaseName implements PurePath.
Example ¶
example := func(p pathlib.PathStr) { fmt.Printf("%q => %q\n", p, p.BaseName()) } fmt.Println("On Unix:") example("/foo/bar/baz.js") example("/foo/bar/baz") example("/foo/bar/baz/") example("dev.txt") example("../todo.txt") example("..") example(".") example("/") example("")
Output: On Unix: "/foo/bar/baz.js" => "baz.js" "/foo/bar/baz" => "baz" "/foo/bar/baz/" => "baz" "dev.txt" => "dev.txt" "../todo.txt" => "todo.txt" ".." => ".." "." => "." "/" => "/" "" => "."
func (PathStr) Clean ¶
Remove ".", "..", and repeated slashes from a path.
See path/filepath.Clean.
Clean implements Transformer.
func (PathStr) Eq ¶
----------------------------------------------------------------------------- Returns true if the two paths represent the same path.
Eq implements Transformer.
func (PathStr) ExpandUser ¶
Expand a leading "~" into the user's home directory. If the home directory cannot be determined, the path is returned unchanged.
Example ¶
home := expect(pathlib.UserHomeDir()) example := func(p pathlib.PathStr) { expanded := expect(p.ExpandUser()) fmt.Printf( "%q => %q\n", p, strings.Replace(string(expanded), string(home), "$HOME", 1), ) } fmt.Println("On Unix:") example("~") example("~/foo/bar.txt") example("foo/~/bar")
Output: On Unix: "~" => "$HOME" "~/foo/bar.txt" => "$HOME/foo/bar.txt" "foo/~/bar" => "foo/~/bar"
func (PathStr) Ext ¶
A wrapper around path/filepath.Ext.
Ext implements PurePath
Example ¶
example := func(p pathlib.PathStr) { fmt.Printf("%q => %q\n", p, p.Ext()) } example("index") example("index.js") example("main.test.js")
Output: "index" => "" "index.js" => ".js" "main.test.js" => ".js"
func (PathStr) IsAbsolute ¶
Returns true if the path is absolute, false otherwise. See path/filepath.IsAbs for more details.
IsAbsolute implements PurePath.
Example ¶
fmt.Println("On Unix:") fmt.Println(pathlib.PathStr("/home/gopher").IsAbsolute()) fmt.Println(pathlib.PathStr(".bashrc").IsAbsolute()) fmt.Println(pathlib.PathStr("..").IsAbsolute()) fmt.Println(pathlib.PathStr(".").IsAbsolute()) fmt.Println(pathlib.PathStr("/").IsAbsolute()) fmt.Println(pathlib.PathStr("").IsAbsolute())
Output: On Unix: true false false false true false
func (PathStr) IsLocal ¶
returns true if the path is local/relative, false otherwise. see path/filepath.IsLocal for more details.
IsLocal implements PurePath.
Example ¶
p := pathlib.PathStr("/absolute/path/to/file.txt") fmt.Println(p.IsLocal()) // false p = pathlib.PathStr("relative/path/to/file.txt") fmt.Println(p.IsLocal()) // true p = pathlib.PathStr("./local/path/to/file.txt") fmt.Println(p.IsLocal()) // true
Output: false true true
func (PathStr) Join ¶
A wrapper around path/filepath.Join.
Example ¶
fmt.Println("On Unix:") example := func(p pathlib.PathStr, segments ...string) { q := p.Join(segments...) x := fmt.Sprintf("%q", segments) fmt.Printf("%T(%q).Join(%s) => %q\n", p, p, x[1:len(x)-1], q) } example(pathlib.PathStr("a"), "b", "c") example(pathlib.PathStr("a"), "b/c") example(pathlib.PathStr("a/b"), "c") example(pathlib.PathStr("a/b"), "/c") example(pathlib.PathStr("a/b"), "../../../xyz")
Output: On Unix: pathlib.PathStr("a").Join("b" "c") => "a/b/c" pathlib.PathStr("a").Join("b/c") => "a/b/c" pathlib.PathStr("a/b").Join("c") => "a/b/c" pathlib.PathStr("a/b").Join("/c") => "a/b/c" pathlib.PathStr("a/b").Join("../../../xyz") => "../xyz"
func (PathStr) Localize ¶
See path/filepath.Localize. Localize implements Transformer.
func (PathStr) Lstat ¶
Observe the file info of the path on-disk. Note that this does not follow symlinks.
see os.Lstat.
Lstat implements Beholder.
func (PathStr) Parts ¶
Splits the path at every character that's a path separator. Omits empty segments. See os.IsPathSeparator.
Example ¶
example := func(p pathlib.PathStr) { fmt.Printf("%q => %#v\n", p, p.Parts()) } fmt.Println("On Unix:") example("/a/b") example("./a/b") example("a/b") example("a/../b") example("a//b") example("")
Output: On Unix: "/a/b" => []string{"/", "a", "b"} "./a/b" => []string{".", "a", "b"} "a/b" => []string{"a", "b"} "a/../b" => []string{"a", "..", "b"} "a//b" => []string{"a", "b"} "" => []string(nil)
func (PathStr) Read ¶
Read attempts to read what the path represents. See File.Read, Dir.Read, and Symlink.Read for the possible return types.
Read implements Readable.
Example ¶
var dir = pathlib.TempDir().Join("PathStr.Read") var file = dir.Join("a.txt") var link = dir.Join("link") _, err := expect(file.AsFile().MakeAll(0666, 0777)).WriteString("file text") if err != nil { panic(err) } defer func() { expect(dir.AsDir().RemoveAll()) }() expect(link.AsSymlink().LinkTo(file)) var contents any contents = expect(pathlib.PathStr(file).Read()) fmt.Printf("file.Read() => %T(%q)\n", contents, contents) contents = expect(pathlib.PathStr(dir).Read()) fmt.Printf("dir.Read() => %T(%q)\n", contents, contents) contents = expect(pathlib.PathStr(link).Read()) fmt.Printf("link.Read() => %T(%q)", contents, contents)
Output: file.Read() => []uint8("file text") dir.Read() => []fs.DirEntry(["- a.txt" "L link"]) link.Read() => pathlib.PathStr("/tmp/PathStr.Read/a.txt")
func (PathStr) Rel ¶
Returns a relative path to the target directory, or an error if the path cannot be made relative.
See path/filepath.Rel.
Rel implements Transformer
Example ¶
example := func(a pathlib.PathStr, b pathlib.Dir) { val, err := a.Rel(b) if err == nil { fmt.Printf("%T(%q).Rel(%q) => %T(%q)\n", a, a, b, val, val) } else { fmt.Printf("%T(%q).Rel(%q) => Err(%v)\n", a, a, b, err) } } fmt.Println("On Unix") example("/a/b/c", "/a") example("/b/c", "/a") example("./b/c", "/a")
Output: On Unix pathlib.PathStr("/a/b/c").Rel("/a") => pathlib.PathStr("b/c") pathlib.PathStr("/b/c").Rel("/a") => pathlib.PathStr("../b/c") pathlib.PathStr("./b/c").Rel("/a") => Err(Rel: can't make ./b/c relative to /a)
func (PathStr) Remove ¶
See os.Remove.
Remove implements Remover.
Example ¶
dir := expect(pathlib.TempDir().Join("pathStr-remove").AsDir().Make(0777)) defer func() { expect(dir.RemoveAll()) }() f := dir.Join("foo.txt") expect(f.AsFile().Make(0666)) link := dir.Join("link") expect(link.AsSymlink().LinkTo(f)) err := pathlib.PathStr(dir).Remove() if err != nil { fmt.Println("Unable to delete non-empty directory " + dir) } enforce(link.Remove()) if !link.Exists() { fmt.Println("removed symlink " + link) } if f.Exists() { fmt.Println("removing " + link + " didn't affect link target " + f) } else { panic("removing " + link + " removed " + f) } enforce(f.Remove()) if !f.Exists() { fmt.Println("removed file " + f) }
Output: Unable to delete non-empty directory /tmp/pathStr-remove removed symlink /tmp/pathStr-remove/link removing /tmp/pathStr-remove/link didn't affect link target /tmp/pathStr-remove/foo.txt removed file /tmp/pathStr-remove/foo.txt
func (PathStr) String ¶
Convenience method to cast get the untyped string representation of the path.
String implements Transformer.
type PurePath ¶
type PurePath interface { // See [path/filepath.Join]. Join(segments ...string) PathStr // Return the parent directory. Should have the same properties as [path/filepath.Dir]. Parent() Dir // See [path/filepath.Base]. BaseName() string // See [path/filepath.Ext]. Ext() string // Split the path into multiple non-empty segments. Parts() []string // Returns true if the path is absolute. See [path/filepath.IsAbs]. IsAbsolute() bool // Returns true if the path is local/relative. See [path/filepath.IsLocal]. IsLocal() bool }
String-only infallible path operations that do not require filesystem access or syscalls.
type Remover ¶
type Remover[P Kind] interface { // see [os.Remove]. Remove() error // see [os.Rename]. Rename(newPath PathStr) (P, error) }
Behaviors that cause something at a path to no longer be there.
type Symlink ¶
type Symlink PathStr
func (Symlink) Abs ¶
Returns an absolute path, or an error if the path cannot be made absolute. Note that there may be more than one absolute path for a given input path.
See path/filepath.Abs.
Abs implements Transformer.
func (Symlink) Clean ¶
Remove ".", "..", and repeated slashes from a path.
See path/filepath.Clean.
Clean implements Transformer
func (Symlink) Eq ¶
Returns true if the two paths represent the same path. This does not take into account any links.
Eq implements Transformer.
func (Symlink) Exists ¶
Returns true if the path exists on-disk WITHOUT following symlinks.
See os.Stat, fs.ErrNotExist.
Exists implements Beholder.
func (Symlink) ExpandUser ¶
ExpandUser implements Transformer
func (Symlink) Localize ¶
Localize implements Transformer.
func (Symlink) Lstat ¶
Looks up the symlink's info on-disk. Note that this returns information about the symlink itself, not its target. If the file info's mode not match fs.ModeSymlink, Lstat returns a WrongTypeOnDisk error.
See os.Lstat.
Lstat implements Beholder.
Example ¶
tempDir := expect(pathlib.Cwd()). Join("temp", "symlink-lstat"). AsDir() expect(tempDir.MakeAll(0o755, 0o755)) defer func() { expect(tempDir.RemoveAll()) }() file := tempDir.Join("file.txt").AsFile() expect(file.Make(0o666)) { link := tempDir.Join("link") if link.String() != tempDir.String()+"/link" { panic(link) } } link := expect(tempDir.Join("link"). AsSymlink(). LinkTo(pathlib.PathStr(file.String()))) onDisk := expect(link.Lstat()) fmt.Printf( "%s -> %s", expect(onDisk.Path().Rel(tempDir)), expect(expect(link.Read()).Rel(tempDir)), )
Output: link -> file.txt
func (Symlink) Read ¶
Returns the target of the symlink. Note that the target may not exist.
See os.Readlink.
Read implements Readable.
func (Symlink) Rel ¶
Returns a relative path to the target directory, or an error if the path cannot be made relative.
See path/filepath.Rel.
Rel implements Transformer.
func (Symlink) Remove ¶
Remove the link without affecting the link target.
See os.Remove.
Remove implements Remover.
func (Symlink) Rename ¶
Rename the link without affecting the link target.
see os.Rename.
Rename implements Remover.
func (Symlink) Stat ¶
Since Stat follows symlinks, it doesn't perform any validation of returned Info's file mode.
See os.Stat.
Stat implements Beholder.
func (Symlink) String ¶
Convenience method to cast get the untyped string representation of the path.
String implements Transformer.
type Transformer ¶
type Transformer[P Kind] interface { // Returns an absolute path, or an error if the path cannot be made absolute. Note that there may be more than one // absolute path for a given input path. // // See [path/filepath.Abs]. Abs() (P, error) // Returns a relative path to the target directory, or an error if the path cannot be made relative. // // See [path/filepath.Rel]. Rel(target Dir) (P, error) // See [path/filepath.Localize]. Localize() (P, error) // Expand `~` into the home directory of the current user. ExpandUser() (P, error) // Remove ".", "..", and repeated slashes from a path. // // See [path/filepath.Clean]. Clean() P // Returns true if the two paths represent the same path. Eq(other P) bool // Convenience method to cast get the untyped string representation of the path. String() string }
transforms the appearance of a path, but not what it represents.
type WrongTypeOnDisk ¶
func (WrongTypeOnDisk[P]) Error ¶
func (w WrongTypeOnDisk[P]) Error() string