@@ -363,12 +363,6 @@ func newTemplate(name string) *template.Template {
363363}
364364
365365func 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-
372366 filteredContainers := Context {}
373367 if config .OnlyPublished {
374368 for _ , container := range containers {
@@ -386,55 +380,39 @@ func generateFile(config Config, containers Context) bool {
386380 filteredContainers = containers
387381 }
388382
389- dest := os .Stdout
383+ contents := executeTemplate (config .Template , filteredContainers )
384+
385+ if config .SkipBlankLines {
386+ contents = removeBlankLines (contents )
387+ }
388+
390389 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" )
392391 defer func () {
393392 dest .Close ()
394393 os .Remove (dest .Name ())
395394 }()
396395 if err != nil {
397396 log .Fatalf ("unable to create temp file: %s\n " , err )
398397 }
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- }
420398
421- if config . Dest != "" {
399+ dest . Write ( contents )
422400
423- contents := []byte {}
401+ oldContents := []byte {}
424402 if fi , err := os .Stat (config .Dest ); err == nil {
425403 if err := dest .Chmod (fi .Mode ()); err != nil {
426404 log .Fatalf ("unable to chmod temp file: %s\n " , err )
427405 }
428406 if err := dest .Chown (int (fi .Sys ().(* syscall.Stat_t ).Uid ), int (fi .Sys ().(* syscall.Stat_t ).Gid )); err != nil {
429407 log .Fatalf ("unable to chown temp file: %s\n " , err )
430408 }
431- contents , err = ioutil .ReadFile (config .Dest )
409+ oldContents , err = ioutil .ReadFile (config .Dest )
432410 if err != nil {
433411 log .Fatalf ("unable to compare current file contents: %s: %s\n " , config .Dest , err )
434412 }
435413 }
436414
437- if bytes .Compare (contents , buf . Bytes () ) != 0 {
415+ if bytes .Compare (oldContents , contents ) != 0 {
438416 err = os .Rename (dest .Name (), config .Dest )
439417 if err != nil {
440418 log .Fatalf ("unable to create dest file %s: %s\n " , config .Dest , err )
@@ -443,6 +421,34 @@ func generateFile(config Config, containers Context) bool {
443421 return true
444422 }
445423 return false
424+ } else {
425+ os .Stdout .Write (contents )
446426 }
447427 return true
448428}
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