fileutil

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package fileutil provides utility functions for file operations including reading, writing, searching, and managing files and directories.

Index

Constants

View Source
const (
	DefaultDirPerms  = 0o755
	DefaultFilePerms = 0o644
	// MaxReadSize limits AI from reading massive files (1MB is usually plenty for context)
	MaxReadSize = 1 * 1024 * 1024
)

Standard permissions for a Native application

View Source
const MaxLineLength = 2000

MaxLineLength is the maximum allowed line length when reading files

Variables

View Source
var CommonIgnoredDirs = map[string]bool{
	".DS_Store":        true,
	".opencode":        true,
	"node_modules":     true,
	"vendor":           true,
	"dist":             true,
	"build":            true,
	"target":           true,
	".git":             true,
	".svn":             true,
	".hg":              true,
	".idea":            true,
	".vscode":          true,
	".cursor":          true,
	".claude":          true,
	".vscode-insiders": true,
	".zig-cache":       true,
	".zig-out":         true,
	".zig":             true,
	".gradle":          true,
	".cache":           true,
	"cache":            true,
	".venv":            true,
	"venv":             true,
	".env":             true,
	"env":              true,
	".gitignore":       true,
	".gitattributes":   true,
	".gitmodules":      true,
	".gitkeep":         true,
	".gitlab":          true,
	".github":          true,
	".githooks":        true,
	".agents":          true,
	"__pycache__":      true,
	"bin":              true,
	"obj":              true,
	"out":              true,
	".coverage":        true,
	"coverage":         true,
	"tmp":              true,
	"temp":             true,
	"logs":             true,
	"generated":        true,
	"bower_components": true,
	"jspm_packages":    true,
}

CommonIgnoredDirs is a map of directory names that should be ignored in file operations

View Source
var CommonIgnoredExtensions = map[string]bool{
	".swp":   true,
	".swo":   true,
	".tmp":   true,
	".temp":  true,
	".bak":   true,
	".log":   true,
	".obj":   true,
	".out":   true,
	".pyc":   true,
	".pyo":   true,
	".pyd":   true,
	".o":     true,
	".so":    true,
	".dylib": true,
	".dll":   true,
	".a":     true,
	".exe":   true,
	".lock":  true,
}

CommonIgnoredExtensions is a map of file extensions that should be ignored

View Source
var ImageTypes = map[string]string{
	".jpg":  "JPEG",
	".jpeg": "JPEG",
	".png":  "PNG",
	".gif":  "GIF",
	".bmp":  "BMP",
	".svg":  "SVG",
	".webp": "WebP",
}

ImageTypes maps image extensions to their type names

Functions

func CreateFile added in v1.5.0

func CreateFile(path, content string) error

CreateFile creates a new file with the given content

func DeleteDir added in v1.5.0

func DeleteDir(path string) error

DeleteDir deletes a directory recursively

func DeleteFile added in v1.5.0

func DeleteFile(path string) error

DeleteFile deletes a file

func DirExists added in v1.5.0

func DirExists(path string) bool

DirExists checks if a directory exists

func EnsureParentDir added in v1.5.0

func EnsureParentDir(path string) error

EnsureParentDir ensures the parent directory of a path exists

func EscapeRegexPattern added in v1.5.0

func EscapeRegexPattern(pattern string) string

EscapeRegexPattern escapes special regex characters

func Exists added in v1.5.0

func Exists(path string) bool

Exists checks if a path exists (file or directory)

func FileContainsPattern added in v1.5.0

func FileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, string, error)

FileContainsPattern checks if a file contains a pattern

func FileExists added in v1.5.0

func FileExists(path string) bool

FileExists checks if a path exists and is a file.

func GetFileInfo added in v1.5.0

func GetFileInfo(path string) (os.FileInfo, error)

GetFileInfo returns file information

func GetFileInfoL added in v1.5.0

func GetFileInfoL(path string) (os.FileInfo, error)

GetFileInfoL returns file information without following symlinks

func GetFileModTime added in v1.5.0

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

GetFileModTime returns the modification time of a file

func GetFileSize added in v1.5.0

func GetFileSize(path string) (int64, error)

GetFileSize returns the size of a file

func GetFzfCmd

func GetFzfCmd(query string) *exec.Cmd

GetFzfCmd returns a command for fzf with the given query

func GetParentDir added in v1.5.0

func GetParentDir(path string) string

GetParentDir returns the parent directory of a path

func GetRgCmd

func GetRgCmd(globPattern string) *exec.Cmd

GetRgCmd returns a command for ripgrep with the given glob pattern

func GlobToRegex added in v1.5.0

func GlobToRegex(glob string) string

GlobToRegex converts a glob pattern to a regex pattern

func GlobWithDoublestar

func GlobWithDoublestar(pattern, searchPath string, limit int) ([]string, bool, error)

GlobWithDoublestar finds files matching a pattern

func IsDirectory added in v1.5.0

func IsDirectory(path string) bool

IsDirectory checks if a path is a directory

func IsFile added in v1.5.0

func IsFile(path string) bool

IsFile checks if a path is a file (not a directory)

func IsFileModifiedSince added in v1.5.0

func IsFileModifiedSince(path string, since time.Time) (bool, error)

IsFileModifiedSince checks if a file has been modified since a given time

func IsHiddenFile added in v1.5.0

