Skip to content

Commit

Permalink
unit: try to avoid unit file modification
Browse files Browse the repository at this point in the history
  • Loading branch information
kayrus committed Apr 8, 2016
1 parent 7b2428f commit 9bb6d77
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion unit/deserialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,16 @@ func (l *lexer) lexNextSectionOrOptionFunc(section string) lexStep {
if err != nil {
if err == io.EOF {
err = nil
l.optchan <- &UnitOption{Section: section, Name: "", Value: ""}
}
return nil, err
}

if unicode.IsSpace(r) {
l.optchan <- &UnitOption{Section: section, Name: "", Value: fmt.Sprintf("%c", r)}
return l.lexNextSectionOrOptionFunc(section), nil
} else if r == '[' {
l.optchan <- &UnitOption{Section: section, Name: "", Value: ""}
return l.lexSectionName, nil
} else if isComment(r) {
return l.ignoreLineFunc(l.lexNextSectionOrOptionFunc(section)), nil
Expand Down Expand Up @@ -238,7 +241,6 @@ func (l *lexer) lexOptionValueFunc(section, name string, partial bytes.Buffer) l
if !eof {
partial.WriteRune('\n')
}

return l.lexOptionValueFunc(section, name, partial), nil
}

Expand Down
16 changes: 9 additions & 7 deletions unit/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,19 @@ func Serialize(opts []*UnitOption) io.Reader {
idx[sec] = append(idx[sec], opt)
}

for i, sect := range sections {
for _, sect := range sections {
writeSectionHeader(&buf, sect)
writeNewline(&buf)

opts := idx[sect]
for _, opt := range opts {
writeOption(&buf, opt)
writeNewline(&buf)
}
if i < len(sections)-1 {
writeNewline(&buf)
if len(opt.Name) == 0 && len(opt.Value) > 0 {
buf.WriteString(opt.Value)
} else if len(opt.Name) > 0 && len(opt.Value) > 0 {
writeNewline(&buf)
writeOption(&buf, opt)
} else {
writeNewline(&buf)
}
}
}

Expand Down

0 comments on commit 9bb6d77

Please sign in to comment.