diff --git a/cmd/kubectx/list.go b/cmd/kubectx/list.go index 57496cf..a8b949d 100644 --- a/cmd/kubectx/list.go +++ b/cmd/kubectx/list.go @@ -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 } + diff --git a/go.mod b/go.mod index 0d192a4..3e930d8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b365711..28e60a5 100644 --- a/go.sum +++ b/go.sum @@ -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=