Documentation
¶
Overview ¶
Package fileutil provides utility functions for file operations including reading, writing, searching, and managing files and directories.
Index ¶
- Constants
- Variables
- func CreateFile(path, content string) error
- func DeleteDir(path string) error
- func DeleteFile(path string) error
- func DirExists(path string) bool
- func EnsureParentDir(path string) error
- func EscapeRegexPattern(pattern string) string
- func Exists(path string) bool
- func FileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, string, error)
- func FileExists(path string) bool
- func GetFileInfo(path string) (os.FileInfo, error)
- func GetFileInfoL(path string) (os.FileInfo, error)
- func GetFileModTime(path string) (time.Time, error)
- func GetFileSize(path string) (int64, error)
- func GetFzfCmd(query string) *exec.Cmd
- func GetParentDir(path string) string
- func GetRgCmd(globPattern string) *exec.Cmd
- func GlobToRegex(glob string) string
- func GlobWithDoublestar(pattern, searchPath string, limit int) ([]string, bool, error)
- func IsDirectory(path string) bool
- func IsFile(path string) bool
- func IsFileModifiedSince(path string, since time.Time) (bool, error)
- func IsHiddenFile(path string) bool
- func IsIgnoredDir(path string) bool
- func IsIgnoredExtension(path string) bool
- func IsImageFile(path string) (bool, string)
- func IsInWorkingDir(path, workingDir string) bool
- func IsTextFile(path string) (bool, error)
- func ListDirectory(initialPath string, ignorePatterns []string, limit int) ([]string, bool, error)
- func NormalizePath(path string) string
- func NormalizePathForAI(path string) string
- func ReadFile(path string) (string, error)
- func ReloadTools()
- func ResolvePath(path, workingDir string) string
- func ResolvePaths(paths []string, workingDir string) []string
- func SafeReadFile(path, workingDir string) (string, error)
- func SecureResolvePath(path, workingDir string) (string, error)
- func ShouldSkipPath(path string, ignorePatterns []string) bool
- func SkipHidden(path string) bool
- func WriteFile(path, content string) error
- type FileInfo
- type FileOperation
- type FileReadResult
- type FileValidationResult
- func ValidateFileForDelete(path, workingDir string) *FileValidationResult
- func ValidateFileForRead(path, workingDir string, lastReadTime time.Time) *FileValidationResult
- func ValidateFileForReadWithoutLastRead(path, workingDir string) *FileValidationResult
- func ValidateFileForWrite(path, workingDir string, lastReadTime time.Time) *FileValidationResult
- func ValidateFileOperation(path, workingDir string, operation FileOperation) *FileValidationResult
- type PathConfig
Constants ¶
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
const MaxLineLength = 2000
MaxLineLength is the maximum allowed line length when reading files
Variables ¶
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
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
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
CreateFile creates a new file with the given content
func EnsureParentDir ¶ added in v1.5.0
EnsureParentDir ensures the parent directory of a path exists
func EscapeRegexPattern ¶ added in v1.5.0
EscapeRegexPattern escapes special regex characters
func FileContainsPattern ¶ added in v1.5.0
FileContainsPattern checks if a file contains a pattern
func FileExists ¶ added in v1.5.0
FileExists checks if a path exists and is a file.
func GetFileInfo ¶ added in v1.5.0
GetFileInfo returns file information
func GetFileInfoL ¶ added in v1.5.0
GetFileInfoL returns file information without following symlinks
func GetFileModTime ¶ added in v1.5.0
GetFileModTime returns the modification time of a file
func GetFileSize ¶ added in v1.5.0
GetFileSize returns the size of a file
func GetParentDir ¶ added in v1.5.0
GetParentDir returns the parent directory of a path
func GlobToRegex ¶ added in v1.5.0
GlobToRegex converts a glob pattern to a regex pattern
func GlobWithDoublestar ¶
GlobWithDoublestar finds files matching a pattern
func IsDirectory ¶ added in v1.5.0
IsDirectory checks if a path is a directory
func IsFileModifiedSince ¶ added in v1.5.0
IsFileModifiedSince checks if a file has been modified since a given time
func IsHiddenFile ¶ added in v1.5.0
IsHiddenFile checks if a file is hidden (starts with dot)
func IsIgnoredDir ¶ added in v1.5.0
IsIgnoredDir checks if a directory should be ignored
func IsIgnoredExtension ¶ added in v1.5.0
IsIgnoredExtension checks if a file extension should be ignored
func IsImageFile ¶ added in v1.5.0
IsImageFile checks if a file is an image based on extension
func IsInWorkingDir ¶ added in v1.5.0
IsInWorkingDir checks if a path is within the working directory
func IsTextFile ¶ added in v1.5.0
IsTextFile checks if a file is likely text vs binary to prevent AI from reading garbage.
func ListDirectory ¶ added in v1.5.0
ListDirectory lists files in a directory
func NormalizePath ¶ added in v1.5.0
NormalizePath normalizes a path by resolving . and .. components
func NormalizePathForAI ¶ added in v1.5.0
NormalizePathForAI ensures paths always use forward slashes, which AI models handle better.
func ReloadTools ¶ added in v1.5.0
func ReloadTools()
ReloadTools refreshes the path for external dependencies
func ResolvePath ¶ added in v1.5.0
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
ResolvePaths resolves multiple paths to absolute paths
func SafeReadFile ¶ added in v1.5.0
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
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
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 ¶
SkipHidden checks if a path should be skipped (hidden or ignored). Optimized to avoid heavy allocations like strings.Split.
Types ¶
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
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
PathConfig holds configuration for path operations
func DefaultPathConfig ¶ added in v1.5.0
func DefaultPathConfig(workingDir string) PathConfig
DefaultPathConfig returns a default path configuration