diff --git a/cmd/renderizer/main.go b/cmd/renderizer/main.go index c112700..b78e0f9 100644 --- a/cmd/renderizer/main.go +++ b/cmd/renderizer/main.go @@ -8,9 +8,9 @@ import ( "path/filepath" "strings" + "github.com/gomatic/renderizer/v2/pkg/renderizer" "github.com/imdario/mergo" "github.com/kardianos/osext" - "github.com/gomatic/renderizer/v2/pkg/renderizer" "github.com/urfave/cli/v2" "gopkg.in/yaml.v2" ) @@ -123,6 +123,13 @@ func main() { EnvVars: []string{"RENDERIZER_VEBOSE"}, Destination: &settings.Options.Verbose, }, + &cli.StringFlag{ + Name: "output", + Aliases: []string{"o"}, + Usage: "writes template to output file rather than stdout", + Value: settings.Options.Output, + Destination: &settings.Options.Output, + }, } app.Before = func(ctx *cli.Context) error { @@ -241,7 +248,7 @@ func main() { flag = parts[0] } switch flag[2:] { - case "settings", "missing", "environment", "env": + case "settings", "missing", "environment", "env", "output": // If the flag requires a parameter but it is not specified with an =, grab the next argument too. if !strings.Contains(larg, "=") { next = true diff --git a/pkg/renderizer/renderizer.go b/pkg/renderizer/renderizer.go index 159dd77..d6b4154 100644 --- a/pkg/renderizer/renderizer.go +++ b/pkg/renderizer/renderizer.go @@ -44,6 +44,8 @@ type Options struct { Verbose bool // Testing bool + // + Output string } // @@ -215,7 +217,19 @@ func (settings *Options) Render() error { } data = b.Bytes() - fmt.Println(string(data)) + fh := os.Stdout + + if settings.Output != "" { + if fh, err = os.Create(settings.Output); err != nil { + log.Print(err) + return 8 + } + } + + if _, err = fh.Write(data); err != nil { + log.Print(err) + return 8 + } return 0 }()