Implement context listing

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2020-04-10 13:21:52 -07:00
parent da08491f0b
commit 04e963c02c
No known key found for this signature in database
GPG Key ID: 441833503E604E2C
3 changed files with 24 additions and 4 deletions

View File

@ -1,8 +1,11 @@
package main package main
import ( import (
"fmt"
"io" "io"
"facette.io/natsort"
"github.com/fatih/color"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -17,6 +20,8 @@ type kubeconfig struct {
} }
func printListContexts(out io.Writer) error { func printListContexts(out io.Writer) error {
// TODO extract printing and sorting into a function that's testable
cfgPath, err := kubeconfigPath() cfgPath, err := kubeconfigPath()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to determine kubeconfig path") return errors.Wrap(err, "failed to determine kubeconfig path")
@ -28,9 +33,21 @@ func printListContexts(out io.Writer) error {
} }
_ = cfg _ = cfg
// print each context ctxs := make([]string, 0, len(cfg.Contexts))
// - natural sort for _, c := range cfg.Contexts {
// - highlight current context ctxs = append(ctxs, c.Name)
}
natsort.Sort(ctxs)
// TODO support KUBECTX_CURRENT_FGCOLOR
// TODO support KUBECTX_CURRENT_BGCOLOR
for _, c := range ctxs {
out := c
if c == cfg.CurrentContext {
out = color.New(color.FgYellow, color.Bold).Sprint(c)
}
fmt.Println(out)
}
return nil return nil
} }

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/ahmetb/kubectx
go 1.14 go 1.14
require ( require (
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/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1

2
go.sum
View File

@ -1,3 +1,5 @@
facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb h1:1pSweJFeR3Pqx7uoelppkzeegfUBXL6I2FFAbfXw570=
facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb/go.mod h1:npRYmtaITVom7rcSo+pRURltHSG2r4TQM1cdqJ2dUB0=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=