picfg

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Package picfg models configuration files used by the Raspberry Pi bootloader

Implementation is based on the official documentation https://www.raspberrypi.org/documentation/configuration/config-txt/

A tl;dr; variant of the documentation is:

INI-style [section] key=value file with one exception when the key is
"initramfs" then the value separator is " " instead of "=". Sections
are stateful but this does not affect parsing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigTxt

type ConfigTxt struct {
	Comments []string // Comments or white-space lines without trailing newlines.
	Sections []Section
}

ConfigTxt represents the contents of a config.txt file.

The file is represented as a slice of sections. Each section starts with an optional conditional filter and is followed by a slice of parameters.

The representation can optionally preserve and restore comments. This makes round-trips less jarring, as comments are preserved and mostly retain their locations even when edited programmatically.

func (*ConfigTxt) Equal added in v1.4.0

func (cfg *ConfigTxt) Equal(other *ConfigTxt) bool

Equal returns true if two configuration files are identical.

Equal checks that all the sections define the same parameters, in the same order and on the same line number. Equal also compares comments, even though if they have no semantic value.

func (ConfigTxt) MarshalText

func (cfg ConfigTxt) MarshalText() ([]byte, error)

MarshalText implements TextMarshaler.

Comments are marshalled if present.

func (*ConfigTxt) UnmarshalText

func (cfg *ConfigTxt) UnmarshalText(text []byte) error

UnmarshalText implements TextUnmarshaler.

Parsed comments discarded. Create a new decoder and use PreserveComments before decoding to preserve them.

type Decoder

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

Decoder reads and decodes config.txt from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a decoder that reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode(cfg *ConfigTxt) error

Decode reads the complete config.txt file and stores it in cfg.

func (*Decoder) PreserveComments added in v1.1.0

func (dec *Decoder) PreserveComments()

PreserveComments makes the decoder preserve comments.

Comments are associated with files, sections, parameters.

IMPORTANT: Precise association of comments may change over time. All comments are parsed and preserved but the exact logic if a comment is associated to the preceding or following entity is not covered by API stability guarantees.

type Encoder

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

Encoder encodes and writes config.txt from an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns an encoder that writes to w.

func (*Encoder) Encode

func (enc *Encoder) Encode(cfg ConfigTxt) error

Encode writes a representation of cfg to the stream.

type Location

type Location struct {
	FileName string
	LineNo   int

	// SeqNum is a sequence number, greater than zero and monotonically
	// increasing as the decoder reads files. Unlike LineNo, when an "include"
	// directive is read, the sequence number keeps increasing.
	//
	// Sequence number can be used to determine which of a pair of
	// parameters was read earlier.
	SeqNum uint
}

Location pairs file name with a line number.

The decoder provides location for each section and parameter. The encoder ignores those values.

func (*Location) String

func (loc *Location) String() string

String returns the location as a file-name:line-number string.

type Parameter

type Parameter struct {
	Comments []string // Comments or white-space lines without trailing newlines.
	Name     string
	Value    string
	Location Location
}

Parameter represents a single configurable entry.

Most parameters are represented as a key=value pair. Some keys can be assigned to multiple times for a stacking effect. This is not represented in the parser as the inner semantics of the firmware is not precisely documented.

As an exception, the "initramfs" key is not using "key=value" syntax and instead is using "key value", with a space instead of the equals sign.

func (*Parameter) Equal added in v1.4.0

func (p *Parameter) Equal(other *Parameter) bool

Equal returns true if the two paramters have the same comments, name, value and location.

func (Parameter) MarshalText

func (p Parameter) MarshalText() ([]byte, error)

MarshalText implements TextMarshaler.

Comments do not undergo marshaling.

func (Parameter) String

func (p Parameter) String() string

func (*Parameter) UnmarshalText

func (p *Parameter) UnmarshalText(text []byte) error

UnmarshalText implements TextUnmarshaler.

Comments do not undergo marshaling.

type ParameterSyntaxError

type ParameterSyntaxError struct {
	Text []byte
}

ParameterSyntaxError reports inability to parse a key-value parameter.

func (*ParameterSyntaxError) Error

func (err *ParameterSyntaxError) Error() string

Error implements the error interface.

type ParseError

type ParseError struct {
	LineNo int
	Err    error
}

ParseError describes a parser error.

func (*ParseError) Error

func (pe *ParseError) Error() string

Error implements error.

func (*ParseError) Unwrap added in v1.4.0

func (pe *ParseError) Unwrap() error

Unwrap returns the error that caused parsing to fail.

type Section

type Section struct {
	Comments   []string // Comments or white-space lines without trailing newlines.
	Filter     string   // Filter is either empty or matches /\[^]+\]/.
	Parameters []Parameter
	Location   Location
}

Section scopes parameters to an optional conditional filter.

The way a conditional filter impacts boot selection is stateful and depends on the filters in prior sections. To understand the effective set of filters that apply, use the StateTracker from the condfilter package. The parser does not offer this functionality.

func (*Section) Equal added in v1.4.0

func (sect *Section) Equal(other *Section) bool

Equal returns true if the two sections have the same parameters, comment, location and any optional conditional filter.

Directories

Path Synopsis
Package condfilter implements conditional filters as distinct types.
Package condfilter implements conditional filters as distinct types.

Jump to

Keyboard shortcuts

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