From e96cc7bc3cffa6bcd6526203f8296ee711a530e8 Mon Sep 17 00:00:00 2001 From: Evgeny Budilovsky Date: Tue, 1 Oct 2019 21:45:02 +0300 Subject: [PATCH] Print total results banner at the end of execution (#160) --- cmd/exec/exec.go | 23 +++++++++----- cmd/utils/printer.go | 39 +++++++++++++++++++----- testing/fixtures/execute_cmd_fixtures.go | 21 ++++++------- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index 464fe03f..b01ac7dc 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -2,6 +2,8 @@ package exec import ( "fmt" + "time" + "github.com/codeskyblue/go-sh" "github.com/gopinath-langote/1build/cmd/config" "github.com/gopinath-langote/1build/cmd/models" @@ -11,6 +13,8 @@ import ( // ExecutePlan executes the Execution plan func ExecutePlan(commands ...string) { + executeStart := time.Now() + configuration, err := config.LoadOneBuildConfiguration() if err != nil { fmt.Println(err) @@ -21,30 +25,33 @@ func ExecutePlan(commands ...string) { executionPlan.Print() if executionPlan.HasBefore() { - executeAndStopIfFailed(executionPlan.Before) + executeAndStopIfFailed(executionPlan.Before, executeStart) } if executionPlan.HasCommands() { for _, commandContext := range executionPlan.Commands { - executeAndStopIfFailed(commandContext) + executeAndStopIfFailed(commandContext, executeStart) } } if executionPlan.HasAfter() { - executeAndStopIfFailed(executionPlan.After) + executeAndStopIfFailed(executionPlan.After, executeStart) } - fmt.Println() - fmt.Println(utils.ColoredB("SUCCESS", utils.CYAN)) - + utils.PrintResultsBanner(true, executeStart) } -func executeAndStopIfFailed(command *models.CommandContext) { +func executeAndStopIfFailed(command *models.CommandContext, executeStart time.Time) { command.PrintBanner() err := command.CommandSession.Run() if err != nil { exitCode := (err.Error())[12:] - utils.PrintlnDashedErr("Execution failed during phase \"" + command.Name + "\" - Execution of the script \"" + command.Command + "\" returned non-zero exit code : " + exitCode) + utils.PrintlnErr("Execution failed during phase \"" + + command.Name + + "\" - Execution of the script \"" + + command.Command + + "\" returned non-zero exit code : " + exitCode) + utils.PrintResultsBanner(false, executeStart) utils.ExitWithCode(exitCode) } } diff --git a/cmd/utils/printer.go b/cmd/utils/printer.go index 5876d667..f556bad6 100644 --- a/cmd/utils/printer.go +++ b/cmd/utils/printer.go @@ -2,8 +2,10 @@ package utils import ( "fmt" - "github.com/logrusorgru/aurora" "strings" + "time" + + "github.com/logrusorgru/aurora" ) // BANNER return dashes with fixed length - 72 @@ -19,7 +21,7 @@ const ( CYAN OneBuildColor = 0 // RED is used in failure messages - RED OneBuildColor = 1 + RED OneBuildColor = 1 ) // ColoredB return text in color with bold format @@ -37,13 +39,10 @@ func ColoredBU(text string, color OneBuildColor) string { return colorize(text, color).Bold().Underline().String() } -// PrintlnDashedErr prints error line to console in bold Red with dashes above and below -func PrintlnDashedErr(text string) { - errDash := strings.Repeat("-", len(text)) +// PrintlnErr prints error line to console in bold Red +func PrintlnErr(text string) { fmt.Println() - fmt.Println(errDash) fmt.Println(ColoredB(text, RED)) - fmt.Println(errDash) } func colorize(text string, color OneBuildColor) aurora.Value { @@ -55,3 +54,29 @@ func colorize(text string, color OneBuildColor) aurora.Value { } return coloredText } + +// PrintResultsBanner prints result banner at the end of the test +func PrintResultsBanner(isSuccess bool, startTime time.Time) { + timeDelta := time.Since(startTime).Round(time.Second) + mins := int64(timeDelta / time.Minute) + secs := int64((timeDelta % time.Minute) / time.Second) + var timeStr string + if mins == 0 { + timeStr = fmt.Sprintf("%.2ds", secs) + } else { + timeStr = fmt.Sprintf("%.2dm %.2ds", mins, secs) + } + result := aurora.BrightCyan("SUCCESS") + if !isSuccess { + result = aurora.Red("FAILURE") + } + + s := fmt.Sprintf("%s - Total Time: %s", result, timeStr) + + if isSuccess { + fmt.Println() + } + fmt.Println(BANNER()) + fmt.Println(s) + fmt.Println(BANNER()) +} diff --git a/testing/fixtures/execute_cmd_fixtures.go b/testing/fixtures/execute_cmd_fixtures.go index 16090e2d..1e95e5c1 100644 --- a/testing/fixtures/execute_cmd_fixtures.go +++ b/testing/fixtures/execute_cmd_fixtures.go @@ -1,12 +1,17 @@ package fixtures import ( + "strings" "testing" "github.com/gopinath-langote/1build/testing/utils" "github.com/stretchr/testify/assert" ) +func successBanner() string { + return strings.Repeat("-", 72) + "\n" + utils.Colored("SUCCESS", utils.CYAN) +} + func featureExecuteCmdTestData() []Test { feature := "exec" @@ -37,8 +42,7 @@ build echo building project -------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]-------------------------------- building project -` + utils.ColoredB("SUCCESS", utils.CYAN) + ` -` +` + successBanner() return Test{ Feature: feature, Name: "shouldExecuteAvailableCommand", @@ -99,8 +103,7 @@ running pre-command -------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]-------------------------------- building project -` + utils.ColoredB("SUCCESS", utils.CYAN) + ` -` +` + successBanner() return Test{ Feature: feature, Name: "shouldExecuteBeforeCommand", @@ -134,8 +137,7 @@ building project -------------------------------[ ` + utils.Colored("after", utils.CYAN) + ` ]-------------------------------- running post-command -` + utils.ColoredB("SUCCESS", utils.CYAN) + ` -` +` + successBanner() return Test{ Feature: feature, Name: "shouldExecuteAfterCommand", @@ -173,8 +175,7 @@ building project -------------------------------[ ` + utils.Colored("after", utils.CYAN) + ` ]-------------------------------- running post-command -` + utils.ColoredB("SUCCESS", utils.CYAN) + ` -` +` + successBanner() return Test{ Feature: feature, Name: "shouldExecuteBeforeAndAfterCommand", @@ -207,9 +208,7 @@ after echo running post-command -------------------------------[ ` + utils.Colored("before", utils.CYAN) + ` ]------------------------------- ------------------------------------------------------------------------------------------------------------ ` + utils.ColoredB("Execution failed during phase \"before\" - Execution of the script \"exit 10\" returned non-zero exit code : 10", utils.RED) + ` ------------------------------------------------------------------------------------------------------------ ` return Test{ Feature: feature, @@ -245,9 +244,7 @@ after echo running post-command running pre-command -------------------------------[ ` + utils.Colored("build", utils.CYAN) + ` ]-------------------------------- -------------------------------------------------------------------------------------------------------------------- ` + utils.ColoredB("Execution failed during phase \"build\" - Execution of the script \"invalid_command\" returned non-zero exit code : 127", utils.RED) + ` -------------------------------------------------------------------------------------------------------------------- ` return Test{ Feature: feature,