add optional timestamps to build log

This commit is contained in:
Ashley Lowde 2018-09-29 22:39:24 +09:30
parent b7b1720a91
commit 12496e3702
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 {
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,
}

View File

@ -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)
}

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
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.

View File

@ -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
```