systemd-file.gopack

module
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

README

Systemd unit file parser

This is a parser library to parse Systemd unit files, which can be any unit file or Podman Quadlet or any configuration file that wants to follow this pattern.

The interface is very similar than encoding/json in the standard library.

Install

Run the following command:

go get -u code.thinkaboutit.tech/pandora/systemd-file.gopack

Examples to use

Parse existing file with specific struct

If we had the following unit file:

[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)

[Automount]
Where=/var/mnt/data

[X-Other]
Property=Value is here
Property=Another value is here

We can parse it from Go:

type AutoMountFile struct {
    Unit      AutoMountUnit
    Automount AutoMount
    XOther    XOther `systemd:"X-Other"`
}

type AutoMountUnit struct {
    SourcePath    string
    Documentation string
}

type AutoMount struct {
    Where string
}

type XOther struct {
    Property []string
}

func main() {
    input, err := os.ReadFile("path/to/test.automount")
    if err != nil {
        fmt.Println(err.Error())
        os.Exit(1)
    }

    var file AutoMountFile
    err = systemd.Unmarshal([]byte(input), &file)
    if err != nil {
        fmt.Println(err.Error())
        os.Exit(1)
    }
    fmt.Printf("%+v\n", file)
}
Read the whole file
input = `
[Setup]
TargetRegistry=code.thinkaboutit.tech
ProdOwner=pandora
StagingOwner=staging

[WolifBase]
Source=cgr.dev/chainguard/wolfi-base:latest@sha256:195c8853f52e0463d217aa8bdf14f6730ae97305de399559d86b76403f9528af

[DebianBase]
Source=dhi.io/debian-base:trixie@sha256:5a539a75a33da9019e8f667c3e7e3e1eb00b52a69cace95b0225f8134aee17b3
TargetName=debian-base

[DebianDev]
Source=dhi.io/debian-base:trixie-dev@sha256:57c88d9180b30314a04650426af8d4301c5c9738e4c5672a1db99a03f6a54721
TargetName=debian-dev
`

var result map[string]map[string]any
err = systemd.Unmarshal([]byte(input), &result)
if err != nil {
    fmt.Println(err.Error())
    os.Exit(1)
}

for sName, sValue := range result {
    for k, v := range sValue {
        fmt.Printf("%s.%s ==> %v\n", sName, k, v)
    }
}
Convert map to systemd file

The library can parse map or struct to string in Systemd file format.

data := map[string]any{
    "Service": map[string]any{
        "Restart":    "always",
        "TimeoutSec": 30,
        "Environment": []string{
            "ENV=prod",
            "DEBUG=false",
        },
    },
}

marshaled, err := systemd.Marshal(data)
require.NoError(t, err)

expected := `[Service]
Environment=ENV=prod
Environment=DEBUG=false
Restart=always
TimeoutSec=30
`
assert.Equal(t, expected, string(marshaled))

Directories

Path Synopsis
encoding
systemd
Package systemd is a systemd unit file parser.
Package systemd is a systemd unit file parser.
Package testutils is a package that holds utilities for unit tests.
Package testutils is a package that holds utilities for unit tests.

Jump to

Keyboard shortcuts

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