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 ¶
- Variables
- func FormatDuration(d time.Duration) string
- func Marshal(v interface{}) ([]byte, error)
- func ParseBool(s string) (bool, error)
- func ParseDuration(s string, def time.Duration) (time.Duration, error)
- func Unmarshal(data []byte, v interface{}) error
- type Entry
- type FieldConflictError
- type FieldError
- type File
- type Marshaler
- type Section
- type Unmarshaler
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidBool = errors.New("invalid systemd boolean") ErrInvalidDuration = errors.New("invalid systemd duration") )
Functions ¶
func FormatDuration ¶
FormatDuration returns a string representing the duration as defined in systemd.time (time spans)
func Marshal ¶
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 ¶
ParseBool returns the boolean value represented by the string as defined in systemd.syntax
func ParseDuration ¶
ParseDuration returns the time.Duration value represented by the string as defined in systemd.time (time spans)
func Unmarshal ¶
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 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
type Marshaler ¶
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
type Unmarshaler ¶
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) DurationSlice ¶
DurationSlice returns a slice of defined values parsed as time.Durations
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). |