@@ -48,7 +48,6 @@ type UnitFileParser struct {
48
48
49
49
currentGroup * unitGroup
50
50
pendingComments []* unitLine
51
- lineNr int
52
51
}
53
52
54
53
func newUnitLine (key string , value string , isComment bool ) * unitLine {
@@ -347,7 +346,7 @@ func (p *UnitFileParser) parseKeyValuePair(line string) error {
347
346
return nil
348
347
}
349
348
350
- func (p * UnitFileParser ) parseLine (line string ) error {
349
+ func (p * UnitFileParser ) parseLine (line string , lineNr int ) error {
351
350
switch {
352
351
case lineIsComment (line ):
353
352
return p .parseComment (line )
@@ -356,7 +355,7 @@ func (p *UnitFileParser) parseLine(line string) error {
356
355
case lineIsKeyValuePair (line ):
357
356
return p .parseKeyValuePair (line )
358
357
default :
359
- return fmt .Errorf ("file contains line %d: “%s” which is not a key-value pair, group, or comment" , p . lineNr , line )
358
+ return fmt .Errorf ("file contains line %d: “%s” which is not a key-value pair, group, or comment" , lineNr , line )
360
359
}
361
360
}
362
361
@@ -376,53 +375,39 @@ func (p *UnitFileParser) flushPendingComments(toComment bool) {
376
375
}
377
376
}
378
377
379
- func nextLine (data string , afterPos int ) (string , string ) {
380
- rest := data [afterPos :]
381
- if i := strings .Index (rest , "\n " ); i >= 0 {
382
- return strings .TrimSpace (data [:i + afterPos ]), data [i + afterPos + 1 :]
383
- }
384
- return data , ""
385
- }
386
-
387
- func trimSpacesFromLines (data string ) string {
388
- lines := strings .Split (data , "\n " )
389
- for i , line := range lines {
390
- lines [i ] = strings .TrimSpace (line )
391
- }
392
- return strings .Join (lines , "\n " )
393
- }
394
-
395
378
// Parse an already loaded unit file (in the form of a string)
396
379
func (f * UnitFile ) Parse (data string ) error {
397
380
p := & UnitFileParser {
398
- file : f ,
399
- lineNr : 1 ,
381
+ file : f ,
400
382
}
401
383
402
- data = trimSpacesFromLines (data )
403
-
404
- for len (data ) > 0 {
405
- origdata := data
406
- nLines := 1
407
- var line string
408
- line , data = nextLine (data , 0 )
384
+ lines := strings .Split (strings .TrimSuffix (data , "\n " ), "\n " )
385
+ remaining := ""
409
386
410
- if ! lineIsComment (line ) {
411
- // Handle multi-line continuations
412
- // Note: This doesn't support comments in the middle of the continuation, which systemd does
413
- if lineIsKeyValuePair (line ) {
414
- for len (data ) > 0 && line [len (line )- 1 ] == '\\' {
415
- line , data = nextLine (origdata , len (line )+ 1 )
416
- nLines ++
387
+ for lineNr , line := range lines {
388
+ if lineIsComment (line ) {
389
+ // ignore the comment is inside a continuation line.
390
+ if remaining != "" {
391
+ continue
392
+ }
393
+ } else {
394
+ line = strings .TrimSpace (line )
395
+ if strings .HasSuffix (line , "\\ " ) {
396
+ line = line [:len (line )- 1 ]
397
+ // check whether the line is a continuation of the previous line
398
+ if lineNr != len (lines )- 1 {
399
+ remaining += line
400
+ continue
417
401
}
418
402
}
403
+ if remaining != "" {
404
+ line = remaining + line
405
+ remaining = ""
406
+ }
419
407
}
420
-
421
- if err := p .parseLine (line ); err != nil {
408
+ if err := p .parseLine (line , lineNr + 1 ); err != nil {
422
409
return err
423
410
}
424
-
425
- p .lineNr += nLines
426
411
}
427
412
428
413
if p .currentGroup == nil {
@@ -690,7 +675,6 @@ func (f *UnitFile) LookupInt(groupName string, key string, defaultValue int64) i
690
675
}
691
676
692
677
intVal , err := convertNumber (v )
693
-
694
678
if err != nil {
695
679
return defaultValue
696
680
}
0 commit comments