Documentation
¶
Overview ¶
Package util provides common utility functions and types for ClusterCockpit applications. This file implements a file system watcher for monitoring file changes.
Index ¶
- func AddListener(path string, l Listener)
- func CheckFileExists(filePath string) bool
- func CompressFile(fileIn string, fileOut string) error
- func Contains[T comparable](items []T, item T) bool
- func CopyDir(src string, dst string) (err error)
- func CopyFile(src, dst string) (err error)
- func DiskUsage(dirpath string) float64
- func FsWatcherShutdown()
- func GetFilecount(path string) int
- func GetFilesize(filePath string) int64
- func Max[T cmp.Ordered](a, b T) T
- func Mean(input []float64) (float64, error)
- func Median(input []float64) (median float64, err error)
- func Min[T cmp.Ordered](a, b T) T
- func UncompressFile(fileIn string, fileOut string) error
- type Listener
- type Selector
- type SelectorElement
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddListener ¶
AddListener registers a new file system watcher for the specified path. The watcher is initialized on the first call to AddListener. The listener will be notified of file system events matching its EventMatch criteria.
func CheckFileExists ¶
CheckFileExists checks if a file or directory exists at the given path. Returns true if the file exists, false otherwise.
func CompressFile ¶
CompressFile compresses a file using gzip compression. It reads the input file (fileIn), creates a gzip-compressed output file (fileOut), and removes the original file upon successful compression. Returns an error if any operation fails.
func Contains ¶
func Contains[T comparable](items []T, item T) bool
Contains checks if an item exists in a slice of comparable items. It is a generic wrapper around slices.Contains that works with any comparable type T.
func CopyDir ¶
CopyDir recursively copies a directory from src to dst, preserving file modes. It creates the destination directory with the same permissions as the source, and recursively copies all files and subdirectories. Symbolic links are skipped during the copy operation. Returns an error if the source is not a directory, if the destination already exists, or if any file operation fails.
func CopyFile ¶
CopyFile copies a file from src to dst, preserving the file mode. It creates the destination file, copies the content, syncs to disk, and sets the same permissions as the source file. Returns an error if any operation fails.
func DiskUsage ¶
DiskUsage calculates the total disk usage of a directory in megabytes (MB). It sums up the sizes of all files in the specified directory (non-recursive) and returns the result in MB (multiplied by 1e-6). Returns 0 if the directory cannot be opened or read.
func FsWatcherShutdown ¶
func FsWatcherShutdown()
FsWatcherShutdown closes the file system watcher. This should be called during application shutdown to clean up resources.
func GetFilecount ¶
GetFilecount returns the number of entries (files and directories) in a directory. Returns 0 if the directory cannot be read.
func GetFilesize ¶
GetFilesize returns the size of a file in bytes. Returns 0 if the file cannot be accessed or does not exist.
func Mean ¶
Mean calculates the arithmetic mean (average) of a float64 slice. Returns NaN and an error if the input slice is empty.
func Median ¶
Median calculates the median value of a float64 slice. For even-length slices, it returns the mean of the two middle values. For odd-length slices, it returns the middle value. Returns NaN and an error if the input slice is empty.
func UncompressFile ¶
UncompressFile decompresses a gzip-compressed file. It reads the gzip-compressed input file (fileIn), creates an uncompressed output file (fileOut), and removes the compressed file upon successful decompression. Returns an error if any operation fails.
Types ¶
type Listener ¶
Listener is an interface for file system event callbacks. Implementations should define what events to match and how to respond to them.
type Selector ¶
type Selector []SelectorElement
Selector is a slice of SelectorElements used for matching and filtering.
type SelectorElement ¶
SelectorElement represents a selector that can be either a single string, an array of strings, or a wildcard. It supports special JSON marshaling/unmarshaling behavior: - A single string (e.g., "value") is stored in the String field - An array of strings (e.g., ["val1", "val2"]) is stored in the Group field - The wildcard "*" is represented by setting Any to true
func (*SelectorElement) MarshalJSON ¶
func (se *SelectorElement) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for SelectorElement. It converts the selector back to JSON: - Any=true becomes "*" - String field becomes a JSON string - Group field becomes a JSON array
func (*SelectorElement) UnmarshalJSON ¶
func (se *SelectorElement) UnmarshalJSON(input []byte) error
UnmarshalJSON implements json.Unmarshaler for SelectorElement. It handles three formats: - A JSON string (converted to String field, or Any if "*") - A JSON array (converted to Group field) - Any other format returns an error