Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 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
33 changes: 32 additions & 1 deletion unit/deserialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ After=baz.service
&UnitOption{"Unit", "Description", "Bar"},
&UnitOption{"Unit", "Requires", "baz.service"},
&UnitOption{"Unit", "After", "baz.service"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -56,8 +57,14 @@ Pants=on
`),
[]*UnitOption{
&UnitOption{"Unit", "Description", "Foo"},
&UnitOption{"Unit", "", "\n"},
&UnitOption{"Unit", "", ""},
&UnitOption{"Service", "ExecStart", "/usr/bin/sleep infinity"},
&UnitOption{"Service", "", "\n"},
&UnitOption{"Service", "", ""},
&UnitOption{"X-Third-Party", "Pants", "on"},
&UnitOption{"X-Third-Party", "", "\n"},
&UnitOption{"X-Third-Party", "", ""},
},
},

Expand All @@ -67,7 +74,11 @@ Pants=on
[Service]
[X-Third-Party]
`),
[]*UnitOption{},
[]*UnitOption{
&UnitOption{"Unit", "", ""},
&UnitOption{"Service", "", ""},
&UnitOption{"X-Third-Party", "", ""},
},
},

// multiple values not special-cased
Expand All @@ -77,6 +88,7 @@ Environment= "FOO=BAR" "BAZ=QUX"
`),
[]*UnitOption{
&UnitOption{"Service", "Environment", "\"FOO=BAR\" \"BAZ=QUX\""},
&UnitOption{"Service", "", ""},
},
},

Expand All @@ -89,6 +101,7 @@ Description= Unnecessarily wrapped \
[]*UnitOption{
&UnitOption{"Unit", "Description", `Unnecessarily wrapped \
words here`},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -106,6 +119,7 @@ Description=Bar
`),
[]*UnitOption{
&UnitOption{"Unit", "Description", "Bar"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -121,7 +135,9 @@ Baz
`),
[]*UnitOption{
&UnitOption{"Unit", "Description", "Bar\\\n# comment alpha"},
&UnitOption{"Unit", "", "\n"},
&UnitOption{"Unit", "Description", "Bar\\\n# comment bravo \\\nBaz"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -133,6 +149,7 @@ Description=Bar
`),
[]*UnitOption{
&UnitOption{"Unit", "Description", "Bar"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -144,6 +161,7 @@ Description=Bar
`),
[]*UnitOption{
&UnitOption{"Unit", "Description", "Bar"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -154,6 +172,7 @@ Description=Bar
`),
[]*UnitOption{
&UnitOption{"Unit", "<<<<<<<<", "Bar"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -164,6 +183,7 @@ Some Thing = Bar
`),
[]*UnitOption{
&UnitOption{"Unit", "Some Thing", "Bar"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -173,6 +193,7 @@ Some Thing = Bar
Description=Bar`),
[]*UnitOption{
&UnitOption{"Unit", "Description", "Bar"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -182,6 +203,7 @@ Description=Bar`),
Description=Bar \`),
[]*UnitOption{
&UnitOption{"Unit", "Description", "Bar \\"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -191,6 +213,7 @@ Description=Bar \`),
µ☃=ÇôrèÕ$`),
[]*UnitOption{
&UnitOption{"©", "µ☃", "ÇôrèÕ$"},
&UnitOption{"©", "", ""},
},
},

Expand All @@ -200,7 +223,9 @@ Description=Bar \`),
Description =words here
`),
[]*UnitOption{
&UnitOption{"Unit", "", " "},
&UnitOption{"Unit", "Description", "words here"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -210,6 +235,7 @@ Description=Bar \`),
Description= words here `),
[]*UnitOption{
&UnitOption{"Unit", "Description", "words here"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -220,6 +246,7 @@ Description= words here \
`),
[]*UnitOption{
&UnitOption{"Unit", "Description", "words here \\\n"},
&UnitOption{"Unit", "", ""},
},
},

Expand All @@ -230,6 +257,7 @@ ExecStart=/bin/bash -c "while true; do echo \"ping\"; sleep 1; done"
`),
[]*UnitOption{
&UnitOption{"Service", "ExecStart", `/bin/bash -c "while true; do echo \"ping\"; sleep 1; done"`},
&UnitOption{"Service", "", ""},
},
},

Expand All @@ -239,6 +267,7 @@ ExecStart=/bin/bash -c "while true; do echo \"ping\"; sleep 1; done"
ExecStart=/bin/bash echo poof \ `),
[]*UnitOption{
&UnitOption{"Service", "ExecStart", `/bin/bash echo poof \`},
&UnitOption{"Service", "", ""},
},
},
// a long unit file line that's just equal to the maximum permitted length
Expand All @@ -247,6 +276,7 @@ ExecStart=/bin/bash echo poof \ `),
ExecStart=/bin/bash -c "echo ................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................."`),
[]*UnitOption{
&UnitOption{"Service", "ExecStart", `/bin/bash -c "echo ................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................."`},
&UnitOption{"Service", "", ""},
},
},
// the same, but with a trailing newline
Expand All @@ -258,6 +288,7 @@ Option=value
[]*UnitOption{
&UnitOption{"Service", "ExecStart", `/bin/bash -c "echo ................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................."`},
&UnitOption{"Service", "Option", "value"},
&UnitOption{"Service", "", ""},
},
},
}
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
17 changes: 17 additions & 0 deletions unit/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestSerialize(t *testing.T) {
[]*UnitOption{
&UnitOption{"Unit", "Description", "Foo"},
&UnitOption{"Unit", "BindsTo", "bar.service"},
&UnitOption{"Unit", "", ""},
},
`[Unit]
Description=Foo
Expand All @@ -47,6 +48,7 @@ BindsTo=bar.service
[]*UnitOption{
&UnitOption{"Unit", "Description", "Foo"},
&UnitOption{"Unit", "Description", "Bar"},
&UnitOption{"Unit", "", ""},
},
`[Unit]
Description=Foo
Expand All @@ -58,7 +60,10 @@ Description=Bar
{
[]*UnitOption{
&UnitOption{"Unit", "Description", "Foo"},
&UnitOption{"Unit", "", "\n"},
&UnitOption{"Unit", "", ""},
&UnitOption{"Service", "ExecStart", "/usr/bin/sleep infinity"},
&UnitOption{"Service", "", ""},
},
`[Unit]
Description=Foo
Expand All @@ -74,6 +79,9 @@ ExecStart=/usr/bin/sleep infinity
&UnitOption{"Unit", "Description", "Foo"},
&UnitOption{"Service", "ExecStart", "/usr/bin/sleep infinity"},
&UnitOption{"Unit", "BindsTo", "bar.service"},
&UnitOption{"Unit", "", "\n"},
&UnitOption{"Unit", "", ""},
&UnitOption{"Service", "", ""},
},
`[Unit]
Description=Foo
Expand All @@ -92,7 +100,12 @@ ExecStart=/usr/bin/sleep infinity
&UnitOption{"Unit", "BindsTo", "bar.service"},
&UnitOption{"X-Foo", "Bar", "baz"},
&UnitOption{"Service", "ExecStop", "/usr/bin/sleep 1"},
&UnitOption{"Service", "", "\n"},
&UnitOption{"Service", "", ""},
&UnitOption{"Unit", "Documentation", "https://foo.com"},
&UnitOption{"Unit", "", "\n"},
&UnitOption{"Unit", "", ""},
&UnitOption{"X-Foo", "", ""},
},
`[Unit]
Description=Foo
Expand All @@ -112,6 +125,7 @@ Bar=baz
{
[]*UnitOption{
&UnitOption{"©", "µ☃", "ÇôrèÕ$"},
&UnitOption{"©", "", ""},
},
`[©]
µ☃=ÇôrèÕ$
Expand All @@ -122,6 +136,7 @@ Bar=baz
{
[]*UnitOption{
&UnitOption{"Un\nit", "Description", "Foo"},
&UnitOption{"Un\nit", "", ""},
},
`[Un
it]
Expand All @@ -133,6 +148,7 @@ Description=Foo
{
[]*UnitOption{
&UnitOption{"Unit", "Desc\nription", "Foo"},
&UnitOption{"Unit", "", ""},
},
`[Unit]
Desc
Expand All @@ -144,6 +160,7 @@ ription=Foo
{
[]*UnitOption{
&UnitOption{"Unit", "Description", "Fo\no"},
&UnitOption{"Unit", "", ""},
},
`[Unit]
Description=Fo
Expand Down