utils

package
v7.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package utils provides utility functions for working with file paths, URIs, and other common operations.

Index

Constants

View Source
const (
	// ErrBadAbsFilePath constant is returned when a file path is not absolute
	ErrBadAbsFilePath = "absolute file path is invalid - must include leading slash and may not include trailing slash"
	// ErrBadRelFilePath constant is returned when a file path is not relative
	ErrBadRelFilePath = "relative file path is invalid - may not include leading or trailing slashes"
	// ErrBadAbsLocationPath constant is returned when a file path is not absolute
	ErrBadAbsLocationPath = "absolute location path is invalid - must include leading and trailing slashes"
	// ErrBadRelLocationPath constant is returned when a file path is not relative
	ErrBadRelLocationPath = "relative location path is invalid - may not include leading slash but must include trailing slash"
	// ErrBadPrefix constant is returned when a prefix is not relative or ends in / or is empty
	ErrBadPrefix = "prefix is invalid - may not include leading or trailing slashes and may not be empty"
	// TouchCopyMinBufferSize min buffer size used in TouchCopyBuffered in bytes
	TouchCopyMinBufferSize = 262144
)

Variables

This section is empty.

Functions

func EncodeURI

func EncodeURI(scheme, username, hostport, path string) string

EncodeURI ensure that a uri is properly percent-encoded

func EnsureLeadingSlash

func EnsureLeadingSlash(dir string) string

EnsureLeadingSlash is like EnsureTrailingSlash except that it adds the leading slash if needed.

func EnsureTrailingSlash

func EnsureTrailingSlash(dir string) string

EnsureTrailingSlash is like AddTrailingSlash but will only ever use / since it's use for web uri's, never a Windows OS path.

func GetFileURI

func GetFileURI(f vfs.File) string

GetFileURI returns a File URI

func GetLocationURI

func GetLocationURI(l vfs.Location) string

GetLocationURI returns a Location URI

func PathToURI

func PathToURI(p string) (string, error)

PathToURI takes a relative or absolute path and returns an OS URI. We assume non-scheme path is an OS File or Location. We assume URI authority is empty. We assume relative paths are relative to the pwd (program's working directory)

/absolute/path/to/file.txt : file:///absolute/path/to/file.txt /some/absolute/path/ : file:///absolute/path/ relative/path/to/file.txt : file:///absolute/path/with/relative/path/to/file.txt relative/path/ : file:///absolute/path/with/relative/path/

func Ptr

func Ptr[T any](value T) *T

Ptr returns a pointer to the given value.

func RemoveLeadingSlash

func RemoveLeadingSlash(path string) string

RemoveLeadingSlash removes leading slash, if any

func RemoveTrailingSlash

func RemoveTrailingSlash(path string) string

RemoveTrailingSlash removes trailing slash, if any

func SeekTo

func SeekTo(length, position, offset int64, whence int) (int64, error)

SeekTo is a helper function for Seek. It takes the current position, offset, whence, and length of the file and returns the new position. It also checks for invalid offsets and returns an error if one is found.

func TouchCopy

func TouchCopy(writer io.Writer, reader io.Reader) error

TouchCopy is a wrapper around io.Copy which ensures that even empty source files (reader) will get written as an empty file. It guarantees a Write() call on the target file. Deprecated: Use TouchCopyBuffer Instead

func TouchCopyBuffered

func TouchCopyBuffered(writer io.Writer, reader io.Reader, bufferSize int) error

TouchCopyBuffered is a wrapper around io.CopyBuffer which ensures that even empty source files (reader) will get written as an empty file. It guarantees a Write() call on the target file. bufferSize is in bytes and if is less than TouchCopyMinBufferSize will result in a buffer of size TouchCopyMinBufferSize bytes. If bufferSize is > TouchCopyMinBufferSize it will result in a buffer of size bufferSize bytes

func UpdateLastModifiedByMoving

func UpdateLastModifiedByMoving(file vfs.File) error

UpdateLastModifiedByMoving is used by some backends' Touch() method when a file already exists.

func ValidateAbsoluteFilePath

func ValidateAbsoluteFilePath(name string) error

ValidateAbsoluteFilePath ensures that a file path has a leading slash but not a trailing slash

func ValidateAbsoluteLocationPath

func ValidateAbsoluteLocationPath(name string) error

ValidateAbsoluteLocationPath ensure that a file path has both leading and trailing slashes

func ValidatePrefix

func ValidatePrefix(prefix string) error

ValidatePrefix ensures that a prefix path has neither leading nor trailing slashes may not be empty but unlike relative file path, *may* be simply "."

func ValidateRelativeFilePath

func ValidateRelativeFilePath(name string) error

ValidateRelativeFilePath ensures that a file path has neither leading nor trailing slashes

func ValidateRelativeLocationPath

func ValidateRelativeLocationPath(name string) error

ValidateRelativeLocationPath ensure that a file path has no leading slash but has a trailing slash

func WrapCloseError

func WrapCloseError(err error) error

WrapCloseError returns a wrapped close error

func WrapCopyToFileError added in v7.3.0

func WrapCopyToFileError(err error) error

WrapCopyToFileError returns a wrapped copyToFile error

func WrapCopyToLocationError added in v7.3.0

func WrapCopyToLocationError(err error) error

WrapCopyToLocationError returns a wrapped copyToLocation error

func WrapDeleteError added in v7.3.0

func WrapDeleteError(err error) error

WrapDeleteError returns a wrapped delete error

func WrapExistsError added in v7.3.0

func WrapExistsError(err error) error

WrapExistsError returns a wrapped exists error

func WrapLastModifiedError added in v7.3.0

func WrapLastModifiedError(err error) error

WrapLastModifiedError returns a wrapped lastModified error

func WrapMoveToFileError added in v7.3.0

func WrapMoveToFileError(err error) error

WrapMoveToFileError returns a wrapped moveToFile error

func WrapMoveToLocationError added in v7.3.0

func WrapMoveToLocationError(err error) error

WrapMoveToLocationError returns a wrapped moveToLocation error

func WrapReadError

func WrapReadError(err error) error

WrapReadError returns a wrapped read error

func WrapSeekError

func WrapSeekError(err error) error

WrapSeekError returns a wrapped seek error

func WrapSizeError added in v7.3.0

func WrapSizeError(err error) error

WrapSizeError returns a wrapped size error

func WrapTouchError added in v7.3.0

func WrapTouchError(err error) error

WrapTouchError returns a wrapped touch error

func WrapWriteError

func WrapWriteError(err error) error

WrapWriteError returns a wrapped write error

Types

This section is empty.

Directories

Path Synopsis
Package authority provides a struct for parsing and manipulating URI authority strings.
Package authority provides a struct for parsing and manipulating URI authority strings.

Jump to

Keyboard shortcuts

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