func IsHiddenFile(path string) bool

IsHiddenFile checks if a file is hidden (starts with dot)

func IsIgnoredDir added in v1.5.0

func IsIgnoredDir(path string) bool

IsIgnoredDir checks if a directory should be ignored

func IsIgnoredExtension added in v1.5.0

func IsIgnoredExtension(path string) bool

IsIgnoredExtension checks if a file extension should be ignored

func IsImageFile added in v1.5.0

func IsImageFile(path string) (bool, string)

IsImageFile checks if a file is an image based on extension

func IsInWorkingDir added in v1.5.0

func IsInWorkingDir(path, workingDir string) bool

IsInWorkingDir checks if a path is within the working directory

func IsTextFile added in v1.5.0

func IsTextFile(path string) (bool, error)

IsTextFile checks if a file is likely text vs binary to prevent AI from reading garbage.

func ListDirectory added in v1.5.0

func ListDirectory(initialPath string, ignorePatterns []string, limit int) ([]string, bool, error)

ListDirectory lists files in a directory

func NormalizePath added in v1.5.0

func NormalizePath(path string) string

NormalizePath normalizes a path by resolving . and .. components

func NormalizePathForAI added in v1.5.0

func NormalizePathForAI(path string) string

NormalizePathForAI ensures paths always use forward slashes, which AI models handle better.

func ReadFile added in v1.5.0

func ReadFile(path string) (string, error)

ReadFile reads a file and returns its content

func ReloadTools added in v1.5.0

func ReloadTools()

ReloadTools refreshes the path for external dependencies

func ResolvePath added in v1.5.0

func ResolvePath(path, workingDir string) string

ResolvePath converts a path to absolute using the working directory. It cleans the path to prevent basic traversal but does NOT enforce boundaries.

func ResolvePaths added in v1.5.0

func ResolvePaths(paths []string, workingDir string) []string

ResolvePaths resolves multiple paths to absolute paths

func SafeReadFile added in v1.5.0

func SafeReadFile(path, workingDir string) (string, error)

SafeReadFile reads a file only if it meets security and size requirements. It opens the file once and reuses the file descriptor for all operations, avoiding redundant I/O operations.

func SecureResolvePath added in v1.5.0

func SecureResolvePath(path, workingDir string) (string, error)

SecureResolvePath ensures the resolved path is trapped within the workingDir. Use this for any AI-generated path to prevent Path Traversal attacks.

func ShouldSkipPath added in v1.5.0

func ShouldSkipPath(path string, ignorePatterns []string) bool

ShouldSkipPath checks if a path should be skipped based on ignore patterns This is a more comprehensive version that combines hidden check with custom patterns

func SkipHidden

func SkipHidden(path string) bool

SkipHidden checks if a path should be skipped (hidden or ignored). Optimized to avoid heavy allocations like strings.Split.

func WriteFile added in v1.5.0

func WriteFile(path, content string) error

WriteFile writes content to a file

Types

type FileInfo

type FileInfo struct {
	Path    string
	ModTime time.Time
	Size    int64
	IsDir   bool
}

FileInfo contains basic file information

type FileOperation added in v1.5.0

type FileOperation int

FileOperation represents the type of file operation being performed

const (
	OpRead FileOperation = iota
	OpWrite
	OpEdit
	OpDelete
	OpCreate
)

type FileReadResult added in v1.5.0

type FileReadResult struct {
	Content    string
	TotalLines int
	Error      error
}

FileReadResult contains the result of reading a file

func ReadFileWithLimit added in v1.5.0

func ReadFileWithLimit(path string, offset, limit int) (FileReadResult, error)

ReadFileWithLimit reads a file with line offset and limit

type FileValidationResult added in v1.5.0

type FileValidationResult struct {
	AbsPath      string
	Exists       bool
	IsDirectory  bool
	IsModified   bool
	LastReadTime time.Time
	ModTime      time.Time
	Size         int64
	Error        error
}

FileValidationResult contains the result of file validation

func ValidateFileForDelete added in v1.5.0

func ValidateFileForDelete(path, workingDir string) *FileValidationResult

ValidateFileForDelete validates a file for deletion

func ValidateFileForRead added in v1.5.0

func ValidateFileForRead(path, workingDir string, lastReadTime time.Time) *FileValidationResult

ValidateFileForRead validates a file for reading

func ValidateFileForReadWithoutLastRead added in v1.5.0

func ValidateFileForReadWithoutLastRead(path, workingDir string) *FileValidationResult

ValidateFileForReadWithoutLastRead validates a file for reading without last read check

func ValidateFileForWrite added in v1.5.0

func ValidateFileForWrite(path, workingDir string, lastReadTime time.Time) *FileValidationResult

ValidateFileForWrite validates a file for writing

func ValidateFileOperation added in v1.5.0

func ValidateFileOperation(path, workingDir string, operation FileOperation) *FileValidationResult

ValidateFileOperation performs comprehensive validation for file operations

type PathConfig added in v1.5.0

type PathConfig struct {
	WorkingDir     string
	IgnorePatterns []string
}

PathConfig holds configuration for path operations

func DefaultPathConfig added in v1.5.0

func DefaultPathConfig(workingDir string) PathConfig

DefaultPathConfig returns a default path configuration

Jump to

Keyboard shortcuts

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