From 12496e37023e09e6cc0def590298579d2bfd1fcc Mon Sep 17 00:00:00 2001 From: Ashley Lowde Date: Sat, 29 Sep 2018 22:39:24 +0930 Subject: [PATCH] add optional timestamps to build log --- command/build.go | 11 ++++++- packer/ui.go | 34 ++++++++++++++++++++++ website/source/docs/commands/build.html.md | 3 ++ website/source/docs/commands/index.html.md | 2 +- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/command/build.go b/command/build.go index c8e2bd994..ecbccc35b 100644 --- a/command/build.go +++ b/command/build.go @@ -23,13 +23,14 @@ type BuildCommand struct { } func (c *BuildCommand) Run(args []string) int { - var cfgColor, cfgDebug, cfgForce, cfgParallel bool + var cfgColor, cfgDebug, cfgForce, cfgTimestamp, cfgParallel bool var cfgOnError string flags := c.Meta.FlagSet("build", FlagSetBuildFilter|FlagSetVars) flags.Usage = func() { c.Ui.Say(c.Help()) } flags.BoolVar(&cfgColor, "color", true, "") flags.BoolVar(&cfgDebug, "debug", false, "") flags.BoolVar(&cfgForce, "force", false, "") + flags.BoolVar(&cfgTimestamp, "timestamp", false, "") flagOnError := enumflag.New(&cfgOnError, "cleanup", "abort", "ask") flags.Var(flagOnError, "on-error", "") flags.BoolVar(&cfgParallel, "parallel", true, "") @@ -101,6 +102,12 @@ func (c *BuildCommand) Run(args []string) int { // Add a newline between the color output and the actual output c.Ui.Say("") } + // Now add timestamps if requested + if cfgTimestamp { + ui = &packer.TimestampedUi{ + Ui: ui, + } + } } } @@ -302,6 +309,7 @@ Options: -machine-readable Machine-readable output -on-error=[cleanup|abort|ask] If the build fails do: clean up (default), abort, or ask -parallel=false Disable parallelization (on by default) + -timestamp=true Enable timestamps in build log (off by default) -var 'key=value' Variable for templates, can be used multiple times. -var-file=path JSON file containing user variables. ` @@ -327,6 +335,7 @@ func (*BuildCommand) AutocompleteFlags() complete.Flags { "-machine-readable": complete.PredictNothing, "-on-error": complete.PredictNothing, "-parallel": complete.PredictNothing, + "-timestamp": complete.PredictNothing, "-var": complete.PredictNothing, "-var-file": complete.PredictNothing, } diff --git a/packer/ui.go b/packer/ui.go index 340c56827..9e46d46b8 100644 --- a/packer/ui.go +++ b/packer/ui.go @@ -99,6 +99,14 @@ type MachineReadableUi struct { var _ Ui = new(MachineReadableUi) +// TimestampedUi is a UI that wraps another UI implementation and prefixes +// prefixes each message with an RFC3339 timestamp +type TimestampedUi struct { + Ui Ui +} + +var _ Ui = new(TimestampedUi) + func (u *ColoredUi) Ask(query string) (string, error) { return u.Ui.Ask(u.colorize(query, u.Color, true)) } @@ -342,3 +350,29 @@ func (u *MachineReadableUi) Machine(category string, args ...string) { func (u *MachineReadableUi) ProgressBar() ProgressBar { return new(NoopProgressBar) } + +func (u *TimestampedUi) Ask(query string) (string, error) { + return u.Ui.Ask(query) +} + +func (u *TimestampedUi) Say(message string) { + u.Ui.Say(u.timestampLine(message)) +} + +func (u *TimestampedUi) Message(message string) { + u.Ui.Message(u.timestampLine(message)) +} + +func (u *TimestampedUi) Error(message string) { + u.Ui.Error(u.timestampLine(message)) +} + +func (u *TimestampedUi) Machine(message string, args ...string) { + u.Ui.Machine(message, args...) +} + +func (u *TimestampedUi) ProgressBar() ProgressBar { return u.Ui.ProgressBar() } + +func (u *TimestampedUi) timestampLine(string string) string { + return fmt.Sprintf("%v: %v", time.Now().Format(time.RFC3339), string) +} diff --git a/website/source/docs/commands/build.html.md b/website/source/docs/commands/build.html.md index 575f9ed84..433419390 100644 --- a/website/source/docs/commands/build.html.md +++ b/website/source/docs/commands/build.html.md @@ -50,6 +50,9 @@ that are created will be outputted at the end of the build. - `-parallel=false` - Disable parallelization of multiple builders (on by default). +- `-timestamp=true` - Enable timestamps for build logs without adding the extra + information included if PACKER_LOG is true. + - `-var` - Set a variable in your packer template. This option can be used multiple times. This is useful for setting version numbers for your build. diff --git a/website/source/docs/commands/index.html.md b/website/source/docs/commands/index.html.md index d3a28fc65..63543c134 100644 --- a/website/source/docs/commands/index.html.md +++ b/website/source/docs/commands/index.html.md @@ -119,5 +119,5 @@ For example, assume a tab is typed at the end of each prompt line: $ packer p plugin build $ packer build - --color -debug -except -force -machine-readable -on-error -only -parallel -var -var-file +-color -debug -except -force -machine-readable -on-error -only -parallel -timestamp -var -var-file ```