From 91e00f9867725e497a2a3c892f1e5ab8564585b1 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Sun, 12 Apr 2020 15:59:48 -0700 Subject: [PATCH] Support for fzf, color ignore/force knobs Signed-off-by: Ahmet Alp Balkan --- cmd/kubectx/flags.go | 4 +++ cmd/kubectx/fzf.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ cmd/kubectx/list.go | 35 +++++++++++++++------- go.mod | 2 ++ go.sum | 12 ++++++++ 5 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 cmd/kubectx/fzf.go diff --git a/cmd/kubectx/flags.go b/cmd/kubectx/flags.go index 5056a89..bc5a2aa 100644 --- a/cmd/kubectx/flags.go +++ b/cmd/kubectx/flags.go @@ -2,6 +2,7 @@ package main import ( "io" + "os" "strings" "github.com/pkg/errors" @@ -22,6 +23,9 @@ func (op UnsupportedOp) Run(_, _ io.Writer) error { // and decides which operation should be taken. func parseArgs(argv []string) Op { if len(argv) == 0 { + if isInteractiveMode(os.Stdout){ + return InteractiveSwitchOp{SelfCmd: os.Args[0]} + } return ListOp{} } diff --git a/cmd/kubectx/fzf.go b/cmd/kubectx/fzf.go new file mode 100644 index 0000000..c02f241 --- /dev/null +++ b/cmd/kubectx/fzf.go @@ -0,0 +1,69 @@ +package main + +import ( + "bytes" + "fmt" + "io" + "os" + "os/exec" + "strings" + + "github.com/pkg/errors" + + "github.com/mattn/go-isatty" +) + +const ( + envFZFIgnore = "KUBECTX_IGNORE_FZF" +) + +type InteractiveSwitchOp struct { + SelfCmd string +} + +func (op InteractiveSwitchOp) Run(_, stderr io.Writer) error { + cmd := exec.Command("fzf", "--ansi", "--no-preview") + var out bytes.Buffer + cmd.Stdin = os.Stdin + cmd.Stderr = stderr + cmd.Stdout = &out + + cmd.Env = append(os.Environ(), + fmt.Sprintf("FZF_DEFAULT_COMMAND=%s", op.SelfCmd), + fmt.Sprintf("%s=1", envForceColor)) + if err := cmd.Run(); err != nil { + if _, ok := err.(*exec.ExitError); !ok { + return err + } + } + choice := strings.TrimSpace(out.String()) + if choice == "" { + return errors.New("you did not choose any of the options") + } + name, err := switchContext(choice) + if err != nil { + return errors.Wrap(err, "failed to switch context") + } + printSuccess(stderr, "Switched to context %q.", name) + return nil +} + +// isTerminal determines if given fd is a TTY. +func isTerminal(fd *os.File) bool { + return isatty.IsTerminal(fd.Fd()) +} + +// fzfInstalled determines if fzf(1) is in PATH. +func fzfInstalled() bool { + v, _ := exec.LookPath("fzf") + if v != "" { + return true + } + return false +} + +// isInteractiveMode determines if we can do choosing with fzf. +func isInteractiveMode(stdout *os.File) bool { + v := os.Getenv(envFZFIgnore) + return v == "" && isTerminal(stdout) && fzfInstalled() +} diff --git a/cmd/kubectx/list.go b/cmd/kubectx/list.go index a06c341..1ed785b 100644 --- a/cmd/kubectx/list.go +++ b/cmd/kubectx/list.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io" + "os" "facette.io/natsort" "github.com/fatih/color" @@ -11,16 +12,6 @@ import ( "github.com/ahmetb/kubectx/cmd/kubectx/kubeconfig" ) -type context struct { - Name string `yaml:"name"` -} - -type kubeconfigContents struct { - APIVersion string `yaml:"apiVersion"` - CurrentContext string `yaml:"current-context"` - Contexts []context `yaml:"contexts"` -} - // ListOp describes listing contexts. type ListOp struct{} @@ -36,13 +27,35 @@ func (_ ListOp) Run(stdout, _ io.Writer) error { // TODO support KUBECTX_CURRENT_FGCOLOR // TODO support KUBECTX_CURRENT_BGCOLOR + + currentColor := color.New(color.FgGreen, color.Bold) + if useColors(){ + currentColor.EnableColor() + } else { + currentColor.DisableColor() + } + cur := kc.GetCurrentContext() for _, c := range ctxs { s := c if c == cur { - s = color.New(color.FgGreen, color.Bold).Sprint(c) + s = currentColor.Sprint(c) } fmt.Fprintf(stdout, "%s\n", s) } 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 +} diff --git a/go.mod b/go.mod index 3e930d8..808bf9d 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,8 @@ require ( facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb github.com/fatih/color v1.9.0 github.com/google/go-cmp v0.4.0 + github.com/mattn/go-isatty v0.0.12 + github.com/mattn/go-tty v0.0.3 // indirect github.com/pkg/errors v0.9.1 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c ) diff --git a/go.sum b/go.sum index 28e60a5..e4a1eef 100644 --- a/go.sum +++ b/go.sum @@ -7,13 +7,25 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-tty v0.0.3 h1:5OfyWorkyO7xP52Mq7tB36ajHDG5OHrmBGIS/DtakQI= +github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=