Skip to content
This repository has been archived by the owner on Sep 8, 2022. It is now read-only.

Commit

Permalink
print contexts not clusters and sort output
Browse files Browse the repository at this point in the history
  • Loading branch information
ghouscht committed Nov 16, 2018
1 parent 92ee8d9 commit 8e8539f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cmd/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"sort"

"github.com/fatih/color"
"github.com/spf13/cobra"
Expand All @@ -28,6 +29,7 @@ type CtxOptions struct {
args []string

userSpecifiedContext string
availableContexts []string // contains a sorted list of all contexts

genericclioptions.IOStreams
}
Expand Down Expand Up @@ -78,6 +80,11 @@ func (o *CtxOptions) Complete(cmd *cobra.Command, args []string) error {
return err
}

for ctx := range o.rawConfig.Contexts {
o.availableContexts = append(o.availableContexts, ctx)
}
sort.Strings(o.availableContexts)

return nil
}

Expand Down Expand Up @@ -130,11 +137,11 @@ func (o *CtxOptions) changeCurrentCtx() error {
// in red
func (o *CtxOptions) printContexts() {
red := color.New(color.FgRed)
for cluster := range o.rawConfig.Clusters {
if cluster == o.rawConfig.CurrentContext {
red.Fprintf(o.Out, "%s\n", cluster)
for _, ctx := range o.availableContexts {
if ctx == o.rawConfig.CurrentContext {
red.Fprintf(o.Out, "%s\n", ctx)
} else {
fmt.Fprintf(o.Out, "%s\n", cluster)
fmt.Fprintf(o.Out, "%s\n", ctx)
}
}
}

0 comments on commit 8e8539f

Please sign in to comment.