Skip to content

Commit

Permalink
Print total results banner at the end of execution (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
budevg authored and gopinath-langote committed Oct 1, 2019
1 parent b1682f2 commit e96cc7b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
23 changes: 15 additions & 8 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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)
}
}
Expand Down
39 changes: 32 additions & 7 deletions cmd/utils/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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())
}
21 changes: 9 additions & 12 deletions testing/fixtures/execute_cmd_fixtures.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e96cc7b

Please sign in to comment.