@@ -362,12 +362,6 @@ func newTemplate(name string) *template.Template {
362362}
363363
364364func generateFile (config Config , containers Context ) bool {
365- templatePath := config .Template
366- tmpl , err := newTemplate (filepath .Base (templatePath )).ParseFiles (templatePath )
367- if err != nil {
368- log .Fatalf ("unable to parse template: %s" , err )
369- }
370-
371365 filteredContainers := Context {}
372366 if config .OnlyPublished {
373367 for _ , container := range containers {
@@ -385,42 +379,41 @@ func generateFile(config Config, containers Context) bool {
385379 filteredContainers = containers
386380 }
387381
388- dest := os .Stdout
382+ contents := executeTemplate (config .Template , filteredContainers )
383+
384+ if ! config .KeepBlankLines {
385+ buf := new (bytes.Buffer )
386+ removeBlankLines (bytes .NewReader (contents ), buf )
387+ contents = buf .Bytes ()
388+ }
389+
389390 if config .Dest != "" {
390- dest , err = ioutil .TempFile (filepath .Dir (config .Dest ), "docker-gen" )
391+ dest , err : = ioutil .TempFile (filepath .Dir (config .Dest ), "docker-gen" )
391392 defer func () {
392393 dest .Close ()
393394 os .Remove (dest .Name ())
394395 }()
395396 if err != nil {
396397 log .Fatalf ("unable to create temp file: %s\n " , err )
397398 }
398- }
399-
400- var buf bytes.Buffer
401- multiwriter := io .MultiWriter (dest , & buf )
402- err = tmpl .ExecuteTemplate (multiwriter , filepath .Base (templatePath ), & filteredContainers )
403- if err != nil {
404- log .Fatalf ("template error: %s\n " , err )
405- }
406399
407- if config . Dest != "" {
400+ dest . Write ( contents )
408401
409- contents := []byte {}
402+ oldContents := []byte {}
410403 if fi , err := os .Stat (config .Dest ); err == nil {
411404 if err := dest .Chmod (fi .Mode ()); err != nil {
412405 log .Fatalf ("unable to chmod temp file: %s\n " , err )
413406 }
414407 if err := dest .Chown (int (fi .Sys ().(* syscall.Stat_t ).Uid ), int (fi .Sys ().(* syscall.Stat_t ).Gid )); err != nil {
415408 log .Fatalf ("unable to chown temp file: %s\n " , err )
416409 }
417- contents , err = ioutil .ReadFile (config .Dest )
410+ oldContents , err = ioutil .ReadFile (config .Dest )
418411 if err != nil {
419412 log .Fatalf ("unable to compare current file contents: %s: %s\n " , config .Dest , err )
420413 }
421414 }
422415
423- if bytes .Compare (contents , buf . Bytes () ) != 0 {
416+ if bytes .Compare (oldContents , contents ) != 0 {
424417 err = os .Rename (dest .Name (), config .Dest )
425418 if err != nil {
426419 log .Fatalf ("unable to create dest file %s: %s\n " , config .Dest , err )
@@ -429,6 +422,22 @@ func generateFile(config Config, containers Context) bool {
429422 return true
430423 }
431424 return false
425+ } else {
426+ os .Stdout .Write (contents )
432427 }
433428 return true
434429}
430+
431+ func executeTemplate (templatePath string , containers Context ) []byte {
432+ tmpl , err := newTemplate (filepath .Base (templatePath )).ParseFiles (templatePath )
433+ if err != nil {
434+ log .Fatalf ("unable to parse template: %s" , err )
435+ }
436+
437+ buf := new (bytes.Buffer )
438+ err = tmpl .ExecuteTemplate (buf , filepath .Base (templatePath ), & containers )
439+ if err != nil {
440+ log .Fatalf ("template error: %s\n " , err )
441+ }
442+ return buf .Bytes ()
443+ }
0 commit comments