file

package
v0.7.9 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

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

func WithDelimiter(delim string) Option

WithDelimiter sets the delimiter used to split entries in the file. Default is newline ("\n").

func WithKVDelimiter

func WithKVDelimiter(kvDelim string) Option

WithKVDelimiter sets the key-value delimiter used in GetMap. Default is "=".

func WithMaxSize

func WithMaxSize(size int) Option

WithMaxSize sets the maximum size (in bytes) of the file to be parsed. Default is 1MB.

func WithSkipComments

func WithSkipComments(skip bool) Option

WithSkipComments sets whether to skip comment lines in the file. Default is true.

func WithSkipEmptyValues

func WithSkipEmptyValues(skip bool) Option

WithSkipEmptyValues sets whether to skip empty values when parsing the file. Default is false.

func WithVDefault

func WithVDefault(vDefault string) Option

WithVDefault sets the default value to use when a key has no associated value. Default is an empty string.

func WithVTrimChars

func WithVTrimChars(trimChars string) Option

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

func NewParser(opts ...Option) *Parser

NewParser creates a new file parser with the provided options. Default settings: newline delimiter ("\n"), 1MB max file size.

func (*Parser) GetLines

func (p *Parser) GetLines(path string) ([]string, error)

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

func (p *Parser) GetMap(path string) (map[string]string, error)

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.

Jump to

Keyboard shortcuts

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