Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add phase banners to the tops of each phase #157

Merged
merged 4 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 35 additions & 2 deletions cmd/models/onebuild-execution-plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package models

import (
"fmt"
"github.com/codeskyblue/go-sh"
"github.com/gopinath-langote/1build/cmd/utils"
"os"
"strings"
"text/tabwriter"

"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 +25,13 @@ type CommandContext struct {
CommandSession *sh.Session
}

const (
commandBannerLength = 72
ian-howell marked this conversation as resolved.
Show resolved Hide resolved

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 +99,27 @@ func longestPhaseAndCommandValue(executionPlan *OneBuildExecutionPlan) (string,
func dashesOfLength(text string) string {
return strings.Repeat("-", len(text))
}

// PrintBanner prints the CommandContext's name in a 72-character banner
ian-howell marked this conversation as resolved.
Show resolved Hide resolved
func (c *CommandContext) PrintBanner() {
centreLength := len(c.Name) + len(bannerOpen) + len(bannerClose)
ian-howell marked this conversation as resolved.
Show resolved Hide resolved
totalDashes := commandBannerLength - 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),
)
}
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