-
Notifications
You must be signed in to change notification settings - Fork 1
/
section.go
53 lines (46 loc) · 1.24 KB
/
section.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package systemdconf
import "io"
// Section is a systemd configuration section.
type Section interface {
SectionName() string
Directives() Directives
}
// WriteSection marshals the section configuration and writes it to w.
func WriteSection(w io.Writer, s Section) (n int, err error) {
// Write the section header
if written, err := io.WriteString(w, "["+s.SectionName()+"]\n"); err != nil {
return n + written, err
} else {
n += written
}
// Write each of the unit directives
for _, directive := range s.Directives() {
if written, err := io.WriteString(w, directive.String()+"\n"); err != nil {
return n + written, err
} else {
n += written
}
}
return n, nil
}
// WriteSections marshals a set of configuration sections and writes their
// configuration to w.
func WriteSections(w io.Writer, sections ...Section) (n int, err error) {
for i, section := range sections {
// Write an extra newline between sections
if i > 0 {
if written, err := io.WriteString(w, "\n"); err != nil {
return n + written, err
} else {
n += written
}
}
// Ask the section to write its contents
if written, err := WriteSection(w, section); err != nil {
return n + written, err
} else {
n += written
}
}
return n, nil
}