mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-06-19 20:23:23 +00:00
Implement list (via exec kubectl), clearer color settings
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
3e34177cb9
commit
d0c352c5bf
@ -34,11 +34,7 @@ func (_ ListOp) Run(stdout, stderr io.Writer) error {
|
|||||||
// TODO support KUBECTX_CURRENT_BGCOLOR
|
// TODO support KUBECTX_CURRENT_BGCOLOR
|
||||||
|
|
||||||
currentColor := color.New(color.FgGreen, color.Bold)
|
currentColor := color.New(color.FgGreen, color.Bold)
|
||||||
if v := printer.UseColors(); v != nil && *v {
|
printer.EnableOrDisableColor(currentColor)
|
||||||
currentColor.EnableColor()
|
|
||||||
} else if v != nil && !*v {
|
|
||||||
currentColor.DisableColor()
|
|
||||||
}
|
|
||||||
|
|
||||||
cur := kc.GetCurrentContext()
|
cur := kc.GetCurrentContext()
|
||||||
for _, c := range ctxs {
|
for _, c := range ctxs {
|
||||||
|
@ -1,11 +1,80 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/ahmetb/kubectx/internal/cmdutil"
|
||||||
|
"github.com/ahmetb/kubectx/internal/kubeconfig"
|
||||||
|
"github.com/ahmetb/kubectx/internal/printer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ListOp struct{}
|
type ListOp struct{}
|
||||||
|
|
||||||
func (op ListOp) Run(stdout, stderr io.Writer) error {
|
func (op ListOp) Run(stdout, stderr io.Writer) error {
|
||||||
panic("implement me")
|
kc := new(kubeconfig.Kubeconfig).WithLoader(cmdutil.DefaultLoader)
|
||||||
|
defer kc.Close()
|
||||||
|
if err := kc.Parse(); err != nil {
|
||||||
|
if cmdutil.IsNotFoundErr(err) {
|
||||||
|
printer.Warning(stderr, "kubeconfig file not found")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.Wrap(err, "kubeconfig error")
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := kc.GetCurrentContext()
|
||||||
|
if ctx == "" {
|
||||||
|
return errors.New("current-context is not set")
|
||||||
|
}
|
||||||
|
curNs, err := kc.NamespaceOfContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "cannot read current namespace")
|
||||||
|
}
|
||||||
|
|
||||||
|
kubectl, err := findKubectl()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ns, err := queryNamespaces(kubectl)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
currentColor := color.New(color.FgGreen, color.Bold)
|
||||||
|
printer.EnableOrDisableColor(currentColor)
|
||||||
|
|
||||||
|
for _, c := range ns {
|
||||||
|
s := c
|
||||||
|
if c == curNs {
|
||||||
|
s = currentColor.Sprint(c)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(stdout, "%s\n", s)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func findKubectl() (string, error) {
|
||||||
|
if v := os.Getenv("KUBECTL"); v != "" {
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
v, err := exec.LookPath("kubectl")
|
||||||
|
return v, errors.Wrap(err, "kubectl not found, needed for kubens")
|
||||||
|
}
|
||||||
|
|
||||||
|
func queryNamespaces(kubectl string) ([]string, error) {
|
||||||
|
var b bytes.Buffer
|
||||||
|
cmd := exec.Command(kubectl, "get", "namespaces", `-o=jsonpath={range .items[*].metadata.name}{@}{"\n"}{end}`)
|
||||||
|
cmd.Env = os.Environ()
|
||||||
|
cmd.Stdout, cmd.Stderr = &b, &b
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "failed to query namespaces: %v", b.String())
|
||||||
|
}
|
||||||
|
return strings.Split(strings.TrimSpace(b.String()), "\n"), nil
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,15 @@ package printer
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
|
||||||
"github.com/ahmetb/kubectx/internal/env"
|
"github.com/ahmetb/kubectx/internal/env"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UseColors returns true if colors are force-enabled,
|
// useColors returns true if colors are force-enabled,
|
||||||
// false if colors are disabled, or nil for default behavior
|
// false if colors are disabled, or nil for default behavior
|
||||||
// which is determined based on factors like if stdout is tty.
|
// which is determined based on factors like if stdout is tty.
|
||||||
func UseColors() *bool {
|
func useColors() *bool {
|
||||||
tr, fa := true, false
|
tr, fa := true, false
|
||||||
if os.Getenv(env.EnvForceColor) != "" {
|
if os.Getenv(env.EnvForceColor) != "" {
|
||||||
return &tr
|
return &tr
|
||||||
@ -18,3 +20,13 @@ func UseColors() *bool {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnableOrDisableColor determines if color should be force-enabled or force-disabled
|
||||||
|
// or left untouched based on environment configuration.
|
||||||
|
func EnableOrDisableColor(c *color.Color) {
|
||||||
|
if v := useColors(); v != nil && *v {
|
||||||
|
c.EnableColor()
|
||||||
|
} else if v != nil && !*v {
|
||||||
|
c.DisableColor()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -16,16 +16,16 @@ func Test_useColors_forceColors(t *testing.T) {
|
|||||||
defer testutil.WithEnvVar("_KUBECTX_FORCE_COLOR", "1")()
|
defer testutil.WithEnvVar("_KUBECTX_FORCE_COLOR", "1")()
|
||||||
defer testutil.WithEnvVar("NO_COLOR", "1")()
|
defer testutil.WithEnvVar("NO_COLOR", "1")()
|
||||||
|
|
||||||
if v := UseColors(); !cmp.Equal(v, &tr) {
|
if v := useColors(); !cmp.Equal(v, &tr) {
|
||||||
t.Fatalf("expected UseColors() = true; got = %v", v)
|
t.Fatalf("expected useColors() = true; got = %v", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_useColors_disableColors(t *testing.T) {
|
func Test_useColors_disableColors(t *testing.T) {
|
||||||
defer testutil.WithEnvVar("NO_COLOR", "1")()
|
defer testutil.WithEnvVar("NO_COLOR", "1")()
|
||||||
|
|
||||||
if v := UseColors(); !cmp.Equal(v, &fa) {
|
if v := useColors(); !cmp.Equal(v, &fa) {
|
||||||
t.Fatalf("expected UseColors() = false; got = %v", v)
|
t.Fatalf("expected useColors() = false; got = %v", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ func Test_useColors_default(t *testing.T) {
|
|||||||
defer testutil.WithEnvVar("NO_COLOR", "")()
|
defer testutil.WithEnvVar("NO_COLOR", "")()
|
||||||
defer testutil.WithEnvVar("_KUBECTX_FORCE_COLOR", "")()
|
defer testutil.WithEnvVar("_KUBECTX_FORCE_COLOR", "")()
|
||||||
|
|
||||||
if v := UseColors(); v != nil {
|
if v := useColors(); v != nil {
|
||||||
t.Fatalf("expected UseColors() = nil; got=%v", *v)
|
t.Fatalf("expected useColors() = nil; got=%v", *v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
colors := UseColors()
|
colors := useColors()
|
||||||
if colors == nil {
|
if colors == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user