From 4bbe0fad79825c7c0860b69a4f6b869a1ff4b826 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Wed, 29 Apr 2020 12:11:59 -0700 Subject: [PATCH] deprecation msgs for KUBECTX_CURRENT_{BG,FG}COLOR Signed-off-by: Ahmet Alp Balkan --- cmd/kubectx/list.go | 9 +------- cmd/kubectx/main.go | 3 +++ cmd/kubens/list.go | 4 +--- cmd/kubens/main.go | 2 ++ internal/cmdutil/deprecated.go | 22 ++++++++++++++++++ internal/cmdutil/deprecated_test.go | 35 +++++++++++++++++++++++++++++ internal/printer/color.go | 8 +++++++ 7 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 internal/cmdutil/deprecated.go create mode 100644 internal/cmdutil/deprecated_test.go diff --git a/cmd/kubectx/list.go b/cmd/kubectx/list.go index 361efcd..1a8976d 100644 --- a/cmd/kubectx/list.go +++ b/cmd/kubectx/list.go @@ -5,7 +5,6 @@ import ( "io" "facette.io/natsort" - "github.com/fatih/color" "github.com/pkg/errors" "github.com/ahmetb/kubectx/internal/cmdutil" @@ -30,17 +29,11 @@ func (_ ListOp) Run(stdout, stderr io.Writer) error { ctxs := kc.ContextNames() natsort.Sort(ctxs) - // TODO support KUBECTX_CURRENT_FGCOLOR - // TODO support KUBECTX_CURRENT_BGCOLOR - - currentColor := color.New(color.FgGreen, color.Bold) - printer.EnableOrDisableColor(currentColor) - cur := kc.GetCurrentContext() for _, c := range ctxs { s := c if c == cur { - s = currentColor.Sprint(c) + s = printer.ActiveItemColor.Sprint(c) } fmt.Fprintf(stdout, "%s\n", s) } diff --git a/cmd/kubectx/main.go b/cmd/kubectx/main.go index fcfc75d..8da7b39 100644 --- a/cmd/kubectx/main.go +++ b/cmd/kubectx/main.go @@ -5,6 +5,7 @@ import ( "io" "os" + "github.com/ahmetb/kubectx/internal/cmdutil" "github.com/ahmetb/kubectx/internal/env" "github.com/ahmetb/kubectx/internal/printer" ) @@ -14,6 +15,8 @@ type Op interface { } func main() { + cmdutil.PrintDeprecatedEnvWarnings(os.Stderr, os.Environ()) + op := parseArgs(os.Args[1:]) if err := op.Run(os.Stdout, os.Stderr); err != nil { printer.Error(os.Stderr, err.Error()) diff --git a/cmd/kubens/list.go b/cmd/kubens/list.go index d6fb4e4..0a244dd 100644 --- a/cmd/kubens/list.go +++ b/cmd/kubens/list.go @@ -39,13 +39,11 @@ func (op ListOp) Run(stdout, stderr io.Writer) error { return errors.Wrap(err, "could not list namespaces (is the cluster accessible?)") } - currentColor := color.New(color.FgGreen, color.Bold) - printer.EnableOrDisableColor(currentColor) for _, c := range ns { s := c if c == curNs { - s = currentColor.Sprint(c) + s = printer.ActiveItemColor.Sprint(c) } fmt.Fprintf(stdout, "%s\n", s) } diff --git a/cmd/kubens/main.go b/cmd/kubens/main.go index fcfc75d..f3e895f 100644 --- a/cmd/kubens/main.go +++ b/cmd/kubens/main.go @@ -5,6 +5,7 @@ import ( "io" "os" + "github.com/ahmetb/kubectx/internal/cmdutil" "github.com/ahmetb/kubectx/internal/env" "github.com/ahmetb/kubectx/internal/printer" ) @@ -14,6 +15,7 @@ type Op interface { } func main() { + cmdutil.PrintDeprecatedEnvWarnings(os.Stderr, os.Environ()) op := parseArgs(os.Args[1:]) if err := op.Run(os.Stdout, os.Stderr); err != nil { printer.Error(os.Stderr, err.Error()) diff --git a/internal/cmdutil/deprecated.go b/internal/cmdutil/deprecated.go new file mode 100644 index 0000000..96b7db8 --- /dev/null +++ b/internal/cmdutil/deprecated.go @@ -0,0 +1,22 @@ +package cmdutil + +import ( + "io" + "strings" + + "github.com/ahmetb/kubectx/internal/printer" +) + +func PrintDeprecatedEnvWarnings(out io.Writer, vars []string) { + for _, vv := range vars { + parts := strings.SplitN(vv, "=", 2) + if len(parts) != 2 { + continue + } + key := parts[0] + + if key == `KUBECTX_CURRENT_FGCOLOR` || key == `KUBECTX_CURRENT_BGCOLOR` { + printer.Warning(out,"%s environment variable is now deprecated", key) + } + } +} diff --git a/internal/cmdutil/deprecated_test.go b/internal/cmdutil/deprecated_test.go new file mode 100644 index 0000000..6ac47a6 --- /dev/null +++ b/internal/cmdutil/deprecated_test.go @@ -0,0 +1,35 @@ +package cmdutil + +import ( + "bytes" + "strings" + "testing" +) + +func TestPrintDeprecatedEnvWarnings_noDeprecatedVars(t *testing.T){ + var out bytes.Buffer + PrintDeprecatedEnvWarnings(&out, []string{ + "A=B", + "PATH=/foo:/bar:/bin", + }) + if v := out.String(); len(v) > 0{ + t.Fatalf("something written to buf: %v", v) + } +} + + +func TestPrintDeprecatedEnvWarnings_bgColors(t *testing.T){ + var out bytes.Buffer + + PrintDeprecatedEnvWarnings(&out, []string{ + "KUBECTX_CURRENT_FGCOLOR=1", + "KUBECTX_CURRENT_BGCOLOR=2", + }) + v := out.String() + if !strings.Contains(v, "KUBECTX_CURRENT_FGCOLOR"){ + t.Fatalf("output doesn't contain 'KUBECTX_CURRENT_FGCOLOR': %q", v) + } + if !strings.Contains(v, "KUBECTX_CURRENT_BGCOLOR"){ + t.Fatalf("output doesn't contain 'KUBECTX_CURRENT_BGCOLOR': %q", v) + } +} diff --git a/internal/printer/color.go b/internal/printer/color.go index 8f87f5b..8c118f5 100644 --- a/internal/printer/color.go +++ b/internal/printer/color.go @@ -8,6 +8,14 @@ import ( "github.com/ahmetb/kubectx/internal/env" ) +var ( + ActiveItemColor = color.New(color.FgGreen, color.Bold) +) + +func init(){ + EnableOrDisableColor(ActiveItemColor) +} + // 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.