path

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 8 Imported by: 33

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidPath = errors.Error("invalid path: %s")
	ErrInvalidName = errors.Error("invalid name: %s")
)

Functions

func Base

func Base(p Path) string

However: Base(p Path) returns the last part, i.e. for directories including the / at the end so joining Dir() and Base() will give the complete relative path

func Clean added in v0.5.0

func Clean(s string) string

func DriveLetter added in v0.17.0

func DriveLetter(a Local) string

DriveLetter returns the drive letter, followed by a : for a windows absolute path and an empty string for non windows paths

func IsAbs

func IsAbs(p Path) bool

func IsDir

func IsDir(p Path) bool

returns true, if the path ends in a slash, is empty or is a dot

func IsValid

func IsValid(name string) bool

func Name

func Name(p Path) string

returns the name (the part after the last slash) if p is a directory, the last slash will not be part of the name that means, that joining Dir() and Name() will not be a directory name, but a filename However: Base(p Path) returns the last part, i.e. for directories including the / at the end so joining Dir() and Base() will give the complete relative path

func NetShare

func NetShare(a Local) string

NetShare returns the network share for a windows UNC absolute path and an empty string otherwise

func ToSystem added in v0.11.0

func ToSystem(p Path) string

ToSystem transforms the given Path into the most common string notation for the running system. (e.g. directories without ending slashes, backslashes instead of slashes as separators etc) For Local paths it returns the absolute path. For Relative path, it returns the relative path. For everything else, it it calling the Relative method to transform it into a relative path and uses this relative paths transformation. The resulting path is a canoncial cleaned path of the running system, without "jumps" (like ./ and ../).

func Validate

func Validate(name string) error

Types

type Absolute

type Absolute interface {
	Path
	Head() string
}

type Local

type Local [2]string

func MustDirFromSystem added in v0.13.0

func MustDirFromSystem(p string) (l Local)

MustDirFromSystem is like ParseDirFromSystem, just that it panics on error

func MustFileFromSystem added in v0.13.0

func MustFileFromSystem(p string) (l Local)

MustFileFromSystem is like ParseFileFromSystem, just that it panics on error

func MustLocal

func MustLocal(abs string) Local

MustLocal calls ParseLocal, panics if the later returns an error and returns the Local path otherwise

func MustWD

func MustWD() Local

WD returns the working directory as an absolute path Local calls ParseLocal, panics if the later returns an error and returns the Absolute path otherwise

func ParseDirFromSystem added in v0.13.0

func ParseDirFromSystem(p string) (l Local, err error)

ParseDirFromSystem parses an existing local path that must be a directory the path might be absolute or relative (than it is relative to the current working directory) the given path might end in a slash or not. the resultig Local will end in a slash if p is an empty string, or a . the working directory is chosen

func ParseFileFromSystem added in v0.13.0

func ParseFileFromSystem(p string) (l Local, err error)

ParseFileFromSystem parses an existing local path that must be a file the path might be absolute or relative (than it is relative to the current working directory) the given path must not end in a slash. if p must not be an empty string, or .

func ParseLocal

func ParseLocal(path string) (a Local, err error)

