utils

package
v1.11.3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangeExtension

func ChangeExtension(path, newExt string) string

ChangeExtension changes the extension of a file path.

func CleanDir

func CleanDir(path string) error

CleanDir removes all contents of a directory but keeps the directory itself.

func ComputeFileHash

func ComputeFileHash(path string, algo HashAlgorithm) (string, error)

ComputeFileHash computes the hash of a file using the specified algorithm.

func ComputeHash

func ComputeHash(data []byte, algo HashAlgorithm) (string, error)

ComputeHash computes the hash of data using the specified algorithm.

func ComputeStringHash

func ComputeStringHash(s string, algo HashAlgorithm) (string, error)

ComputeStringHash computes the hash of a string using the specified algorithm.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a file from src to dst.

func EnsureDir

func EnsureDir(path string) error

EnsureDir creates a directory and all necessary parent directories.

func ExecCommand added in v0.5.0

func ExecCommand(name string, args ...string) (string, error)

ExecCommand executes a command and returns its output

func ExpandPath

func ExpandPath(path string) (string, error)

ExpandPath expands environment variables and home directory in a path.

func FileExists

func FileExists(path string) bool

FileExists checks if a file or directory exists.

func FindExecutable added in v0.5.0

func FindExecutable(name string) (string, error)

FindExecutable finds an executable in the system PATH

func FromSlash

func FromSlash(path string) string

FromSlash converts forward slashes to OS-specific separators.

func GetBaseName

func GetBaseName(path string) string

GetBaseName returns the base name of a path (file name without directory).

func GetDirName

func GetDirName(path string) string

GetDirName returns the directory name of a path.

func GetFileExtension

func GetFileExtension(path string) string

GetFileExtension returns the extension of a file (including the dot).

func GetFileSize

func GetFileSize(path string) (int64, error)

GetFileSize returns the size of a file in bytes.

func GetRelativePath

func GetRelativePath(base, target string) (string, error)

GetRelativePath returns the relative path from base to target.

func GlobPattern

func GlobPattern(pattern string) ([]string, error)

GlobPattern finds all paths matching a pattern.

func HasExtension

func HasExtension(path string, extensions ...string) bool

HasExtension checks if a file has one of the specified extensions.

func HasPathPrefix

func HasPathPrefix(path, prefix string) bool

HasPathPrefix checks if a path starts with the given prefix.

func HashData

func HashData(data []byte) (string, error)

HashData is a convenience function that uses SHA256 by default.

func HashFile

func HashFile(path string) (string, error)

HashFile is a convenience function that uses SHA256 by default.

func HashString

func HashString(s string) (string, error)

HashString is a convenience function that uses SHA256 by default.

func IsAbsolutePath

func IsAbsolutePath(path string) bool

IsAbsolutePath checks if a path is absolute.

func IsDir

func IsDir(path string) bool

IsDir checks if a path is a directory.

func IsSubPath

func IsSubPath(parent, child string) bool

IsSubPath checks if child is a subdirectory or file under parent.

func IsValidPackageName

func IsValidPackageName(name string) bool

IsValidPackageName checks if a package name is valid.

func IsValidProtoFileName

func IsValidProtoFileName(name string) bool

IsValidProtoFileName checks if a file name is a valid proto file name.

func JoinPath

func JoinPath(elements ...string) string

JoinPath joins path elements into a single path.

func MakeAbsolute

func MakeAbsolute(base, path string) (string, error)

MakeAbsolute makes a path absolute relative to a base directory. If the path is already absolute, it returns it unchanged.

func MakeRelative

func MakeRelative(base, path string) (string, error)

MakeRelative makes a path relative to a base directory. If the path is already relative, it returns it unchanged.

func MatchPattern

func MatchPattern(pattern, name string) (bool, error)

MatchPattern checks if a file name matches a glob pattern.

func NormalizePath

func NormalizePath(path string) (string, error)

NormalizePath normalizes a file path by cleaning it and converting to absolute path.

func ParallelExecute

func ParallelExecute(workerCount int, tasks []Task) (int, []error)

ParallelExecute executes tasks in parallel with the specified worker count. Returns the number of successful tasks and any errors that occurred.

func ReadFile

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

ReadFile reads the entire contents of a file.

func RemoveDir

func RemoveDir(path string) error

RemoveDir removes a directory and all its contents.

func RemoveExtension

func RemoveExtension(path string) string

RemoveExtension removes the extension from a file path.

func SplitExtension

func SplitExtension(path string) (name, ext string)

SplitExtension splits a path into name and extension.

func SplitPath

func SplitPath(path string) (dir, file string)

SplitPath splits a path into directory and file name.

func ToSlash

func ToSlash(path string) string

ToSlash converts path separators to forward slashes (for cross-platform compatibility).

func ValidateFilePattern

func ValidateFilePattern(pattern string) error

