fileutils

package
v0.0.0-...-41e44c1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: BSD-3-Clause Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const DirMode = os.ModePerm

DirMode is the mode used for created dirs

View Source
const FileMode = 0o644

FileMode is the mode used for created files

View Source
const LineEnd = "\n"

Variables

View Source
var ErrorFileNotFound = errs.New("File could not be found")

Functions

func AmendFile

func AmendFile(filePath string, data []byte, flag AmendOptions) error

AmendFile amends data to a file, supports append, or prepend

func AmendFileLocked

func AmendFileLocked(filePath string, data []byte, flag AmendOptions) error

func AppendToFile

func AppendToFile(filepath string, data []byte) error

AppendToFile appends the data to the file (if it exists) with the given data, if the file doesn't exist it is created and the data is written

func CaseSensitivePath

func CaseSensitivePath(path string) (string, error)

func CommonParentPath

func CommonParentPath(paths []string) string

CommonParentPath will return the common parent path of the given paths, provided they share a common path. If they do not all share a single common path the result will be empty.

func CopyAndRenameFiles

func CopyAndRenameFiles(fromPath, toPath string, exclude ...string) error

CopyAndRenameFiles copies files from fromDir to toDir. If the target file exists already, the source file is first copied next to the target file, and then overwrites the target by renaming the source. This method is more robust and than copying directly, in case the target file is opened or executed.

func CopyAsset

func CopyAsset(assetName, dest string) error

func CopyFile

func CopyFile(src, target string) error

CopyFile copies a file from one location to another

func CopyFiles

func CopyFiles(src, dst string) error

CopyFiles will copy all of the files/dirs within one directory to another. Both directories must already exist

func CopyFilesDirReader

func CopyFilesDirReader(reader DirReader, src, dst, placeholderFileName string) error

func CopyMultipleFiles

func CopyMultipleFiles(files map[string]string) error
func CopySymlink(src, dest string) error

CopySymlink reads the symlink at src and creates a new link at dest

func DirExists

func DirExists(path string) bool

DirExists checks if the given directory exists

func FileContains

func FileContains(path string, searchText []byte) (bool, error)

func FileExists

func FileExists(path string) bool

FileExists checks if the given file (not folder) exists

func FindFileInPath

func FindFileInPath(dir, filename string) (string, error)

FindFileInPath will find a file by the given file-name in the directory provided or in one of the parent directories of that path by walking up the tree. If the file is found, the path to that file is returned, otherwise an failure is returned.

func GetLongPathName

func GetLongPathName(path string) (string, error)

GetLongPathName returns the Windows long path (ie. ~1 notation is expanded) This function does not alter the path in any way on Linux or MacOS

func GetShortPathName

func GetShortPathName(path string) (string, error)

GetShortPathName returns the Windows short path This function does not alter the path in any way on Linux or MacOS

func HashDirectory

func HashDirectory(path string) (string, error)

HashDirectory will sha256 hash the given directory

func HideFile

func HideFile(path string) error

func IsBinary

func IsBinary(fileBytes []byte) bool

IsBinary checks if the given bytes are for a binary file

func IsDir

func IsDir(path string) bool

IsDir returns true if the given path is a directory

func IsEmptyDir

func IsEmptyDir(path string) (bool, error)

IsEmptyDir returns true if the directory at the provided path has no files (including dirs) within it.

func IsExecutable

func IsExecutable(path string) bool

IsExecutable determines if the file at the given path has any execute permissions. This function does not care whether the current user can has enough privilege to execute the file.

func IsSymlink(path string) bool

IsSymlink checks if a path is a symlink

func IsWritable

func IsWritable(path string) bool

IsWritable returns true if the given path is writable

func Join

func Join(elem ...string) string

Join is identical to filepath.Join except that it doesn't clean the input, allowing for more consistent behavior

func ListDirSimple

func ListDirSimple(sourcePath string, includeDirs bool) ([]string, error)

ListDirSimple recursively lists filepaths under the given sourcePath This does not follow symlinks

func ListFilesUnsafe

func ListFilesUnsafe(sourcePath string) []string

ListFilesUnsafe lists filepaths under the given sourcePath non-recursively

func LogPath

func LogPath(path string) error

LogPath will walk the given file path and log the name, permissions, mod time, and file size of all files it encounters

func Mkdir

func Mkdir(path string, subpath ...string) error

Mkdir is a small helper function to create a directory if it doesnt already exist

func MkdirUnlessExists

func MkdirUnlessExists(path string) error

MkdirUnlessExists will make the directory structure if it doesn't already exists

func ModTime

func ModTime(path string) (time.Time, error)

func MoveAllFiles

func MoveAllFiles(fromPath, toPath string) error

MoveAllFiles will move all of the files/dirs within one directory to another directory. Both directories must already exist.

func MoveAllFilesCrossDisk

func MoveAllFilesCrossDisk(src, dst string) error

MoveAllFilesCrossDisk will move all of the files/dirs within one directory to another directory even across disks. Both directories must already exist.

func MoveAllFilesRecursively

func MoveAllFilesRecursively(fromPath, toPath string, cb MoveAllFilesCallback) error

