fsx

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: BSD-3-Clause Imports: 10 Imported by: 18

Documentation

Overview

Package fsx provides various utility functions for dealing with filesystems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirAndFile

func DirAndFile(file string) string

DirAndFile returns the final dir and file name.

func DirFS

func DirFS(fpath string) (fs.FS, string, error)

DirFS returns the directory part of given file path as an os.DirFS and the filename as a string. These can then be used to access the file using the FS-based interface, consistent with embed and other use-cases.

func Dirs

func Dirs(path string) []string

Dirs returns a slice of all the directories within a given directory

func ExtSplit added in v0.3.3

func ExtSplit(file string) (base, ext string)

ExtSplit returns the split between the extension and name before the extension, for the given file name. Any path elements in the file name are preserved; pass filepath.Base(file) to extract only the last element of the file path if that is what is desired.

func FileExists

func FileExists(filePath string) (bool, error)

FileExists checks whether given file exists, returning true if so, false if not, and error if there is an error in accessing the file.

func FileExistsFS

func FileExistsFS(fsys fs.FS, filePath string) (bool, error)

FileExistsFS checks whether given file exists, returning true if so, false if not, and error if there is an error in accessing the file.

func Filenames

func Filenames(path string, extensions ...string) []string

Filenames returns all the file names with given extension(s) in directory in sorted order (if extensions is empty then all files are returned)

func Files

func Files(path string, extensions ...string) []fs.DirEntry

Files returns all the FileInfo's for files with given extension(s) in directory in sorted order (if extensions are empty then all files are returned). In case of error, returns nil.

func FindFilesOnPaths

func FindFilesOnPaths(paths []string, files ...string) []string

FindFilesOnPaths attempts to locate given file(s) on given list of paths, returning the full Abs path to each file found (nil if none)

func GoSrcDir

func GoSrcDir(dir string) (absDir string, err error)

GoSrcDir tries to locate dir in GOPATH/src/ or GOROOT/src/pkg/ and returns its full path. GOPATH may contain a list of paths. From Robin Elkind github.com/mewkiz/pkg.

func HasFile

func HasFile(path, file string) bool

HasFile returns true if given directory has given file (exact match)

func LatestMod

func LatestMod(path string, exts ...string) time.Time

LatestMod returns the latest (most recent) modification time for any of the files in the directory (optionally filtered by extension(s) if exts != nil) if no files or error, returns zero time value

func RelativeFilePath

func RelativeFilePath(file, root string) string

RelativeFilePath returns the file name relative to given root file path, if it is under that root; otherwise it returns the final dir and file name.

func SplitRootPathFS added in v0.3.3

func SplitRootPathFS(path string) (root, rest string)

SplitRootPathFS returns a split of the given FS path (only / path separators) into the root element and everything after that point. Examples:

  • "/a/b/c" returns "/", "a/b/c"
  • "a/b/c" returns "a", "b/c" (note removal of intervening "/")
  • "a" returns "a", ""
  • "a/" returns "a", "" (note removal of trailing "/")

func Sub

func Sub(fsys fs.FS, dir string) fs.FS

Sub returns fs.Sub with any error automatically logged for cases where the directory is hardcoded and there is no chance of error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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