mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-06-24 14:32:24 +00:00
Extract env vars to a file + test
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
91e00f9867
commit
37ba52f357
@ -57,4 +57,3 @@ func deleteContext(name string) (deleteName string, wasActiveContext bool, err e
|
|||||||
}
|
}
|
||||||
return name, wasActiveContext, errors.Wrap(kc.Save(), "failed to save kubeconfig file")
|
return name, wasActiveContext, errors.Wrap(kc.Save(), "failed to save kubeconfig file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
cmd/kubectx/env.go
Normal file
26
cmd/kubectx/env.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
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`
|
||||||
|
)
|
||||||
|
|
||||||
|
func useColors() bool {
|
||||||
|
if os.Getenv(EnvForceColor) != "" {
|
||||||
|
return true
|
||||||
|
} else if os.Getenv(EnvNoColor) != "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
44
cmd/kubectx/env_test.go
Normal file
44
cmd/kubectx/env_test.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_useColors_forceColors(t *testing.T) {
|
||||||
|
defer withTestVar("_KUBECTX_FORCE_COLOR", "1")()
|
||||||
|
defer withTestVar("NO_COLOR", "1")()
|
||||||
|
|
||||||
|
if !useColors() {
|
||||||
|
t.Fatal("expected useColors() = true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_useColors_disableColors(t *testing.T) {
|
||||||
|
defer withTestVar("NO_COLOR", "1")()
|
||||||
|
|
||||||
|
if useColors() {
|
||||||
|
t.Fatal("expected useColors() = false")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_useColors_default(t *testing.T) {
|
||||||
|
defer withTestVar("NO_COLOR", "")()
|
||||||
|
defer withTestVar("_KUBECTX_FORCE_COLOR", "")()
|
||||||
|
|
||||||
|
if !useColors() {
|
||||||
|
t.Fatal("expected useColors() = true")
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_parseArgs_new(t *testing.T) {
|
func Test_parseArgs_new(t *testing.T) {
|
||||||
@ -59,7 +60,7 @@ func Test_parseArgs_new(t *testing.T) {
|
|||||||
want: RenameOp{"a", "."}},
|
want: RenameOp{"a", "."}},
|
||||||
{name: "unrecognized flag",
|
{name: "unrecognized flag",
|
||||||
args: []string{"-x"},
|
args: []string{"-x"},
|
||||||
want: UnsupportedOp{Args: []string{"-x"}}},
|
want: UnsupportedOp{Err: errors.Errorf("unsupported option \"-x\"")}},
|
||||||
// TODO add more UnsupportedOp cases
|
// TODO add more UnsupportedOp cases
|
||||||
|
|
||||||
// TODO consider these cases
|
// TODO consider these cases
|
||||||
|
@ -13,10 +13,6 @@ import (
|
|||||||
"github.com/mattn/go-isatty"
|
"github.com/mattn/go-isatty"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
envFZFIgnore = "KUBECTX_IGNORE_FZF"
|
|
||||||
)
|
|
||||||
|
|
||||||
type InteractiveSwitchOp struct {
|
type InteractiveSwitchOp struct {
|
||||||
SelfCmd string
|
SelfCmd string
|
||||||
}
|
}
|
||||||
@ -30,7 +26,7 @@ func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error {
|
|||||||
|
|
||||||
cmd.Env = append(os.Environ(),
|
cmd.Env = append(os.Environ(),
|
||||||
fmt.Sprintf("FZF_DEFAULT_COMMAND=%s", op.SelfCmd),
|
fmt.Sprintf("FZF_DEFAULT_COMMAND=%s", op.SelfCmd),
|
||||||
fmt.Sprintf("%s=1", envForceColor))
|
fmt.Sprintf("%s=1", EnvForceColor))
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
if _, ok := err.(*exec.ExitError); !ok {
|
if _, ok := err.(*exec.ExitError); !ok {
|
||||||
return err
|
return err
|
||||||
@ -64,6 +60,6 @@ func fzfInstalled() bool {
|
|||||||
|
|
||||||
// isInteractiveMode determines if we can do choosing with fzf.
|
// isInteractiveMode determines if we can do choosing with fzf.
|
||||||
func isInteractiveMode(stdout *os.File) bool {
|
func isInteractiveMode(stdout *os.File) bool {
|
||||||
v := os.Getenv(envFZFIgnore)
|
v := os.Getenv(EnvFZFIgnore)
|
||||||
return v == "" && isTerminal(stdout) && fzfInstalled()
|
return v == "" && isTerminal(stdout) && fzfInstalled()
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,9 @@ import (
|
|||||||
|
|
||||||
func TestPrintHelp(t *testing.T) {
|
func TestPrintHelp(t *testing.T) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
printHelp(&buf)
|
if err := (&HelpOp{}).Run(&buf, &buf); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
out := buf.String()
|
out := buf.String()
|
||||||
if !strings.Contains(out, "USAGE:") {
|
if !strings.Contains(out, "USAGE:") {
|
||||||
|
@ -60,7 +60,9 @@ func Test_kubeconfigPath_envOvveride(t *testing.T) {
|
|||||||
defer os.Unsetenv("KUBECONFIG")
|
defer os.Unsetenv("KUBECONFIG")
|
||||||
|
|
||||||
v, err := kubeconfigPath()
|
v, err := kubeconfigPath()
|
||||||
if err != nil { t.Fatal(err)}
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
if expected := "foo"; v != expected {
|
if expected := "foo"; v != expected {
|
||||||
t.Fatalf("expected=%q, got=%q", expected, v)
|
t.Fatalf("expected=%q, got=%q", expected, v)
|
||||||
}
|
}
|
||||||
@ -72,7 +74,9 @@ func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
|
|||||||
defer os.Unsetenv("KUBECONFIG")
|
defer os.Unsetenv("KUBECONFIG")
|
||||||
|
|
||||||
_, err := kubeconfigPath()
|
_, err := kubeconfigPath()
|
||||||
if err == nil { t.Fatal("expected error")}
|
if err == nil {
|
||||||
|
t.Fatal("expected error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testfile(t *testing.T, contents string) (path string, cleanup func()) {
|
func testfile(t *testing.T, contents string) (path string, cleanup func()) {
|
||||||
|
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
|
|
||||||
"facette.io/natsort"
|
"facette.io/natsort"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
@ -45,17 +44,3 @@ func (_ ListOp) Run(stdout, _ io.Writer) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
envForceColor = `_KUBECTX_FORCE_COLOR`
|
|
||||||
envNoColor = `NO_COLOR`
|
|
||||||
)
|
|
||||||
|
|
||||||
func useColors() bool {
|
|
||||||
if os.Getenv(envForceColor) != "" {
|
|
||||||
return true
|
|
||||||
} else if os.Getenv(envNoColor) != "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
@ -30,4 +30,3 @@ func printWarning(w io.Writer, format string, args ...interface{}) {
|
|||||||
func printSuccess(w io.Writer, format string, args ...interface{}) {
|
func printSuccess(w io.Writer, format string, args ...interface{}) {
|
||||||
fmt.Fprintf(w, color.GreenString(fmt.Sprintf(format+"\n", args...)))
|
fmt.Fprintf(w, color.GreenString(fmt.Sprintf(format+"\n", args...)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,4 +69,3 @@ func (op RenameOp) Run(_, stderr io.Writer) error {
|
|||||||
printSuccess(stderr, "Context %q renamed to %q.", op.Old, op.New)
|
printSuccess(stderr, "Context %q renamed to %q.", op.Old, op.New)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,6 @@ func switchContext(name string) (string, error) {
|
|||||||
return name, nil
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// swapContext switches to previously switch context.
|
// swapContext switches to previously switch context.
|
||||||
func swapContext() (string, error) {
|
func swapContext() (string, error) {
|
||||||
prevCtxFile, err := kubectxPrevCtxFile()
|
prevCtxFile, err := kubectxPrevCtxFile()
|
||||||
|
@ -29,4 +29,3 @@ func (_ UnsetOp) Run(_, stderr io.Writer) error {
|
|||||||
_, err := fmt.Fprintln(stderr, "Successfully unset the current context")
|
_, err := fmt.Fprintln(stderr, "Successfully unset the current context")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user