MoveAllFilesRecursively moves files and directories from one directory to another. Unlike in MoveAllFiles, the destination directory does not need to be empty, and may include directories that are moved from the source directory. It also counts the moved files for use in a progress bar. Warnings are printed if - a source file overwrites an existing destination file - a sub-directory exists in both the source and and the destination and their permissions do not match

func PathContainsParent

func PathContainsParent(path, parentPath string) (bool, error)

PathContainsParent checks if the directory path is equal to or a child directory of the targeted directory. Symlinks are evaluated for this comparison.

func PathInList

func PathInList(listSep, pathList, path string) (bool, error)

PathInList returns whether the provided path list contains the provided path.

func PathsEqual

func PathsEqual(paths ...string) (bool, error)

PathsEqual checks whether the paths given all resolve to the same path

func PathsMatch

func PathsMatch(paths ...string) (bool, error)

PathsMatch checks if all the given paths resolve to the same value

func PrepareDir

func PrepareDir(path string) (string, error)

PrepareDir prepares a path by ensuring it exists and the path is consistent

func PrependToFile

func PrependToFile(filepath string, data []byte) error

PrependToFile prepends the data to the file (if it exists) with the given data, if the file doesn't exist it is created and the data is written

func ReadFile

func ReadFile(filePath string) ([]byte, error)

ReadFile reads the content of a file

func ReadFileUnsafe

func ReadFileUnsafe(src string) []byte

ReadFileUnsafe is an unsafe version of os.ReadFile, DO NOT USE THIS OUTSIDE OF TESTS

func ReplaceAll

func ReplaceAll(filename, find, replace string) error

ReplaceAll replaces all instances of search text with replacement text in a file, which may be a binary file.

func ResolvePath

func ResolvePath(path string) (string, error)

ResolvePath gets the absolute location of the provided path and fully evaluates the result if it is a symlink.

func ResolvePathIfPossible

func ResolvePathIfPossible(path string) string

func ResolveUniquePath

func ResolveUniquePath(path string) (string, error)

ResolveUniquePath gets the absolute location of the provided path with the best effort attempt to produce the same result for all possible paths to the given target.

func Sha256Hash

func Sha256Hash(path string) (string, error)

Sha256Hash will sha256 hash the given file

func SymlinkTarget

func SymlinkTarget(symlink string) (string, error)

SymlinkTarget retrieves the target of the given symlink

func TargetExists

func TargetExists(path string) bool

TargetExists checks if the given file or folder exists

func TempDirFromBaseDirUnsafe

func TempDirFromBaseDirUnsafe(baseDir string) string

func TempDirUnsafe

func TempDirUnsafe() string

TempDirUnsafe returns a temp path or panics if it cannot be created This is for use in tests, do not use it outside tests!

func TempFilePath

func TempFilePath(dir, pattern string) string

func TempFileUnsafe

func TempFileUnsafe(dir, pattern string) *os.File

TempFileUnsafe returns a tempfile handler or panics if it cannot be created This is for use in tests, do not use it outside tests!

func Touch

func Touch(path string) error

Touch will attempt to "touch" a given filename by trying to open it read-only or create the file with 0644 perms if it does not exist.

func TouchFileUnlessExists

func TouchFileUnlessExists(path string) error

TouchFileUnlessExists will attempt to "touch" a given filename if it doesn't already exists

func WriteFile

func WriteFile(filePath string, data []byte) (rerr error)

WriteFile writes data to a file, if it exists it is overwritten, if it doesn't exist it is created and data is written

func WriteTempFile

func WriteTempFile(pattern string, data []byte) (string, error)

WriteTempFile writes data to a temp file.

func WriteTempFileToDir

func WriteTempFileToDir(dir, pattern string, data []byte, perm os.FileMode) (string, error)

WriteTempFileToDir writes data to a temp file in the given dir

Types

type AmendOptions

type AmendOptions uint8

AmendOptions used to specify write actions for WriteFile

const (
	// AmendByAppend content to end of file
	AmendByAppend AmendOptions = iota
	// WriteOverwrite file with contents
	WriteOverwrite
	// AmendByPrepend - add content start of file
	AmendByPrepend
)

type DirEntries

type DirEntries []DirEntry

func ListDir

func ListDir(sourcePath string, includeDirs bool) (DirEntries, error)

ListDir recursively lists filepaths under the given sourcePath This does not follow symlinks

func (DirEntries) RelativePaths

func (d DirEntries) RelativePaths() []string

type DirEntry

type DirEntry struct {
	fs.DirEntry
	// contains filtered or unexported fields
}

func (DirEntry) AbsolutePath

func (d DirEntry) AbsolutePath() string

func (DirEntry) RelativePath

func (d DirEntry) RelativePath() string

type DirReader

type DirReader interface {
	ReadDir(string) ([]os.DirEntry, error)
}

type ErrAlreadyExist

type ErrAlreadyExist struct {
	Path string
}

func (*ErrAlreadyExist) Error

func (e *ErrAlreadyExist) Error() string

type MoveAllFilesCallback

type MoveAllFilesCallback func(fromPath, toPath string)

MoveAllFilesCallback is invoked for every file that we move

Jump to

Keyboard shortcuts

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