systemd

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package systemd is a systemd unit file parser.

Index

Constants

View Source
const (
	TokenTypeComment  = "comment"   // For example: '# This is a comment'
	TokenTypeSection  = "section"   // For example: '[Unit]'
	TokenTypeKeyword  = "keyword"   // Text before '='
	TokenTypeValue    = "value"     // Text after '='
	TokenTypeAssign   = "assign"    // The '=' character
	TokenTypeContSign = "cont_sign" // The '\' at the end of the line
	TokenTypeEOF      = "eof"       // End of file
)

Variables

This section is empty.

Functions

func IsLetter

func IsLetter(ch rune) bool

func Marshal

func Marshal(v any) ([]byte, error)

Marshal serializes v into systemd unit file format bytes.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal parses systemd unit file bytes into the value pointed to by v.

func Utf16Len

func Utf16Len(s string) uint32

Utf16Len returns the number of UTF-16 code units in a string. This is what VS Code and LSP expect for 'length' and 'charPos'. The simple builtin len() does not work.

Types

type AssignNode

type AssignNode struct {
	StartPos  NodePosition
	EndPos    NodePosition
	Name      *string
	Value     *ValueNode
	Documents []*CommentNode
}

AssignNode represents a key-value pair like 'Image=foo.image', altogether with its document comment.

func (*AssignNode) String

func (a *AssignNode) String() string

type CommentNode

type CommentNode struct {
	StartPos NodePosition
	EndPos   NodePosition
	Text     *string
}

CommentNode represent the comment nodes in the file.

func (*CommentNode) String

func (d *CommentNode) String() string

type FileNode

type FileNode struct {
	Documents []*CommentNode
	Sections  []*SectionNode
}

FileNode represents the whole Quadlet file.

func (*FileNode) FindToken

func (q *FileNode) FindToken(position NodePosition) FindTokenOutput

FindToken method receives two parameter a line and a position number, then searches in the Quadlet and return with the token where the position is.

func (*FileNode) String

func (q *FileNode) String() string

type FindTokenOutput

type FindTokenOutput struct {
	CurrentNode Node
	ParentNodes []Node
}

type Lexer

type Lexer struct {
	Input string // Text that lexer process

	Tokens []Token // The processed tokens from the 'Input', this is the result
	// contains filtered or unexported fields
}

func NewLexer

func NewLexer(input string) *Lexer

func (*Lexer) LastToken

func (l *Lexer) LastToken() *Token

func (*Lexer) Run

func (l *Lexer) Run()

Run method run through the read file and get the tokens. Function ends when reach the end of the file.

type Marshaler

type Marshaler interface {
	MarshalSystemd() (string, error)
}

Marshaler allows custom types to control their own systemd representation.

type Node

type Node interface {
	String() string
}

type NodePosition

type NodePosition = TokenPosition

type Parser

type Parser struct {
	File        *FileNode     // The parsed output
	Errors      []ParserError // If there is any error during reading record it
	Path        string        // Location of the file
	LexerTokens []Token       // Result of the lexer
	// contains filtered or unexported fields
}

func NewParser

func NewParser(path string) Parser

func NewParserFromMemory

func NewParserFromMemory(path, content string) Parser

func (*Parser) Run

func (p *Parser) Run()

Run method iterate over the tokens and create an AST.

type ParserError

type ParserError struct {
	Text  string
	Token *Token
}

type SectionNode

type SectionNode struct {
	StartPos    NodePosition
	EndPos      NodePosition
	Text        *string
	Assignments []*AssignNode
	Documents   []*CommentNode
}

SectionNode represent the sections, like '[Unit]', in the file.

func (*SectionNode) String

func (s *SectionNode) String() string

type Token

type Token struct {
	StartPos TokenPosition // Where the token start
	EndPos   TokenPosition // Where the token ends
	Position uint32        // Start index of token in the input
	Length   uint32        // Length of the token
	Type     TokenType     // What kind of token it is
	Text     string        // Content of the token
}

func NewToken

func NewToken(
	startLineNumber, startInlinePosition uint32,
	endLineNumber, endInlinePosition uint32,
	position uint32,
	length uint32,
	tokenType TokenType,
	text string,
) Token

type TokenPosition

type TokenPosition struct {
	LineNumber uint32
	Position   uint32
}

type TokenType

type TokenType string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalSystemd(value string) error
}

Unmarshaler allows custom types to control their own systemd value decoding.

type ValueNode

type ValueNode struct {
	StartPos NodePosition
	EndPos   NodePosition
	Value    *string
}

func (*ValueNode) String

func (v *ValueNode) String() string

Jump to

Keyboard shortcuts

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