fileutil

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 24 Imported by: 686

README

fileutil

The package contains various helpers to interact with files

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultFilePermission = os.FileMode(0644)
)
View Source
var ErrInvalidSeparator = errors.New("invalid separator")
View Source
var IsNotEmpty = func(data []byte) bool {
	return len(data) > 0
}

IsNotEmpty determines if a data chunk is empty

View Source
var SkipEmptyLine = func(line []byte) bool {
	return len(line) > 0
}

Functions

func CleanPath added in v0.0.57

func CleanPath(inputPath string) (string, error)

CleanPath cleans paths to migtigate any possible path traversal attacks. and it always returns an absolute path

func CleanPathOrDefault added in v0.0.57

func CleanPathOrDefault(inputPath string, defaultPath string) string

CleanPathOrDefault cleans paths to migtigate any possible path traversal attacks.

func CopyFile

func CopyFile(src, dst string) error

CopyFile from source to destination

func CountLines added in v0.0.7

func CountLines(filename string) (uint, error)

CountLines counts the lines in a file

func CountLinesWithOptions added in v0.0.7

func CountLinesWithOptions(reader io.Reader, separator []byte, filter func([]byte) bool) (uint, error)

CountLinesWithOptions from a reader and custom filter function

func CountLinesWithSeparator added in v0.0.7

func CountLinesWithSeparator(separator []byte, filename string) (uint, error)

CountLinesWithSeparator of a file

func CreateFolder

func CreateFolder(path string) error

CreateFolder path

func CreateFolders

func CreateFolders(paths ...string) error

CreateFolders in the list

func DedupeLines added in v0.0.93

func DedupeLines(filename string) error

DedupeLines reads a file and removes duplicate lines from it. The function can be memory intensive for large files.

func DeleteFilesOlderThan

func DeleteFilesOlderThan(folder string, filter FileFilters) error

func DownloadFile

func DownloadFile(filepath string, url string) error

DownloadFile to specified path

func ExecutableName

func ExecutableName() string

func FileExists

func FileExists(filename string) bool

FileExists checks if the file exists in the provided path

func FileExistsIn added in v0.0.46

func FileExistsIn(file string, allowedPaths ...string) (string, error)

FileExistsIn checks if the file exists in the allowed paths

func FileOrFolderExists added in v0.0.4

func FileOrFolderExists(name string) bool

FileOrFolderExists checks if the file/folder exists

func FileSizeToByteLen added in v0.0.35

func FileSizeToByteLen(fileSize string) (int, error)

FileSizeToByteLen converts a file size with units(kb, mb, gb, tb) to byte length e.g. 1kb -> 1024 If no unit is provided, it will fallback to mb. e.g: '2' will be converted to 2097152.

func FixMissingDirs added in v0.0.57

func FixMissingDirs(path string) error

FixMissingDirs creates any missing directories in chain /path/to/file

func FolderExists

func FolderExists(foldername string) bool

FolderExists checks if the folder exists

func GetTempFileName

func GetTempFileName() (string, error)

GetTempFileName generate a temporary file name

func HasPermission

func HasPermission(fileName string, permission int) (bool, error)

HasPermission checks if the file has the requested permission

func HasStdin

func HasStdin() bool

HasStdin determines if the user has piped input

func IsEmpty added in v0.1.2

func IsEmpty(filename string) (bool, error)

IsEmpty checks if the file is empty

func IsReadable

func IsReadable(fileName string) (bool, error)

IsReadable verify file readability

func IsWriteable

func IsWriteable(fileName string) (bool, error)

IsWriteable verify file writeability

func Lines added in v0.11.0

func Lines(filename string, opts ...LineOption) iter.Seq2[string, error]

Lines streams lines from the file at filename, applying any configured transforms. With no options it emits raw scanner lines.

The file is opened lazily on first iteration and closed when iteration ends (including via break). Open and scanner errors are surfaced as a final ("", err) pair, after which iteration stops.

Typical use:

for v, err := range fileutil.Lines(path,
    fileutil.WithSplit(','),
    fileutil.WithTrimSpace(),
    fileutil.WithSkipEmpty(),
) {
    if err != nil { return err }
    // use v
}

func LinesReader added in v0.11.0

func LinesReader(r io.Reader, opts ...LineOption) iter.Seq2[string, error]

LinesReader is the io.Reader variant of Lines. The reader is consumed but not closed; the caller owns its lifecycle.

func Marshal

func Marshal(encodeType EncodeType, data []byte, obj interface{}) error

func MarshalToWriter

func MarshalToWriter(encodeType EncodeType, r io.Writer, obj interface{}) error

func OpenOrCreateFile added in v0.0.41

func OpenOrCreateFile(name string) (*os.File, error)

OpenOrCreate opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDWR. If there is an error, it'll create the named file with mode 0666. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. If there is an error, it will be of type *PathError. Note: The file gets created only if the target directory exists

func ReadFile deprecated

