Merge pull request #6784 from alowde/add-build-timestamps

add optional timestamps to build log
This commit is contained in:
Adrien Delorme 2018-10-02 14:19:14 +02:00 committed by GitHub
commit 83c713c0df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 2 deletions

View File

@ -23,13 +23,14 @@ type BuildCommand struct {
} }
func (c *BuildCommand) Run(args []string) int { func (c *BuildCommand) Run(args []string) int {
var cfgColor, cfgDebug, cfgForce, cfgParallel bool var cfgColor, cfgDebug, cfgForce, cfgTimestamp, cfgParallel bool
var cfgOnError string var cfgOnError string
flags := c.Meta.FlagSet("build", FlagSetBuildFilter|FlagSetVars) flags := c.Meta.FlagSet("build", FlagSetBuildFilter|FlagSetVars)
flags.Usage = func() { c.Ui.Say(c.Help()) } flags.Usage = func() { c.Ui.Say(c.Help()) }
flags.BoolVar(&cfgColor, "color", true, "") flags.BoolVar(&cfgColor, "color", true, "")
flags.BoolVar(&cfgDebug, "debug", false, "") flags.BoolVar(&cfgDebug, "debug", false, "")
flags.BoolVar(&cfgForce, "force", false, "") flags.BoolVar(&cfgForce, "force", false, "")
flags.BoolVar(&cfgTimestamp, "timestamp", false, "")
flagOnError := enumflag.New(&cfgOnError, "cleanup", "abort", "ask") flagOnError := enumflag.New(&cfgOnError, "cleanup", "abort", "ask")
flags.Var(flagOnError, "on-error", "") flags.Var(flagOnError, "on-error", "")
flags.BoolVar(&cfgParallel, "parallel", true, "") 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 // Add a newline between the color output and the actual output
c.Ui.Say("") 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 -machine-readable Machine-readable output
-on-error=[cleanup|abort|ask] If the build fails do: clean up (default), abort, or ask -on-error=[cleanup|abort|ask] If the build fails do: clean up (default), abort, or ask
-parallel=false Disable parallelization (on by default) -parallel=false Disable parallelization (on by default)
-timestamp-ui=true Prefix each ui output with an RFC3339 timestamp (off by default).
-var 'key=value' Variable for templates, can be used multiple times. -var 'key=value' Variable for templates, can be used multiple times.
-var-file=path JSON file containing user variables. -var-file=path JSON file containing user variables.
` `
@ -327,6 +335,7 @@ func (*BuildCommand) AutocompleteFlags() complete.Flags {
"-machine-readable": complete.PredictNothing, "-machine-readable": complete.PredictNothing,
"-on-error": complete.PredictNothing, "-on-error": complete.PredictNothing,
"-parallel": complete.PredictNothing, "-parallel": complete.PredictNothing,
"-timestamp": complete.PredictNothing,
"-var": complete.PredictNothing, "-var": complete.PredictNothing,
"-var-file": complete.PredictNothing, "-var-file": complete.PredictNothing,
} }

View File

@ -342,3 +342,37 @@ func (u *MachineReadableUi) Machine(category string, args ...string) {
func (u *MachineReadableUi) ProgressBar() ProgressBar { func (u *MachineReadableUi) ProgressBar() ProgressBar {
return new(NoopProgressBar) return new(NoopProgressBar)
} }
// 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 *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)
}

View File

@ -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 - `-parallel=false` - Disable parallelization of multiple builders (on by
default). default).
- `-timestamp-ui=true` - Prefix each ui output with an RFC3339 timestamp (off
by default).
- `-var` - Set a variable in your packer template. This option can be used - `-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. multiple times. This is useful for setting version numbers for your build.

View File

@ -119,5 +119,5 @@ For example, assume a tab is typed at the end of each prompt line:
$ packer p $ packer p
plugin build plugin build
$ packer 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
``` ```