ValidateFilePattern validates a file pattern for searching.

func ValidateProtoPackageName

func ValidateProtoPackageName(packageName string) bool

ValidateProtoPackageName validates a proto package name.

func VerifyFileHash

func VerifyFileHash(path, expectedHash string, algo HashAlgorithm) (bool, error)

VerifyFileHash verifies that a file's hash matches the expected hash.

func WriteFile

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

WriteFile writes data to a file, creating it if necessary.

Types

type FileInfo

type FileInfo struct {
	Path     string      // Absolute path to the file
	RelPath  string      // Relative path to the file
	Size     int64       // File size in bytes
	IsDir    bool        // Whether the path is a directory
	ModTime  int64       // Modification time (Unix timestamp)
	FileInfo os.FileInfo // Original FileInfo from os
}

FileInfo contains information about a file.

func FindFiles

func FindFiles(root string, opts FindFilesOptions) ([]FileInfo, error)

FindFiles finds files in a directory matching the given options.

type FileWatcher added in v0.5.0

type FileWatcher struct {
	// contains filtered or unexported fields
}

FileWatcher represents a file system watcher

func NewFileWatcher added in v0.5.0

func NewFileWatcher() (*FileWatcher, error)

NewFileWatcher creates a new file watcher

func (*FileWatcher) Add added in v0.5.0

func (fw *FileWatcher) Add(path string) error

Add adds a path to watch

func (*FileWatcher) Close added in v0.5.0

func (fw *FileWatcher) Close() error

Close closes the watcher

func (*FileWatcher) Errors added in v0.5.0

func (fw *FileWatcher) Errors() <-chan error

Errors returns the channel for watcher errors

func (*FileWatcher) Events added in v0.5.0

func (fw *FileWatcher) Events() <-chan fsnotify.Event

Events returns the channel for file system events

func (*FileWatcher) Remove added in v0.5.0

func (fw *FileWatcher) Remove(path string) error

Remove removes a path from watching

type FindFilesOptions

type FindFilesOptions struct {
	Pattern     string   // File pattern to match (e.g., "*.proto")
	Recursive   bool     // Search recursively in subdirectories
	IncludeDirs bool     // Include directories in results
	Exclude     []string // Patterns to exclude
}

FindFilesOptions contains options for finding files.

type HashAlgorithm

type HashAlgorithm string

HashAlgorithm represents supported hash algorithms.

const (
	MD5    HashAlgorithm = "md5"
	SHA1   HashAlgorithm = "sha1"
	SHA256 HashAlgorithm = "sha256"
	SHA512 HashAlgorithm = "sha512"
)

type Task

type Task func() error

Task represents a unit of work to be executed by a worker pool.

type TaskResult

type TaskResult struct {
	Index int   // Index of the task in the original queue
	Error error // Error if the task failed
}

TaskResult contains the result of a task execution.

func ExecuteAndWait

func ExecuteAndWait(workerCount int, tasks []Task) ([]TaskResult, error)

ExecuteAndWait is a convenience function that creates a pool, executes tasks, and cleans up.

type ValidationResult

type ValidationResult struct {
	Valid    bool     // Whether the validation passed
	Errors   []string // List of validation errors
	Warnings []string // List of validation warnings
}

ValidationResult contains the result of a validation.

func ValidateOutputDir

func ValidateOutputDir(path string) (*ValidationResult, error)

ValidateOutputDir validates an output directory.

func ValidatePath

func ValidatePath(path string) (*ValidationResult, error)

ValidatePath validates a file or directory path.

func ValidateProtoFile

func ValidateProtoFile(path string) (*ValidationResult, error)

ValidateProtoFile validates a proto file.

func (*ValidationResult) AddError

func (vr *ValidationResult) AddError(format string, args ...interface{})

AddError adds an error to the validation result.

func (*ValidationResult) AddWarning

func (vr *ValidationResult) AddWarning(format string, args ...interface{})

AddWarning adds a warning to the validation result.

func (*ValidationResult) HasErrors

func (vr *ValidationResult) HasErrors() bool

HasErrors returns true if there are any errors.

func (*ValidationResult) HasWarnings

func (vr *ValidationResult) HasWarnings() bool

HasWarnings returns true if there are any warnings.

type WorkerPool

type WorkerPool struct {
	// contains filtered or unexported fields
}

WorkerPool manages a pool of workers for concurrent task execution.

func NewWorkerPool

func NewWorkerPool(workerCount int) (*WorkerPool, error)

NewWorkerPool creates a new worker pool with the specified number of workers.

func (*WorkerPool) Close

func (wp *WorkerPool) Close()

Close closes the worker pool (no-op for simplified implementation).

func (*WorkerPool) Execute

func (wp *WorkerPool) Execute(tasks []Task) []TaskResult

Execute executes all tasks and returns the results.

Jump to

Keyboard shortcuts

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