paths

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

builds.sr.ht status Go Reference

paths

A go module for working with paths.

Features

  • Provides Home, Config and Cache for retrieving $HOME, $HOME/.config and $HOME/.cache respectively.
  • Resolves paths via paths.Resolve & paths.PathResolver.Resolve.
  • Provides FileInfo which stores the size in bytes, modified time.Time and other relevant information.

Documentation

Overview

Provides shortcuts to os.UserHomeDir, os.UserConfigDir & os.UserCacheDir, the ability to Resolve a path, and a way to store os.Stat info as FileInfo via Open.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cache

func Cache() string

Returns os.UserCacheDir, but the value is cached locally to avoid unnecessary repeat calls.

func Config

func Config() string

Returns os.UserConfigDir, but the value is cached locally to avoid unnecessary repeat calls.

func Exists

func Exists(path string) bool

Check if a file or directory exists at path.

func Home

func Home() string

Returns os.UserHomeDir, but the value is cached locally to avoid unnecessary repeat calls.

func Resolve

func Resolve(path string) (string, error)

Attempt to resolve a path by checking first if it exists, otherwise make it absolute and then check again. Returns fs.ErrNotExist on failure.

Types

type FileInfo

type FileInfo struct {
	Path      string    `json:"path"`
	IsRegular bool      `json:"is_regular"`
	IsDir     bool      `json:"is_dir"`
	Name      string    `json:"filename"`
	Size      int64     `json:"size"`
	Modified  time.Time `json:"modified"`
}

Stores information on a file.

func Open

func Open(p string) (FileInfo, error)

Calls os.Open and then os.File.Stat to populate FileInfo.

func (*FileInfo) Equals

func (fi *FileInfo) Equals(other *FileInfo) bool

Determines if this *FileInfo is the same as other by comparing name, size & modified time.Time.

func (*FileInfo) Newer

func (fi *FileInfo) Newer(other *FileInfo) bool

Determines if this *FileInfo is newer than other.

func (FileInfo) String

func (fi FileInfo) String() string

Return terminal friendly string.

type FileInfoError

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

Error returned by Open, contains errors returned by os.Open & os.Stat so you may utilise errors.Is like so...

 fi, err := paths.Open(path)
	 if errors.Is(err, os.PathError) {
	   ...
 }

func (*FileInfoError) Error

func (e *FileInfoError) Error() string

func (*FileInfoError) Unwrap

func (e *FileInfoError) Unwrap() error

type Path

type Path string

A Path is just a string that can be operated on.

All operations are platform agnostic.

func (*Path) Exists

func (p *Path) Exists() bool

Check if this *Path exists on the system.

func (*Path) Resolve

func (p *Path) Resolve() (string, error)

Calls Resolve on this *Path.

func (Path) String

func (p Path) String() string

Return Path as a string.

type PathResolver

type PathResolver struct {
	Path          string
	ResolveToHome bool
}

For more complex operations, this struct is provided to facilitate options such as considering `$HOME` when resolving.

func NewPathResolver

func NewPathResolver(path string, options ...ResolverOption) *PathResolver

Create a new *PathResolver.

r := paths.NewPathResolver(path, paths.ResolveToHome())
path, err := r.Resolve()

func (*PathResolver) Resolve

func (resolver *PathResolver) Resolve() (string, error)

Calls Resolve, but on error, it will consider its [ResolverOption]s and act appropriately.

type ResolverOption

type ResolverOption func(*PathResolver)

ResolverOption is a function that accepts a *PathResolver and applies some configuration.

func ResolveToHome

func ResolveToHome() ResolverOption

Pass this to NewPathResolver to instruct PathResolver to consider `$HOME` when resolving a path.

Jump to

Keyboard shortcuts

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