Skip to content

Commit

Permalink
Add phase banners to the tops of each phase (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-howell authored and gopinath-langote committed Sep 30, 2019
1 parent caa32b7 commit c84764b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand Down
38 changes: 36 additions & 2 deletions cmd/models/onebuild-execution-plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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),
)
}
5 changes: 5 additions & 0 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions testing/fixtures/execute_cmd_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Phase Command
build echo building project
-------------------------------[ ` + aurora.BrightCyan("build").String() + ` ]--------------------------------
building project
` + aurora.BrightGreen("SUCCESS").Bold().String() + `
Expand Down Expand Up @@ -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() + `
Expand Down Expand Up @@ -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() + `
Expand Down Expand Up @@ -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() + `
Expand Down Expand Up @@ -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() + `
Expand Down Expand Up @@ -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() + `
Expand Down

0 comments on commit c84764b

Please sign in to comment.