@@ -363,12 +363,6 @@ func newTemplate(name string) *template.Template {
363
363
}
364
364
365
365
func generateFile (config Config , containers Context ) bool {
366
- templatePath := config .Template
367
- tmpl , err := newTemplate (filepath .Base (templatePath )).ParseFiles (templatePath )
368
- if err != nil {
369
- log .Fatalf ("unable to parse template: %s" , err )
370
- }
371
-
372
366
filteredContainers := Context {}
373
367
if config .OnlyPublished {
374
368
for _ , container := range containers {
@@ -386,55 +380,39 @@ func generateFile(config Config, containers Context) bool {
386
380
filteredContainers = containers
387
381
}
388
382
389
- dest := os .Stdout
383
+ contents := executeTemplate (config .Template , filteredContainers )
384
+
385
+ if config .SkipBlankLines {
386
+ contents = removeBlankLines (contents )
387
+ }
388
+
390
389
if config .Dest != "" {
391
- dest , err = ioutil .TempFile (filepath .Dir (config .Dest ), "docker-gen" )
390
+ dest , err : = ioutil .TempFile (filepath .Dir (config .Dest ), "docker-gen" )
392
391
defer func () {
393
392
dest .Close ()
394
393
os .Remove (dest .Name ())
395
394
}()
396
395
if err != nil {
397
396
log .Fatalf ("unable to create temp file: %s\n " , err )
398
397
}
399
- }
400
-
401
- var buf bytes.Buffer
402
- bw := bufio .NewWriter (& buf )
403
- err = tmpl .ExecuteTemplate (bw , filepath .Base (templatePath ), & filteredContainers )
404
- if err != nil {
405
- log .Fatalf ("template error: %s\n " , err )
406
- }
407
- bw .Flush ()
408
-
409
- if config .SkipBlankLines {
410
- scanner := bufio .NewScanner (bufio .NewReader (& buf ))
411
- for scanner .Scan () {
412
- line := scanner .Text ()
413
- if ! isBlank (line ) {
414
- fmt .Fprintln (dest , line )
415
- }
416
- }
417
- } else {
418
- buf .WriteTo (dest )
419
- }
420
398
421
- if config . Dest != "" {
399
+ dest . Write ( contents )
422
400
423
- contents := []byte {}
401
+ oldContents := []byte {}
424
402
if fi , err := os .Stat (config .Dest ); err == nil {
425
403
if err := dest .Chmod (fi .Mode ()); err != nil {
426
404
log .Fatalf ("unable to chmod temp file: %s\n " , err )
427
405
}
428
406
if err := dest .Chown (int (fi .Sys ().(* syscall.Stat_t ).Uid ), int (fi .Sys ().(* syscall.Stat_t ).Gid )); err != nil {
429
407
log .Fatalf ("unable to chown temp file: %s\n " , err )
430
408
}
431
- contents , err = ioutil .ReadFile (config .Dest )
409
+ oldContents , err = ioutil .ReadFile (config .Dest )
432
410
if err != nil {
433
411
log .Fatalf ("unable to compare current file contents: %s: %s\n " , config .Dest , err )
434
412
}
435
413
}
436
414
437
- if bytes .Compare (contents , buf . Bytes () ) != 0 {
415
+ if bytes .Compare (oldContents , contents ) != 0 {
438
416
err = os .Rename (dest .Name (), config .Dest )
439
417
if err != nil {
440
418
log .Fatalf ("unable to create dest file %s: %s\n " , config .Dest , err )
@@ -443,6 +421,34 @@ func generateFile(config Config, containers Context) bool {
443
421
return true
444
422
}
445
423
return false
424
+ } else {
425
+ os .Stdout .Write (contents )
446
426
}
447
427
return true
448
428
}
429
+
430
+ func executeTemplate (templatePath string , containers Context ) []byte {
431
+ tmpl , err := newTemplate (filepath .Base (templatePath )).ParseFiles (templatePath )
432
+ if err != nil {
433
+ log .Fatalf ("unable to parse template: %s" , err )
434
+ }
435
+
436
+ var buf bytes.Buffer
437
+ err = tmpl .ExecuteTemplate (& buf , filepath .Base (templatePath ), & containers )
438
+ if err != nil {
439
+ log .Fatalf ("template error: %s\n " , err )
440
+ }
441
+ return buf .Bytes ()
442
+ }
443
+
444
+ func removeBlankLines (buf []byte ) []byte {
445
+ filtered := new (bytes.Buffer )
446
+ scanner := bufio .NewScanner (bytes .NewReader (buf ))
447
+ for scanner .Scan () {
448
+ line := scanner .Text ()
449
+ if ! isBlank (line ) {
450
+ fmt .Fprintln (filtered , line )
451
+ }
452
+ }
453
+ return filtered .Bytes ()
454
+ }
0 commit comments