ParseLocal parses a local fs path. The returned path is a Local, which in turn is an absolute path. If the given path does not have the correct syntax, ParseLocal will return an error. Syntax A path that is a directory must always end in a separator, which is the slash / on unix and might be a slash / or backslash \ on windows systems. path might be one of the following:

  • "", `.`, `./` or `.\`: then the current local working directory is parsed and returned
  • starting with `./` or `.\`: then the rest of the path would be considered to be relative to the local working directory
  • a local windows path that is starting with a drive letter, followed by a colon and optionally a rest path starting with a separator that can be either the slash / or the backslash \, e.g. `c:`, `G:`, `c:/`, `G:/`, `c:\`, `G:\`, `c:/a.txt`, `G:/b.txt`, `C:\a\b\c\`, `g:/a/b/c/`
  • a windows network share in UNC notation (must start with two baslslashes \\) optionally followed by a rest path with the above separator, e.g. `\\host\share\`, `\\host/share/`, `\\host\share\sub\dir\`, `\\host/share/sub/dir/`, `\\host\share\file.txt`
  • an absolute unix path (must start with a slash), e.g. `/`, `/a.txt`, `/a/`, `/a/b/c.txt`

Resulting heads and tails: Since a Local is just a fixed array of two strings, where the first one is the absolute head and the second on is the relative tail to that here are some examples, how the resulting heads and tails would be.

  • "", `.`, `./` or `.\`: head is the absolute path of the working directory, tail is empty
  • starting with `./` or `.\` : head is the absolute path of the working directory, tail is the relative path that is following where backslashes are replaced by slashes, e.g. `.\a\b\c.txt` results in the tail `a/b/c.txt`
  • a local windows path that is starting with a drive letter: head is the drive letter and tail the rest, e.g. `C:\a\b\c\` would result in a head of `c:/` and a tail of `a/b/c/`
  • a windows network share in UNC notation would end up with a head of the host and share and the tail would be the rest, e.g. `\\host\share\sub\dir\a.txt`: would result in the head `\\host\share/` and the tail `sub/dir/a.txt`
  • an absolute unix path would result in the head being the root dir and the tail being the rest, e.g. `/a/b/c.txt` would result in a head of `/` and a tail of `a/b/c.txt`

The plattform specific string notation of a Local (for the compiled plattform) can always be returned via Local.ToSystem()

func ParseWD

func ParseWD() (a Local, err error)

func ToLocal

func ToLocal(abs Absolute) Local

ToLocal creates a Local path out of an absolute path

func (Local) Dir added in v0.3.0

func (p Local) Dir() Local

func (Local) Head

func (a Local) Head() string

func (Local) Join

func (a Local) Join(parts ...string) Local

Join returns a new Absolute that has the same head but a tail that is the join of the old tail and the given parts within the parts, directories must end with a slash /

func (Local) Relative

func (a Local) Relative() Relative

func (Local) Resolve

func (a Local) Resolve(r Relative) Local

Resolve resolves in the context of the path of local

func (Local) String

func (a Local) String() string

type Path

type Path interface {
	String() string
	Relative() Relative
}

type Relative

type Relative string

Relative is a relative path, i.e. a path that does neither start with a slash, nor with a schema.

func (Relative) Depth added in v0.16.0

func (r Relative) Depth() int

Depth returns the depth of the file / dir relative to its starting position, i.e. the number of parent directories. Depth return -1 for the root directory

func (Relative) Dir added in v0.3.0

func (p Relative) Dir() Relative

func (Relative) IsRoot added in v0.5.0

func (r Relative) IsRoot() bool

func (Relative) Join

func (r Relative) Join(parts ...string) Relative

Join returns a new relative path by joining the old one with the given parts. within the parts, directories must end with a slash /

func (Relative) Relative

func (r Relative) Relative() Relative

Relative fullfills the the Path interface and returns the relative part of the path, which is always itself.

func (Relative) String

func (r Relative) String() string

String return the representation of the path as a string.

func (Relative) ToGoPath added in v0.11.0

func (r Relative) ToGoPath() string

ToGoPath only cuts away slashes at the end and by this returns a path as expected inside the io/fs package of the standard library.

type Remote

type Remote struct {
	*url.URL
}

func MustRemote

func MustRemote(u string) *Remote

MustRemote calls ParseRemote, panics if the later returns an error and returns the Remote path otherwise

func ParseRemote

func ParseRemote(u string) (*Remote, error)

ParseRemote parses remote fs paths, i.e. urls (they are always absolute). the given url should not have querys, nor fragments

func ToRemote

func ToRemote(abs Absolute) (*Remote, error)

ToRemote creates a Remote path out of an absolute path

func (*Remote) Dir added in v0.3.0

func (p *Remote) Dir() *Remote

func (*Remote) Head

func (u *Remote) Head() string

func (*Remote) Join

func (u *Remote) Join(parts ...string) *Remote

Join joins based on the relative path

func (*Remote) Relative

func (u *Remote) Relative() Relative

func (*Remote) Resolve

func (u *Remote) Resolve(r Relative) *Remote

Resolve resolves in the context of Head()

Jump to

Keyboard shortcuts

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