mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-06-29 16:56:48 +00:00
Support for fzf, color ignore/force knobs
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
17f6ffe73b
commit
91e00f9867
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -22,6 +23,9 @@ func (op UnsupportedOp) Run(_, _ io.Writer) error {
|
|||||||
// and decides which operation should be taken.
|
// and decides which operation should be taken.
|
||||||
func parseArgs(argv []string) Op {
|
func parseArgs(argv []string) Op {
|
||||||
if len(argv) == 0 {
|
if len(argv) == 0 {
|
||||||
|
if isInteractiveMode(os.Stdout){
|
||||||
|
return InteractiveSwitchOp{SelfCmd: os.Args[0]}
|
||||||
|
}
|
||||||
return ListOp{}
|
return ListOp{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
69
cmd/kubectx/fzf.go
Normal file
69
cmd/kubectx/fzf.go
Normal file
@ -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()
|
||||||
|
}
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
"facette.io/natsort"
|
"facette.io/natsort"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
@ -11,16 +12,6 @@ import (
|
|||||||
"github.com/ahmetb/kubectx/cmd/kubectx/kubeconfig"
|
"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.
|
// ListOp describes listing contexts.
|
||||||
type ListOp struct{}
|
type ListOp struct{}
|
||||||
|
|
||||||
@ -36,13 +27,35 @@ func (_ ListOp) Run(stdout, _ io.Writer) error {
|
|||||||
|
|
||||||
// TODO support KUBECTX_CURRENT_FGCOLOR
|
// TODO support KUBECTX_CURRENT_FGCOLOR
|
||||||
// TODO support KUBECTX_CURRENT_BGCOLOR
|
// TODO support KUBECTX_CURRENT_BGCOLOR
|
||||||
|
|
||||||
|
currentColor := color.New(color.FgGreen, color.Bold)
|
||||||
|
if useColors(){
|
||||||
|
currentColor.EnableColor()
|
||||||
|
} else {
|
||||||
|
currentColor.DisableColor()
|
||||||
|
}
|
||||||
|
|
||||||
cur := kc.GetCurrentContext()
|
cur := kc.GetCurrentContext()
|
||||||
for _, c := range ctxs {
|
for _, c := range ctxs {
|
||||||
s := c
|
s := c
|
||||||
if c == cur {
|
if c == cur {
|
||||||
s = color.New(color.FgGreen, color.Bold).Sprint(c)
|
s = currentColor.Sprint(c)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(stdout, "%s\n", s)
|
fmt.Fprintf(stdout, "%s\n", s)
|
||||||
}
|
}
|
||||||
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
|
||||||
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -6,6 +6,8 @@ require (
|
|||||||
facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb
|
facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb
|
||||||
github.com/fatih/color v1.9.0
|
github.com/fatih/color v1.9.0
|
||||||
github.com/google/go-cmp v0.4.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
|
github.com/pkg/errors v0.9.1
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
|
||||||
)
|
)
|
||||||
|
12
go.sum
12
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 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
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.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 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
|
||||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
|
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 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
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-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 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
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=
|
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/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
Loading…
Reference in New Issue
Block a user