forked from coreos/fleet
/
file.go
104 lines (85 loc) · 2.29 KB
/
file.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package unit
import (
"fmt"
"strings"
)
type SystemdUnitFile struct {
Contents map[string]map[string]string
}
func (self *SystemdUnitFile) GetSection(section string) map[string]string {
result, ok := self.Contents[section]
if ok {
return result
} else {
return make(map[string]string, 0)
}
}
func (self *SystemdUnitFile) SetField(section string, key string, value string) {
_, ok := self.Contents[section]
if !ok {
self.Contents[section] = make(map[string]string, 0)
}
self.Contents[section][key] = value
}
func (self *SystemdUnitFile) Requirements() map[string][]string {
requirements := make(map[string][]string, 0)
for key, value := range self.GetSection("X-Fleet") {
if !strings.HasPrefix(key, "X-") {
continue
}
// Strip off leading X-
key = key[2:]
if _, ok := requirements[key]; !ok {
requirements[key] = make([]string, 0)
}
requirements[key] = append(requirements[key], value)
}
return requirements
}
func (self *SystemdUnitFile) String() string {
var serialized string
for section, keyMap := range self.Contents {
serialized += fmt.Sprintf("[%s]\n", section)
for key, value := range keyMap {
serialized += fmt.Sprintf("%s=%s\n", key, value)
}
serialized += "\n"
}
return serialized
}
func NewSystemdUnitFile(Contents string) *SystemdUnitFile {
parsed := deserializeUnitFile(Contents)
return &SystemdUnitFile{parsed}
}
// This is dangerously simple and should be rewritten to match the spec
func deserializeUnitFile(contents string) map[string]map[string]string {
sections := make(map[string]map[string]string, 0)
var section string
for _, line := range strings.Split(contents, "\n") {
// Ignore commented-out lines
if strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
continue
}
line = strings.Trim(line, " ")
// Ignore blank lines
if len(line) == 0 {
continue
}
// Check for section
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
section = line[1:len(line)-1]
sections[section] = make(map[string]string, 0)
continue
}
// Check for key=value
if strings.ContainsAny(line, "=") {
parts := strings.SplitN(line, "=", 2)
key := strings.Trim(parts[0], " ")
value := strings.Trim(parts[1], " ")
if len(section) > 0 {
sections[section][key] = value
}
}
}
return sections
}