Skip to content

Commit

Permalink
Add color to output
Browse files Browse the repository at this point in the history
  • Loading branch information
keimoon committed Jun 25, 2018
1 parent 6872c1c commit e20cebf
Show file tree
Hide file tree
Showing 397 changed files with 182,691 additions and 39 deletions.
43 changes: 40 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions buildtree/buildtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (t *BuildTree) Build() error {
if err != nil {
return err
}
fmt.Println("Building new images")
utils.Info("Building new images")
for _, node := range t.rootNodes {
err = t.buildNodeAndChildren(node)
if err != nil {
Expand All @@ -217,7 +217,7 @@ func (t *BuildTree) TryBuild() error {
if err != nil {
return err
}
fmt.Println("Try building new images")
utils.Info("Try building new images")
for _, node := range t.rootNodes {
err = t.tryBuildNodeAndChildren(node)
if err != nil {
Expand All @@ -233,14 +233,14 @@ func (t *BuildTree) Push() error {
if err != nil {
return err
}
fmt.Println("Logging into registry")
utils.Info("Logging into registry")
for _, credential := range t.credentials {
err = utils.DockerLogin(credential.Registry, credential.Username, credential.Password)
if err != nil {
return err
}
}
fmt.Println("Pushing new images")
utils.Info("Pushing new images")
for _, node := range t.rootNodes {
err = t.pushNodeAndChildren(node)
if err != nil {
Expand Down Expand Up @@ -273,7 +273,7 @@ func (t *BuildTree) Clean() {
if node.buildRoot != "provided" {
err := utils.DockerRMI(node.name, node.tag)
if err != nil {
utils.PrintError(err)
utils.Error(err)
}
}
}
Expand Down Expand Up @@ -379,16 +379,16 @@ func (t *BuildTree) needBuild(node *buildNode) bool {

func (t *BuildTree) buildNodeAndChildren(node *buildNode) error {
if !t.needBuild(node) {
fmt.Printf("====> Skipping %s\n", node.name)
utils.Info2("====> Skipping %s", node.name)
} else {
fmt.Printf("====> Building %s:%s\n", node.name, node.tag)
utils.Info2("====> Building %s:%s", node.name, node.tag)
err := t.buildNode(node, node.tag)
if err != nil {
return err
}
if node.pushLatest {
latestTag := "latest"
fmt.Printf("====> Building %s:%s\n", node.name, latestTag)
utils.Info2("====> Building %s:%s", node.name, latestTag)
err = t.buildNode(node, latestTag)
if err != nil {
return err
Expand All @@ -406,18 +406,18 @@ func (t *BuildTree) buildNodeAndChildren(node *buildNode) error {

func (t *BuildTree) tryBuildNodeAndChildren(node *buildNode) error {
if !t.needBuild(node) {
fmt.Printf("====> Skipping %s\n", node.name)
utils.Info2("====> Skipping %s", node.name)
} else {
randomTag := fmt.Sprintf("%s-%d", node.tag, time.Now().UnixNano())
fmt.Printf("====> Building %s:%s\n", node.name, randomTag)
utils.Info2("====> Building %s:%s", node.name, randomTag)
err := t.buildNode(node, randomTag)
if err != nil {
return err
}
fmt.Printf("====> Removing %s:%s\n", node.name, randomTag)
utils.Info2("====> Removing %s:%s", node.name, randomTag)
err = utils.DockerRMI(node.name, randomTag)
if err != nil {
utils.PrintError(err)
utils.Error(err)
}
}
for _, child := range node.children {
Expand Down Expand Up @@ -455,16 +455,16 @@ func (t *BuildTree) resolveShellCommandPath(buildRoot, command string) string {

func (t *BuildTree) pushNodeAndChildren(node *buildNode) error {
if !t.needBuild(node) {
fmt.Printf("====> Skipping %s\n", node.name)
utils.Info2("====> Skipping %s", node.name)
} else {
fmt.Printf("====> Pushing %s:%s\n", node.name, node.tag)
utils.Info2("====> Pushing %s:%s", node.name, node.tag)
err := utils.DockerPush(node.name, node.tag)
if err != nil {
return err
}
if node.pushLatest {
latestTag := "latest"
fmt.Printf("====> Pushing %s:%s\n", node.name, latestTag)
utils.Info2("====> Pushing %s:%s", node.name, latestTag)
err := utils.DockerPush(node.name, latestTag)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ var buildCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
t, err := buildtree.ReadBuildTreeFromFile(cfgFile, variableMap, variableFiles)
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
err = t.Prepare()
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
err = t.Build()
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var cleanCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
t, err := buildtree.ReadBuildTreeFromFile(cfgFile, variableMap, variableFiles)
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
t.Clean()
Expand Down
4 changes: 2 additions & 2 deletions cmd/dryrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ var dryrunCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
t, err := buildtree.ReadBuildTreeFromFile(cfgFile, variableMap, variableFiles)
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
err = t.Prepare()
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
t.PrintTree(dryrunNoColor)
Expand Down
4 changes: 2 additions & 2 deletions cmd/findlatest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ Currently, only images hosted in gcr.io are supported by this command.
Run: func(cmd *cobra.Command, args []string) {
t, err := buildtree.ReadBuildTreeFromFile(cfgFile, variableMap, variableFiles)
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
tag, err := t.FindLatestTag(args[0])
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
fmt.Println(tag)
Expand Down
6 changes: 3 additions & 3 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ var pushCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
t, err := buildtree.ReadBuildTreeFromFile(cfgFile, variableMap, variableFiles)
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
err = t.Prepare()
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
err = t.Push()
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
},
Expand Down
6 changes: 3 additions & 3 deletions cmd/trybuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ var trybuildCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
t, err := buildtree.ReadBuildTreeFromFile(cfgFile, variableMap, variableFiles)
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
err = t.Prepare()
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
err = t.TryBuild()
if err != nil {
utils.PrintError(err)
utils.Error(err)
os.Exit(1)
}
},
Expand Down
48 changes: 41 additions & 7 deletions utils/common.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package utils

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/fatih/color"
"github.com/palantir/stacktrace"
)

// Version returns doriath version
func Version() string {
return "1.4.3"
return "1.4.4"
}

// ResolveDir appends a path to a rootDir
Expand All @@ -26,13 +26,47 @@ func ResolveDir(rootDir, path string) string {
return filepath.Join(rootDir, path)
}

// PrintError prints additional stack trace if DEBUG is set to true
func PrintError(err error) {
if os.Getenv("DEBUG") == "true" {
fmt.Fprintln(os.Stderr, "ERROR:", err)
// Info .
func Info(msg string, args ...interface{}) {
color.New(color.FgBlue).Printf(appendNewLine(msg), args...)
}

// Info2 .
func Info2(msg string, args ...interface{}) {
color.New(color.FgCyan).Printf(appendNewLine(msg), args...)
}

// Warn .
func Warn(msg string, args ...interface{}) {
color.New(color.FgYellow).Printf(appendNewLine(msg), args...)
}

// Success .
func Success(msg string, args ...interface{}) {
color.New(color.FgGreen).Printf(appendNewLine(msg), args...)
}

// Error .
func Error(err error) {
debug := os.Getenv("DEBUG")
if debug == "true" || debug == "1" {
color.New(color.FgRed).Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stderr, "ERROR:", stacktrace.RootCause(err))
color.New(color.FgRed).Fprintln(os.Stderr, stacktrace.RootCause(err))
}
}

// Fatal .
func Fatal(err error) {
Error(err)
os.Exit(1)
}

func appendNewLine(msg string) string {
if strings.HasSuffix(msg, "\n") {
return msg
}
return msg + "\n"
}

// DetectRequirement detects requirement for doriath
Expand Down
5 changes: 5 additions & 0 deletions vendor/github.com/fatih/color/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/fatih/color/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e20cebf

Please sign in to comment.