Back to godoc.org

Package normalpath

v0.20.5
Latest Go to latest

The latest major version is .

Published: Jul 24, 2020 | License: Apache-2.0 | Module: github.com/bufbuild/buf

Overview

Package normalpath provides functions similar to filepath.

A normalized path is a cleaned and to-slash'ed path. A validated path validates that a path is relative and does not jump context.

Index

func Base

func Base(path string) string

Base is equivalent to filepath.Base.

Normalizes before returning.

func ByDir

func ByDir(paths ...string) map[string][]string

ByDir maps the paths into a map from directory via Dir to the original paths.

The paths for each value slice will be sorted.

The path is expected to be normalized.

func Components

func Components(path string) []string

Components splits the path into it's components.

This calls filepath.Split repeatedly.

The path is expected to be normalized.

func ContainsPath

func ContainsPath(dirPath string, path string, pathType PathType) bool

ContainsPath returns true if the dirPath contains the path.

The path and value are expected to be normalized and validated if Relative is used. The path and value are expected to be normalized and absolute if Absolute is used.

For a given dirPath:

- If path == PathType, dirPath does not contain the path.
- If dirPath == PathType, the dirPath contains the path.
- If dirPath is a directory that contains path, this returns true.

func Dir

func Dir(path string) string

Dir is equivalent to filepath.Dir.

Normalizes before returning.

func EqualsOrContainsPath

func EqualsOrContainsPath(value string, path string, pathType PathType) bool

EqualsOrContainsPath returns true if the value is equal to or contains the path.

The path and value are expected to be normalized and validated if Relative is used. The path and value are expected to be normalized and absolute if Absolute is used.

For a given value:

- If value == PathType, the value contains the path.
- If value == path, the value is equal to the path.
- If value is a directory that contains path, this returns true.

func ErrorEquals

func ErrorEquals(err error, target error) bool

ErrorEquals returns true if err is an Error and err.Error == target.

func Ext

func Ext(path string) string

Ext is equivalent to filepath.Ext.

Can return empty string.

func Join

func Join(paths ...string) string

Join is equivalent to filepath.Join.

Empty strings are ignored, Can return empty string.

Normalizes before returning otherwise.

func MapAllEqualOrContainingPaths

func MapAllEqualOrContainingPaths(m map[string]struct{}, path string, pathType PathType) []string

MapAllEqualOrContainingPaths returns the matching paths in the map.

The path and all keys in m are expected to be normalized and validated.

For a given key x:

- If x == PathType, the path always matches.
- If x == path, the path matches.
- If x is a directory that contains path, the path matches.

If the map is empty, returns nil.

func MapHasEqualOrContainingPath

func MapHasEqualOrContainingPath(m map[string]struct{}, path string, pathType PathType) bool

MapHasEqualOrContainingPath returns true if the path matches any file or directory in the map.

The path and value are expected to be normalized and validated if Relative is used. The path and value are expected to be normalized and absolute if Absolute is used.

For a given key x:

- If x == PathType, the path always matches.
- If x == path, the path matches.
- If x is a directory that contains path, the path matches.

If the map is empty, returns false.

func Normalize

func Normalize(path string) string

Normalize normalizes the given path.

This calls filepath.Clean and filepath.ToSlash on the path. If the path is "" or ".", this returns ".".

func NormalizeAndAbsolute

func NormalizeAndAbsolute(path string) (string, error)

NormalizeAndAbsolute normalizes the path and makes it absolute.

func NormalizeAndTransformForPathType

func NormalizeAndTransformForPathType(path string, pathType PathType) (string, error)

NormalizeAndTransformForPathType calls NormalizeAndValidate for relative paths, and NormalizeAndAbsolute for absolute paths.

func NormalizeAndValidate

func NormalizeAndValidate(path string) (string, error)

NormalizeAndValidate normalizes and validates the given path.

This calls Normalize on the path. Returns Error if the path is not relative or jumps context. This can be used to validate that paths are valid to use with Buckets. The error message is safe to pass to users.

func Rel

func Rel(basepath string, targpath string) (string, error)

Rel is equivalent to filepath.Rel.

Can return empty string, especially on error.

Normalizes before returning otherwise.

func StripComponents

func StripComponents(path string, countUint32 uint32) (string, bool)

StripComponents strips the specified number of components.

Path expected to be normalized. Returns false if the path does not have more than the specified number of components.

func Unnormalize

func Unnormalize(path string) string

Unnormalize unnormalizes the given path.

This calls filepath.FromSlash on the path. If the path is "", this returns "".

type Error

type Error struct {
	Path string
	Err  error
}

Error is a path error.

func NewError

func NewError(path string, err error) *Error

NewError returns a new Error.

func (*Error) Error

func (e *Error) Error() string

Error implements error.

type PathType

type PathType int

PathType is a terminate type for path comparisons.

const (
	// Relative is the PathType for normalized and validated paths.
	Relative PathType = 1
	// Absolute is the PathType for normalized and absolute paths.
	Absolute PathType = 2
)

func (PathType) Separator

func (t PathType) Separator() string

Separator gets the string value of the separator.

TODO: rename to Terminator if we keep this TODO: we should probably refactor so we never need to use absolute paths at all this could be accomplished if we could for ExternalPathToRelPath on buckets

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier