Repository: grafana/tanka (go)

Issue:
tk apply diff is not colored anymore
I've just upgraded to tanka `master` and noticed that the diff displayed by `tk apply` is not colored anymore. The previous version I was running was `0.5` and the output was colored there.

Pull request:
fix(cli): restore diff coloring on apply
Lost that functionality somewhere during refactoring, but it really aids
readability, so we do need this.

Fixes #209 

Patch:
diff --git a/cmd/tk/util.go b/cmd/tk/util.go
index 6d328431..c8721aa8 100644
--- a/cmd/tk/util.go
+++ b/cmd/tk/util.go
@@ -1,17 +1,13 @@
 package main
 
 import (
-	"bytes"
 	"encoding/json"
 	"fmt"
 	"io"
 	"io/ioutil"
 	"os"
 	"os/exec"
-	"regexp"
 	"strings"
-
-	"github.com/fatih/color"
 )
 
 func pageln(i ...interface{}) {
@@ -44,35 +40,6 @@ func fPageln(r io.Reader) {
 	}
 }
 
-func colordiff(d string) io.Reader {
-	exps := map[string]func(s string) bool{
-		"add":  regexp.MustCompile(`^\+.*`).MatchString,
-		"del":  regexp.MustCompile(`^\-.*`).MatchString,
-		"head": regexp.MustCompile(`^diff -u -N.*`).MatchString,
-		"hid":  regexp.MustCompile(`^@.*`).MatchString,
-	}
-
-	buf := bytes.Buffer{}
-	lines := strings.Split(d, "\n")
-
-	for _, l := range lines {
-		switch {
-		case exps["add"](l):
-			color.New(color.FgGreen).Fprintln(&buf, l)
-		case exps["del"](l):
-			color.New(color.FgRed).Fprintln(&buf, l)
-		case exps["head"](l):
-			color.New(color.FgBlue, color.Bold).Fprintln(&buf, l)
-		case exps["hid"](l):
-			color.New(color.FgMagenta, color.Bold).Fprintln(&buf, l)
-		default:
-			fmt.Fprintln(&buf, l)
-		}
-	}
-
-	return &buf
-}
-
 // writeJSON writes the given object to the path as a JSON file
 func writeJSON(i interface{}, path string) error {
 	out, err := json.MarshalIndent(i, "", "  ")
diff --git a/cmd/tk/workflow.go b/cmd/tk/workflow.go
index d2c17f3a..9cea31a4 100644
--- a/cmd/tk/workflow.go
+++ b/cmd/tk/workflow.go
@@ -10,6 +10,7 @@ import (
 	"github.com/spf13/cobra"
 	"github.com/spf13/pflag"
 
+	"github.com/grafana/tanka/pkg/cli"
 	"github.com/grafana/tanka/pkg/cli/cmp"
 	"github.com/grafana/tanka/pkg/kubernetes/util"
 	"github.com/grafana/tanka/pkg/tanka"
@@ -103,7 +104,7 @@ func diffCmd() *cobra.Command {
 		}
 
 		if interactive {
-			r := colordiff(*changes)
+			r := cli.Colordiff(*changes)
 			fPageln(r)
 		} else {
 			fmt.Println(*changes)
diff --git a/pkg/cli/colordiff.go b/pkg/cli/colordiff.go
new file mode 100644
index 00000000..6575b185
--- /dev/null
+++ b/pkg/cli/colordiff.go
@@ -0,0 +1,40 @@
+package cli
+
+import (
+	"bytes"
+	"fmt"
+	"regexp"
+	"strings"
+
+	"github.com/fatih/color"
+)
+
+// Colordiff colorizes unified diff output (diff -u -N)
+func Colordiff(d string) *bytes.Buffer {
+	exps := map[string]func(s string) bool{
+		"add":  regexp.MustCompile(`^\+.*`).MatchString,
+		"del":  regexp.MustCompile(`^\-.*`).MatchString,
+		"head": regexp.MustCompile(`^diff -u -N.*`).MatchString,
+		"hid":  regexp.MustCompile(`^@.*`).MatchString,
+	}
+
+	buf := bytes.Buffer{}
+	lines := strings.Split(d, "\n")
+
+	for _, l := range lines {
+		switch {
+		case exps["add"](l):
+			color.New(color.FgGreen).Fprintln(&buf, l)
+		case exps["del"](l):
+			color.New(color.FgRed).Fprintln(&buf, l)
+		case exps["head"](l):
+			color.New(color.FgBlue, color.Bold).Fprintln(&buf, l)
+		case exps["hid"](l):
+			color.New(color.FgMagenta, color.Bold).Fprintln(&buf, l)
+		default:
+			fmt.Fprintln(&buf, l)
+		}
+	}
+
+	return &buf
+}
diff --git a/pkg/tanka/workflow.go b/pkg/tanka/workflow.go
index 011fe1c4..a69ab150 100644
--- a/pkg/tanka/workflow.go
+++ b/pkg/tanka/workflow.go
@@ -5,6 +5,7 @@ import (
 
 	"github.com/pkg/errors"
 
+	"github.com/grafana/tanka/pkg/cli"
 	"github.com/grafana/tanka/pkg/kubernetes"
 	"github.com/grafana/tanka/pkg/kubernetes/manifest"
 )
@@ -35,7 +36,8 @@ func Apply(baseDir string, mods ...Modifier) error {
 		tmp := "Warning: There are no differences. Your apply may not do anything at all."
 		diff = &tmp
 	}
-	fmt.Println(*diff)
+	b := cli.Colordiff(*diff)
+	fmt.Print(b.String())
 
 	return kube.Apply(p.Resources, opts.apply)
 }


Test patch:
diff --git a/cmd/tk/util_test.go b/pkg/cli/colordiff_test.go
similarity index 99%
rename from cmd/tk/util_test.go
rename to pkg/cli/colordiff_test.go
index 4851e14f..13d1b32d 100644
--- a/cmd/tk/util_test.go
+++ b/pkg/cli/colordiff_test.go
@@ -1,4 +1,4 @@
-package main
+package cli
 
 import (
 	"io/ioutil"
@@ -86,7 +86,7 @@ func TestColordiff(t *testing.T) {
 		"", // newline
 	}, "\n")
 
-	r := colordiff(data)
+	r := Colordiff(data)
 	got, err := ioutil.ReadAll(r)
 	require.NoError(t, err)