mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-06-24 14:32:24 +00:00
Implement context listing
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
b1afdbf375
commit
3c6fa48260
@ -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
1
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
|
||||
|
2
go.sum
2
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=
|
||||
|
Loading…
Reference in New Issue
Block a user