Skip to content

Commit

Permalink
Merge pull request #49 from asteris-llc/feature/color-summaries
Browse files Browse the repository at this point in the history
color summaries
  • Loading branch information
BrianHicks committed Jun 7, 2016
2 parents 47ccc76 + 5d4118b commit 5da5daf
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Godeps/Godeps.json

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

12 changes: 11 additions & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"golang.org/x/net/context"

"github.com/Sirupsen/logrus"
"github.com/acmacalister/skittles"
"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,7 +98,16 @@ var applyCmd = &cobra.Command{
}
}

fmt.Printf("\nApply complete. %d changes, %d successful, %d failed\n", counts.results, counts.success, counts.failures)
// summarize the changes for the user
summary := fmt.Sprintf("\nApply complete. %d changes, %d successful, %d failed\n", counts.results, counts.success, counts.failures)
if UseColor() {
if counts.failures > 0 {
summary = skittles.Red(summary)
} else {
summary = skittles.Green(summary)
}
}
fmt.Print(summary)

if counts.failures > 0 {
os.Exit(1)
Expand Down
12 changes: 11 additions & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"golang.org/x/net/context"

"github.com/Sirupsen/logrus"
"github.com/acmacalister/skittles"
"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -80,7 +81,16 @@ var planCmd = &cobra.Command{
}
}

fmt.Printf("\nPlan complete. %d checks, %d will change\n", counts.results, counts.changes)
// summarize the potential changes for the user
summary := fmt.Sprintf("\nPlan complete. %d checks, %d will change\n", counts.results, counts.changes)
if UseColor() {
if counts.changes > 0 {
summary = skittles.Yellow(summary)
} else {
summary = skittles.Green(summary)
}
}
fmt.Print(summary)
}
},
}
Expand Down
10 changes: 10 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmd

import (
"runtime"

"github.com/Sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand All @@ -26,3 +28,11 @@ func viperBindPFlags(flags *pflag.FlagSet) {
logrus.WithError(err).Fatal("could not bind flags")
}
}

// UseColor tells us whether or not to print colors using ANSI escape sequences
// based on the following: 1. If we're in a color terminal 2. If the user has
// specified the `nocolor` option (deduced via Viper) 3. If we're on Windows.
func UseColor() bool {
isColorTerminal := logrus.IsTerminal() && (runtime.GOOS != "windows")
return !viper.GetBool("nocolor") && isColorTerminal
}
26 changes: 26 additions & 0 deletions cmd/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd_test

import (
"testing"

"github.com/asteris-llc/converge/cmd"
"github.com/stretchr/testify/assert"
)

func TestUseColor(t *testing.T) {
assert.NotPanics(t, func() { cmd.UseColor() })
}

0 comments on commit 5da5daf

Please sign in to comment.