forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize.go
51 lines (39 loc) · 885 Bytes
/
serialize.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
package unit
import (
"bytes"
"io"
)
// Serialize encodes all of the given UnitOption objects into a unit file
func Serialize(opts []*UnitOption) io.Reader {
var buf bytes.Buffer
if len(opts) == 0 {
return &buf
}
curSection := opts[0].Section
writeSectionHeader(&buf, curSection)
writeNewline(&buf)
for _, opt := range opts {
if opt.Section != curSection {
curSection = opt.Section
writeNewline(&buf)
writeSectionHeader(&buf, curSection)
writeNewline(&buf)
}
writeOption(&buf, opt)
writeNewline(&buf)
}
return &buf
}
func writeNewline(buf *bytes.Buffer) {
buf.WriteRune('\n')
}
func writeSectionHeader(buf *bytes.Buffer, section string) {
buf.WriteRune('[')
buf.WriteString(section)
buf.WriteRune(']')
}
func writeOption(buf *bytes.Buffer, opt *UnitOption) {
buf.WriteString(opt.Name)
buf.WriteRune('=')
buf.WriteString(opt.Value)
}