func ReadFile(filename string) (chan string, error)

ReadFile streams the file at filename line by line on a channel.

Deprecated: use Lines, which returns an iter.Seq2[string, error] and surfaces open / scanner errors. Equivalent invocation:

for line, err := range fileutil.Lines(filename) { ... }

func ReadFileWithBufferSize deprecated

func ReadFileWithBufferSize(filename string, maxCapacity int) (chan string, error)

ReadFileWithBufferSize streams the file at filename line by line on a channel using the given scanner buffer size.

Deprecated: use Lines with WithBufferSize. Equivalent invocation:

for line, err := range fileutil.Lines(filename, fileutil.WithBufferSize(n)) { ... }

func ReadFileWithReader deprecated

func ReadFileWithReader(r io.Reader) (chan string, error)

ReadFileWithReader streams r line by line on a channel.

Deprecated: use LinesReader, which returns an iter.Seq2[string, error] and surfaces scanner errors. Equivalent invocation:

for line, err := range fileutil.LinesReader(r) { ... }

func ReadFileWithReaderAndBufferSize deprecated

func ReadFileWithReaderAndBufferSize(r io.Reader, maxCapacity int) (chan string, error)

ReadFileWithReaderAndBufferSize streams r line by line on a channel using the given scanner buffer size.

Deprecated: use LinesReader with WithBufferSize. Equivalent invocation:

for line, err := range fileutil.LinesReader(r, fileutil.WithBufferSize(n)) { ... }

func RemoveAll

func RemoveAll(paths ...string) (errored map[string]error)

RemoveAll specified paths, returning those that caused error

func ResolveNClean added in v0.0.57

func ResolveNClean(inputPath string, baseDir ...string) (string, error)

ResolveNClean resolves the path and cleans it ex: a nuclei template can be either abs or relative to a specified directory this function uses given path as a base to resolve the path instead of cwd

func ResolveNCleanOrDefault added in v0.0.57

func ResolveNCleanOrDefault(inputPath string, defaultPath string, baseDir ...string) string

ResolveNCleanOrDefault resolves the path and cleans it

func SafeOpen added in v0.0.57

func SafeOpen(path string) (*os.File, error)

SafeOpen opens a file after cleaning the path in read mode

func SafeOpenAppend added in v0.0.57

func SafeOpenAppend(path string) (*os.File, error)

SafeOpenAppend opens a file after cleaning the path in append mode and creates any missing directories in chain /path/to/file

func SafeOpenWrite added in v0.0.57

func SafeOpenWrite(path string) (*os.File, error)

SafeOpenWrite opens a file after cleaning the path in write mode and creates any missing directories in chain /path/to/file

func SafeWriteFile added in v0.0.57

func SafeWriteFile(path string, data []byte) error

SafeWriteFile writes data to a file after cleaning the path in write mode and creates any missing directories in chain /path/to/file

func SubstituteConfigFromEnvVars added in v0.0.31

func SubstituteConfigFromEnvVars(filepath string) (io.Reader, error)

SubstituteConfigFromEnvVars reads a config file and generates a reader with substituted config values from environment variables

func Unmarshal

func Unmarshal(encodeType EncodeType, data []byte, obj interface{}) error

func UnmarshalFromReader

func UnmarshalFromReader(encodeType EncodeType, r io.Reader, obj interface{}) error

func UseMusl

func UseMusl(path string) (bool, error)

UseMusl checks if the specified elf file uses musl

Types

type EncodeType

type EncodeType uint8
const (
	YAML EncodeType = iota
	JSON
)

type FileFilters

type FileFilters struct {
	OlderThan    time.Duration
	Prefix       string
	Suffix       string
	RegexPattern string
	CustomCheck  func(filename string) bool
	Callback     func(filename string) error
}

type LineOption added in v0.11.0

type LineOption func(*lineConfig)

LineOption configures the line iterator returned by Lines / LinesReader.

func WithBufferSize added in v0.11.0

func WithBufferSize(n int) LineOption

WithBufferSize sets the underlying bufio.Scanner buffer. A non-positive value leaves the scanner default (64 KiB) in place.

func WithFilter added in v0.11.0

func WithFilter(keep func(string) bool) LineOption

WithFilter keeps only values for which keep returns true. The filter runs after split / trim / skip-empty so it sees the final value that would be yielded.

func WithSkipEmpty added in v0.11.0

func WithSkipEmpty() LineOption

WithSkipEmpty drops empty values, evaluated after WithTrimSpace.

func WithSplit added in v0.11.0

func WithSplit(separators ...rune) LineOption

WithSplit splits each scanned line on any of the given runes (strings.FieldsFunc semantics: runs of separator runes are collapsed and empty pieces are not produced). Each piece becomes its own emitted value.

func WithTrimSpace added in v0.11.0

func WithTrimSpace() LineOption

WithTrimSpace trims leading/trailing whitespace from each emitted value.

Jump to

Keyboard shortcuts

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