Create printer pkg, fix color force enable/disable

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan
2020-04-12 22:30:10 -07:00
parent bef0a4cca7
commit 7598c4d4dd
13 changed files with 135 additions and 62 deletions

19
internal/env/constants.go vendored Normal file
View File

@@ -0,0 +1,19 @@
package env
const (
// EnvFZFIgnore describes the environment variable to set to disable
// interactive context selection when fzf is installed.
EnvFZFIgnore = "KUBECTX_IGNORE_FZF"
// EnvForceColor describes the environment variable to disable color usage
// when printing current context in a list.
EnvNoColor = `NO_COLOR`
// EnvForceColor describes the "internal" environment variable to force
// color usage to show current context in a list.
EnvForceColor = `_KUBECTX_FORCE_COLOR`
// EnvDebug describes the internal environment variable for more verbose logging.
EnvDebug = `DEBUG`
)

20
internal/printer/color.go Normal file
View File

@@ -0,0 +1,20 @@
package printer
import (
"os"
"github.com/ahmetb/kubectx/internal/env"
)
// UseColors returns true if colors are force-enabled,
// false if colors are disabled, or nil for default behavior
// which is determined based on factors like if stdout is tty.
func UseColors() *bool {
tr, fa := true, false
if os.Getenv(env.EnvForceColor) != "" {
return &tr
} else if os.Getenv(env.EnvNoColor) != "" {
return &fa
}
return nil
}

View File

@@ -0,0 +1,50 @@
package printer
import (
"os"
"testing"
"github.com/google/go-cmp/cmp"
)
func withTestVar(key, value string) func() {
orig, ok := os.LookupEnv(key)
os.Setenv(key, value)
return func() {
if ok {
os.Setenv(key, orig)
} else {
os.Unsetenv(key)
}
}
}
var (
tr, fa = true, false
)
func Test_useColors_forceColors(t *testing.T) {
defer withTestVar("_KUBECTX_FORCE_COLOR", "1")()
defer withTestVar("NO_COLOR", "1")()
if v := UseColors(); !cmp.Equal(v, &tr) {
t.Fatalf("expected UseColors() = true; got = %v", v)
}
}
func Test_useColors_disableColors(t *testing.T) {
defer withTestVar("NO_COLOR", "1")()
if v := UseColors(); !cmp.Equal(v, &fa) {
t.Fatalf("expected UseColors() = false; got = %v", v)
}
}
func Test_useColors_default(t *testing.T) {
defer withTestVar("NO_COLOR", "")()
defer withTestVar("_KUBECTX_FORCE_COLOR", "")()
if v := UseColors(); v != nil {
t.Fatalf("expected UseColors() = nil; got=%v", *v)
}
}

View File

@@ -0,0 +1,42 @@
package printer
import (
"fmt"
"io"
"github.com/fatih/color"
)
var (
errorColor = color.New(color.FgRed, color.Bold)
warningColor = color.New(color.FgYellow, color.Bold)
successColor = color.New(color.FgGreen)
)
func init() {
colors := UseColors()
if colors == nil {
return
}
if *colors {
errorColor.EnableColor()
warningColor.EnableColor()
successColor.EnableColor()
} else {
errorColor.DisableColor()
warningColor.DisableColor()
successColor.DisableColor()
}
}
func Error(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, color.RedString("error: ")+format+"\n", args...)
}
func Warning(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, color.YellowString("warning: ")+format+"\n", args...)
}
func Success(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, color.GreenString(fmt.Sprintf(format+"\n", args...)))
}