Skip to content

Latest commit

 

History

History
84 lines (57 loc) · 3.08 KB

File metadata and controls

84 lines (57 loc) · 3.08 KB

Systemd parser

Round-trip systemd configuration file parser.

Usage / documentation:

  • parse(input, options):
import { parse } from "systemd-parser"

const input = "[Section]\nKey=Value"
const output = parse(input, {
  // A function that executes upon successful assignment.
  // If it returns something, it'll be used as value parser, changing the result
  func: (section: string, key: string, value: string) => {};
  warnFunc: (message: string, lineNum: number, severity: "hint"|"info"|"warning"|"error") => {}; // Ran on warnings

  logWarns: false;  // Log parsing warnings to the console
  strict: false;  // Make parsing fail on wrong syntax

  includePrefixed: false; // default false, include keys prefixed with X-
  allowedKeys: { Section: [ "Key1", "Key2" ] }; // object of allowed sections and keys. Prefix '-' to ignore a key
  includeDisallowed: false; // include keys not allowed by allowedKeys
})

// output: { "Section": { "Key": [ "Value" ] } }
  • generate(input):
import { generate } from "systemd-parser";

const input = { Section: { Key: ["Value"] } };
const output = generate(input);
// [Section] Key=Value
  • parseDocument(input):
import { parseDocument } from "systemd-parser"

const doc = parseDocument("[Section]\nKey=Value")
// doc.output: { "Section": { "Key": [ "Value" ] } }

doc.add('Section', 'Key', 'ThirdValue')
doc.add('Section', 'Key', 'SecondValue', 1) // insert
// doc.content: [Section] Key=Value Key=SecondValue Key=ThirdValue

doc.set('Section', 'Key' 'OtherValue', 1)
// doc.content: [Section] Key=Value Key=OtherValue Key=ThirdValue

doc.remove('Section', 'Key', 1)
// doc.content: [Section] Key=Value Key=ThirdValue

doc.set('Section', 'Key', 'Value') // resets all instances
// doc.content: [Section] Key=Value

doc.remove('Setion', 'Key', 'Value') // removes all instances
// doc.content: ""

Systemd ini format

Basics of the file format are listed at systemd.syntax(7). For more, see the references below.

References


License

This project uses the MIT license. It uses some of the logic from Red Hat for parsing systemd ini files, which is licensed under both LGPL-2.1+ [1] and Apache-2.0 [2]. For more details, see NOTICE