Documentation
¶
Overview ¶
Package file provides utilities for reading files from the filesystem.
This package wraps standard file I/O operations with error handling conventions used throughout the collector framework. It provides a simple interface for reading file contents as strings.
Usage ¶
Read a single file:
content, err := file.ReadFile("/etc/os-release")
if err != nil {
// Handle error
}
fmt.Println(content)
The function automatically handles:
- File opening and closing
- Content reading
- Error wrapping with context
Error Handling ¶
Errors are wrapped with descriptive context:
content, err := file.ReadFile("/nonexistent")
// Error: failed to open file "/nonexistent": no such file or directory
Common error scenarios:
- File does not exist (os.ErrNotExist)
- Permission denied (os.ErrPermission)
- I/O errors during read
Use in Collectors ¶
Collectors use this package for reading configuration files:
content, err := file.ReadFile("/etc/default/grub")
if err != nil {
return nil, fmt.Errorf("failed to read GRUB config: %w", err)
}
// Parse content...
Thread Safety ¶
Functions in this package are thread-safe and can be called concurrently from multiple collectors.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*Parser)
Options for configuring the Parser.
func WithDelimiter ¶
WithDelimiter sets the delimiter used to split entries in the file. Default is newline ("\n").
func WithKVDelimiter ¶
WithKVDelimiter sets the key-value delimiter used in GetMap. Default is "=".
func WithMaxSize ¶
WithMaxSize sets the maximum size (in bytes) of the file to be parsed. Default is 1MB.
func WithSkipComments ¶
WithSkipComments sets whether to skip comment lines in the file. Default is true.
func WithSkipEmptyValues ¶
WithSkipEmptyValues sets whether to skip empty values when parsing the file. Default is false.
func WithVDefault ¶
WithVDefault sets the default value to use when a key has no associated value. Default is an empty string.
func WithVTrimChars ¶
WithVTrimChars sets characters to trim from values in GetMap. Default is no trimming.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser parses configuration files with customizable settings.
func NewParser ¶
NewParser creates a new file parser with the provided options. Default settings: newline delimiter ("\n"), 1MB max file size.
func (*Parser) GetLines ¶
GetLines reads the file at the given path and splits its content into lines based on the configured delimiter. It returns a slice of non-empty lines. An error is returned if the file cannot be read, exceeds the maximum size, or contains invalid UTF-8 content.
func (*Parser) GetMap ¶
GetMap reads the file at the given path and parses its content into a map. Each line is split into key-value pairs using the specified kvDel delimiter. If a line does not contain the delimiter, the value is set to vDefault. Returns an error if the file cannot be read or parsed.