mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-07-19 09:39:36 +00:00
deprecation msgs for KUBECTX_CURRENT_{BG,FG}COLOR
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
5b745727c3
commit
4bbe0fad79
@ -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)
|
||||
}
|
||||
|
@ -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())
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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())
|
||||
|
22
internal/cmdutil/deprecated.go
Normal file
22
internal/cmdutil/deprecated.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
35
internal/cmdutil/deprecated_test.go
Normal file
35
internal/cmdutil/deprecated_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user