Skip to content

Commit 9bb6d77

Browse files
committed
unit: try to avoid unit file modification
1 parent 7b2428f commit 9bb6d77

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

unit/deserialize.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,16 @@ func (l *lexer) lexNextSectionOrOptionFunc(section string) lexStep {
173173
if err != nil {
174174
if err == io.EOF {
175175
err = nil
176+
l.optchan <- &UnitOption{Section: section, Name: "", Value: ""}
176177
}
177178
return nil, err
178179
}
179180

180181
if unicode.IsSpace(r) {
182+
l.optchan <- &UnitOption{Section: section, Name: "", Value: fmt.Sprintf("%c", r)}
181183
return l.lexNextSectionOrOptionFunc(section), nil
182184
} else if r == '[' {
185+
l.optchan <- &UnitOption{Section: section, Name: "", Value: ""}
183186
return l.lexSectionName, nil
184187
} else if isComment(r) {
185188
return l.ignoreLineFunc(l.lexNextSectionOrOptionFunc(section)), nil
@@ -238,7 +241,6 @@ func (l *lexer) lexOptionValueFunc(section, name string, partial bytes.Buffer) l
238241
if !eof {
239242
partial.WriteRune('\n')
240243
}
241-
242244
return l.lexOptionValueFunc(section, name, partial), nil
243245
}
244246

unit/serialize.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,19 @@ func Serialize(opts []*UnitOption) io.Reader {
4141
idx[sec] = append(idx[sec], opt)
4242
}
4343

44-
for i, sect := range sections {
44+
for _, sect := range sections {
4545
writeSectionHeader(&buf, sect)
46-
writeNewline(&buf)
4746

4847
opts := idx[sect]
4948
for _, opt := range opts {
50-
writeOption(&buf, opt)
51-
writeNewline(&buf)
52-
}
53-
if i < len(sections)-1 {
54-
writeNewline(&buf)
49+
if len(opt.Name) == 0 && len(opt.Value) > 0 {
50+
buf.WriteString(opt.Value)
51+
} else if len(opt.Name) > 0 && len(opt.Value) > 0 {
52+
writeNewline(&buf)
53+
writeOption(&buf, opt)
54+
} else {
55+
writeNewline(&buf)
56+
}
5557
}
5658
}
5759

0 commit comments

Comments
 (0)