From 88792c4bda7169fc41d1e2fdb79c8c970499e1e7 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 12 Sep 2019 07:17:22 +0200 Subject: [PATCH] Add parameter to write Dockerfile directly Signed-off-by: Knut Ahlers --- main.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index c913230..7d33cd6 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + "io" "os" "os/exec" "regexp" @@ -23,6 +24,7 @@ var ( Timezone string `flag:"timezone,t" default:"" description:"Set timezone in Dockerfile (format 'Europe/Berlin')"` VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` Volumes []string `flag:"volume,v" default:"" description:"Volumes to create mount points for (format '/data')"` + Write bool `flag:"write,w" default:"false" description:"Directly write into Dockerfile"` }{} version = "dev" @@ -86,7 +88,17 @@ func main() { log.WithError(err).Fatalf("Could not render template %q", tplPath) } - fmt.Println(strings.TrimSpace(regexp.MustCompile(`\n{3,}`).ReplaceAllString(buf.String(), "\n\n"))) + var output io.Writer = os.Stdout + if cfg.Write { + f, err := os.Create("Dockerfile") + if err != nil { + log.WithError(err).Fatal("Could not open Dockerfile for writing") + } + defer f.Close() + output = f + } + + fmt.Fprintln(output, strings.TrimSpace(regexp.MustCompile(`\n{3,}`).ReplaceAllString(buf.String(), "\n\n"))) } func getPackage() (string, error) {