systemdconf

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2021 License: BSD-3-Clause Imports: 11 Imported by: 4

README

systemdconf

Tests Go Reference Go Report Card

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:

go get github.com/sergeymakinen/go-systemdconf/v2

Then import the package into your own code:

import "github.com/sergeymakinen/go-systemdconf/v2"

Example

Marshal
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
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()
if b {
    fmt.Println(b)
}
// Output: Simple firewall
// true

License

BSD 3-Clause

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidBool     = errors.New("invalid systemd boolean")
	ErrInvalidDuration = errors.New("invalid systemd duration")
)

Functions

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration returns a string representing the duration as defined in systemd.time (time spans)

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal encodes the systemd configuration file of v.

1) If the value v is not a struct (or pointer to struct), Marshal returns an error. Marshal encodes sections: each exported struct field becomes a section, using the field name as the section name, unless the field is omitted, and the field value as the section entries for the next step.

If the struct embeds the File struct, any unknown or extra section will be extracted from it.

2) If the field value from the previous step is a struct (or pointer to struct), Marshal encodes entries from it: each exported struct field becomes an entry, using the field name as the section key name, unless the field is omitted, and the field value as the section value for the next step.

If the struct embeds the Section struct, any unknown or extra value will be extracted from it.

3) If the section value implements the Marshaler interface and is not a nil pointer, Marshal calls its MarshalSystemd method to produce a Value. If no MarshalSystemd method is present but the value implements encoding.TextMarshaler instead, Marshal calls its MarshalText method and uses the result as a single value. If no MarshalText method is present, the value must be:

  • boolean
  • slice of booleans
  • string
  • slice of strings
  • time.Duration
  • slice of time.Durations
  • pointer to type above

The marshalling of each struct field can be customized by the format string stored under the "systemd" key in the struct field's tag. The format string gives the name of the field. If the field tag is "-", the field is always omitted.

Anonymous struct fields are marshaled as if their inner exported fields were fields in the outer struct, subject to the usual Go visibility rules.

Pointer values resolve as the value pointed to.

Interface values resolve as the value contained in the interface

Example
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

func ParseBool

func ParseBool(s string) (bool, error)

ParseBool returns the boolean value represented by the string as defined in systemd.syntax

func ParseDuration

func ParseDuration(s string, def time.Duration) (time.Duration, error)

ParseDuration returns the time.Duration value represented by the string as defined in systemd.time (time spans)

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal decodes the systemd configuration file in data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an error.

Unmarshal uses the inverse of the rules that Marshal uses, allocating maps, slices, and pointers as necessary.

To unmarshal data into a pointer, Unmarshal unmarshals data into the value pointed at by the pointer. If the pointer is nil, Unmarshal allocates a new value for it to point to

Example
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()
if b {
	fmt.Println(b)
}
Output:

Simple firewall
true

Types

type Entry

type Entry struct {
	Key   string // Key name
	Value Value  // Entry value
}

Entry represents a key-value configuration entry used by File and Section

type FieldConflictError

type FieldConflictError struct {
	Struct1, Struct2 reflect.Type
	Field1, Field2   string
	Tag1, Tag2       string
}

FieldConflictError represents an error when marshalling/unmarshalling a struct is not possible because field names conflict

func (*FieldConflictError) Error

func (e *FieldConflictError) Error() string

type FieldError

type FieldError struct {
	Struct reflect.Type // Type of the struct containing the field
	Field  string       // Name of the field
	Type   reflect.Type // Type of the field
	Err    error        // The actual error
}

FieldError represents an error when marshalling/unmarshalling struct fields

func (*FieldError) Error

func (e *FieldError) Error() string

type File

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

File embeds unknown and extra sections of a systemd configuration file

func (*File) Extra

func (f *File) Extra() []*Section

Extra returns extra sections of a systemd configuration file

func (*File) Unknown

func (f *File) Unknown() []*Section

Unknown returns unknown sections of a systemd configuration file

type Marshaler

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

Marshaler is the interface implemented by types that can marshal themselves into a systemd Value

type Section

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

Section embeds unknown and extra entries of a systemd configuration file section

func (*Section) Extra

func (s *Section) Extra() []*Entry

Extra returns extra entries of the section

func (*Section) Name

func (s *Section) Name() string

Name returns a section name

func (*Section) Unknown

func (s *Section) Unknown() []*Entry

Unknown returns unknown entries of the section

type Unmarshaler

type Unmarshaler interface {
	UnmarshalSystemd(value Value) error
}

Unmarshaler is the interface implemented by types that can unmarshal a systemd Value of themselves. UnmarshalSystemd must copy the value if it wishes to retain the value after returning

type Value

type Value []string

Value represents a generic systemd value as list of strings

func (Value) Bool

func (v Value) Bool() (bool, error)

Bool returns a first defined value parsed as a boolean

func (Value) BoolSlice

func (v Value) BoolSlice() ([]bool, error)

BoolSlice returns a slice of defined values parsed as booleans

func (Value) Duration

func (v Value) Duration() (time.Duration, error)

Duration returns a first defined value parsed as a time.Duration

func (Value) DurationSlice

func (v Value) DurationSlice() ([]time.Duration, error)

DurationSlice returns a slice of defined values parsed as time.Durations

func (Value) String

func (v Value) String() string

String returns a first defined value, or an empty string if the value does not exist

Directories

Path Synopsis
Package ast contains a parser and a serializer for systemd configuration files through AST trees.
Package ast contains a parser and a serializer for systemd configuration files through AST trees.
cmd
Package conf provides mappings for the following categories: "Program configuration options", "Miscellaneous options and directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package conf provides mappings for the following categories: "Program configuration options", "Miscellaneous options and directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package home provides mappings for the following categories: "Home Area/User Account directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package home provides mappings for the following categories: "Home Area/User Account directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package network provides mappings for the following categories: "Network directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package network provides mappings for the following categories: "Network directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package nspawn provides mappings for the following categories: "systemd.nspawn directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package nspawn provides mappings for the following categories: "systemd.nspawn directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package unit provides mappings for the following categories: "Unit directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).
Package unit provides mappings for the following categories: "Unit directives" (see https://www.freedesktop.org/software/systemd/man/systemd.directives.html).

Jump to

Keyboard shortcuts

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