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 b1afdbf375
commit 3c6fa48260
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
import (
"fmt"
"io"
"facette.io/natsort"
"github.com/fatih/color"
"github.com/pkg/errors"
)
@ -17,6 +20,8 @@ type kubeconfig struct {
}
func printListContexts(out io.Writer) error {
// TODO extract printing and sorting into a function that's testable
cfgPath, err := kubeconfigPath()
if err != nil {
return errors.Wrap(err, "failed to determine kubeconfig path")
@ -27,10 +32,22 @@ func printListContexts(out io.Writer) error {
return errors.Wrap(err, "failed to read kubeconfig file")
}
_ = cfg
// print each context
// - natural sort
// - highlight current context
ctxs := make([]string, 0, len(cfg.Contexts))
for _, c := range cfg.Contexts {
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
}

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/ahmetb/kubectx
go 1.14
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/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/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=