https://github.com/sergeymakinen/go-systemdconf
systemd configuration file parser/serializer
https://github.com/sergeymakinen/go-systemdconf
go golang marshal parser serializer systemd systemd-configuration systemd-networkd systemd-unit unit unmarshal
Last synced: over 1 year ago
JSON representation
systemd configuration file parser/serializer
- Host: GitHub
- URL: https://github.com/sergeymakinen/go-systemdconf
- Owner: sergeymakinen
- License: bsd-3-clause
- Created: 2020-01-02T00:10:23.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-04-19T12:17:02.000Z (about 2 years ago)
- Last Synced: 2025-03-15T06:52:35.230Z (over 1 year ago)
- Topics: go, golang, marshal, parser, serializer, systemd, systemd-configuration, systemd-networkd, systemd-unit, unit, unmarshal
- Language: Go
- Homepage:
- Size: 286 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# systemdconf
[](https://github.com/sergeymakinen/go-systemdconf/actions?query=workflow%3Atests)
[](https://pkg.go.dev/github.com/sergeymakinen/go-systemdconf/v2)
[](https://goreportcard.com/report/github.com/sergeymakinen/go-systemdconf)
[](https://codecov.io/gh/sergeymakinen/go-systemdconf)
Package systemdconf implements encoding and decoding of systemd configuration files
as defined by systemd.syntax (see https://www.freedesktop.org/software/systemd/man/systemd.syntax.html for details).
The mapping between systemd and Go is described
in the documentation for the Marshal and Unmarshal functions
## Installation
Use go get:
```bash
go get github.com/sergeymakinen/go-systemdconf/v2
```
Then import the package into your own code:
```go
import "github.com/sergeymakinen/go-systemdconf/v2"
```
## Example
### Marshal
```go
service := unit.ServiceFile{
Unit: unit.UnitSection{
Description: systemdconf.Value{"Simple firewall"},
},
Service: unit.ServiceSection{
Type: systemdconf.Value{"oneshot"},
RemainAfterExit: systemdconf.Value{"yes"},
ExecStart: systemdconf.Value{
"/usr/local/sbin/simple-firewall-start1",
"/usr/local/sbin/simple-firewall-start2",
},
ExecStop: systemdconf.Value{"/usr/local/sbin/simple-firewall-stop"},
},
Install: unit.InstallSection{
WantedBy: systemdconf.Value{"multi-user.target"},
},
}
b, _ := systemdconf.Marshal(service)
fmt.Println(string(b))
// Output:
// [Unit]
// Description=Simple firewall
//
// [Service]
// Type=oneshot
// RemainAfterExit=yes
// ExecStart=/usr/local/sbin/simple-firewall-start1
// ExecStart=/usr/local/sbin/simple-firewall-start2
// ExecStop=/usr/local/sbin/simple-firewall-stop
//
// [Install]
// WantedBy=multi-user.target
```
### Unmarshal
```go
file := `[Unit]
Description=Simple firewall
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/simple-firewall-start
ExecStop=/usr/local/sbin/simple-firewall-stop
[Install]
WantedBy=multi-user.target`
var service unit.ServiceFile
systemdconf.Unmarshal([]byte(file), &service)
fmt.Println(service.Unit.Description)
b, _ := service.Service.RemainAfterExit.Bool()
fmt.Println(b)
// Output:
// Simple firewall
// true
```
## License
BSD 3-Clause