From c84764bedb20cf2e183757d14b856e6421a2bc7f Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Mon, 30 Sep 2019 07:50:54 -0500 Subject: [PATCH] Add phase banners to the tops of each phase (#157) --- cmd/exec/exec.go | 1 + cmd/models/onebuild-execution-plan.go | 38 ++++++++++++++++++++++-- cmd/utils/utils.go | 5 ++++ testing/fixtures/execute_cmd_fixtures.go | 11 +++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index 4efb7fac..9ebab327 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -41,6 +41,7 @@ func ExecutePlan(commands ...string) { } func executeAndStopIfFailed(command *models.CommandContext) { + command.PrintBanner() err := command.CommandSession.Run() if err != nil { exitCode := (err.Error())[12:] diff --git a/cmd/models/onebuild-execution-plan.go b/cmd/models/onebuild-execution-plan.go index 5a907b5e..8a1f017d 100644 --- a/cmd/models/onebuild-execution-plan.go +++ b/cmd/models/onebuild-execution-plan.go @@ -2,11 +2,14 @@ package models import ( "fmt" - "github.com/codeskyblue/go-sh" - "github.com/gopinath-langote/1build/cmd/utils" "os" "strings" "text/tabwriter" + "unicode/utf8" + + "github.com/codeskyblue/go-sh" + "github.com/gopinath-langote/1build/cmd/utils" + "github.com/logrusorgru/aurora" ) // OneBuildExecutionPlan holds all information for the execution strategy @@ -23,6 +26,11 @@ type CommandContext struct { CommandSession *sh.Session } +const ( + bannerOpen = "[ " + bannerClose = " ]" +) + // HasBefore return true if plan contains before section else false func (executionPlan *OneBuildExecutionPlan) HasBefore() bool { return executionPlan.Before != nil @@ -90,3 +98,29 @@ func longestPhaseAndCommandValue(executionPlan *OneBuildExecutionPlan) (string, func dashesOfLength(text string) string { return strings.Repeat("-", len(text)) } + +// PrintBanner prints the CommandContext's name in a banner of the standard length +func (c *CommandContext) PrintBanner() { + centreLength := utf8.RuneCountInString(c.Name) + + utf8.RuneCountInString(bannerOpen) + + utf8.RuneCountInString(bannerClose) + totalDashes := utils.MaxOutputWidth - centreLength + + // Intentional integer division + numDashesLeft := totalDashes / 2 + numDashesRight := totalDashes / 2 + + // If we need an extra dash, let's add it on the right. + // This way, similar length aliases will line up + if totalDashes%2 == 1 { + numDashesRight++ + } + + fmt.Printf("%s%s%s%s%s\n", + strings.Repeat("-", numDashesLeft), + bannerOpen, + aurora.BrightCyan(c.Name), + bannerClose, + strings.Repeat("-", numDashesRight), + ) +} diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go index 23fa3356..da02993b 100644 --- a/cmd/utils/utils.go +++ b/cmd/utils/utils.go @@ -5,6 +5,11 @@ import ( "strconv" ) +const ( + // MaxOutputWidth is the number of spaces to use on a console + MaxOutputWidth = 72 +) + // Exit exits the current process with specified exit code func Exit(code int) { os.Exit(code) diff --git a/testing/fixtures/execute_cmd_fixtures.go b/testing/fixtures/execute_cmd_fixtures.go index 24615f49..d4e4bbde 100644 --- a/testing/fixtures/execute_cmd_fixtures.go +++ b/testing/fixtures/execute_cmd_fixtures.go @@ -35,6 +35,7 @@ Phase Command build echo building project +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- building project ` + aurora.BrightGreen("SUCCESS").Bold().String() + ` @@ -94,7 +95,9 @@ before echo running pre-command build echo building project +-------------------------------[ ` + aurora.BrightCyan("before").String() + ` ]------------------------------- running pre-command +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- building project ` + aurora.BrightGreen("SUCCESS").Bold().String() + ` @@ -127,7 +130,9 @@ build echo building project after echo running post-command +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- building project +-------------------------------[ ` + aurora.BrightCyan("after").String() + ` ]-------------------------------- running post-command ` + aurora.BrightGreen("SUCCESS").Bold().String() + ` @@ -162,8 +167,11 @@ build echo building project after echo running post-command +-------------------------------[ ` + aurora.BrightCyan("before").String() + ` ]------------------------------- running pre-command +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- building project +-------------------------------[ ` + aurora.BrightCyan("after").String() + ` ]-------------------------------- running post-command ` + aurora.BrightGreen("SUCCESS").Bold().String() + ` @@ -198,6 +206,7 @@ build echo building project after echo running post-command +-------------------------------[ ` + aurora.BrightCyan("before").String() + ` ]------------------------------- ----------------------------------------------------------------------------------------------------------- ` + aurora.Red("Execution failed during phase \"before\" - Execution of the script \"exit 10\" returned non-zero exit code : 10").Bold().String() + ` @@ -233,7 +242,9 @@ build invalid_command after echo running post-command +-------------------------------[ ` + aurora.BrightCyan("before").String() + ` ]------------------------------- running pre-command +-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]-------------------------------- ------------------------------------------------------------------------------------------------------------------- ` + aurora.Red("Execution failed during phase \"build\" - Execution of the script \"invalid_command\" returned non-zero exit code : 127").Bold().String() + `