Unverified Commit 46c3a11f authored by Elf's avatar Elf Committed by GitHub
Browse files

feat: change context (#47)

parent a1958f93
Showing with 91 additions and 13 deletions
+91 -13
......@@ -178,6 +178,13 @@ var (
Mod: gocui.ModNone,
}
changeContext = &guilib.Action{
Keys: keyMap[changeContextActionName],
Name: changeContextActionName,
Handler: changeContextHandler,
Mod: gocui.ModNone,
}
addCustomResourcePanelMoreAction = &moreAction{
NeedSelectResource: false,
Action: *addCustomResourcePanelAction,
......@@ -198,6 +205,12 @@ var (
Action: *runPodAction,
}
changeContextMoreAction = &moreAction{
NeedSelectResource: false,
ShowAction: nil,
Action: *changeContext,
}
commonResourceMoreActions = []*moreAction{
addCustomResourcePanelMoreAction,
editResourceMoreAction,
......@@ -206,6 +219,7 @@ var (
moreActionsMap = map[string][]*moreAction{
clusterInfoViewName: {
addCustomResourcePanelMoreAction,
changeContextMoreAction,
},
namespaceViewName: append(
commonResourceMoreActions,
......
......@@ -651,3 +651,27 @@ func runPodCommandInput(gui *guilib.Gui, namespace, podName, image string) error
}
return nil
}
func changeContextHandler(gui *guilib.Gui, view *guilib.View) error {
if err := showFilterDialog(
gui,
"Selected a context to swicth.",
func(confirmed string) error {
kubecli.Cli.SetCurrentContext(confirmed)
gui.ReRenderAll()
if err := gui.FocusView(clusterInfoViewName, false); err != nil {
return err
}
return nil
},
func(inputted string) ([]string, error) {
return kubecli.Cli.ListContexts(), nil
},
"No contexts.",
false,
); err != nil {
return err
}
return nil
}
......@@ -48,6 +48,7 @@ const (
containerExecCommandActionName = "Execute the command"
changePodLogsContainerActionName = "Change pod logs container"
runPodActionName = "Run a pod with an image"
changeContextActionName = "Change context"
)
var (
......@@ -77,7 +78,8 @@ var (
moreActionsName: {gocui.KeyF3, 'm'},
toFilteredViewAction: {gocui.KeyTab, gocui.KeyArrowDown},
toFilterInputAction: {gocui.KeyTab},
filteredNextLineAction: {gocui.KeyArrowDown},
filteredNextLineAction: {gocui.KeyArrowDown, 'j'},
filteredPreviousLineAction: {gocui.KeyArrowUp, 'h'},
confirmFilterInputAction: {gocui.KeyEnter},
switchConfirmDialogOpt: {gocui.KeyTab, gocui.KeyArrowRight, gocui.KeyArrowLeft, 'k', 'l'},
confirmDialogEnter: {gocui.KeyEnter},
......@@ -88,6 +90,7 @@ var (
inputDialogEnter: {gocui.KeyEnter},
changePodLogsContainerActionName: {'c'},
runPodActionName: {'r'},
changeContextActionName: {'~'},
}
)
......
......@@ -34,6 +34,7 @@ var (
Actions: guilib.ToActionInterfaceArr([]*guilib.Action{
toNavigation,
nextCyclicView,
changeContext,
newMoreActions(moreActionsMap[clusterInfoViewName]),
}),
OnFocus: func(gui *guilib.Gui, view *guilib.View) error {
......
......@@ -258,10 +258,7 @@ func navigationOnClick(gui *guilib.Gui, view *guilib.View) error {
func renderClusterInfo(_ *guilib.Gui, view *guilib.View) error {
view.Clear()
currentContext, err := kubecli.Cli.CurrentContext()
if err != nil {
return nil
}
currentContext := kubecli.Cli.CurrentContext()
if _, err := fmt.Fprintf(view, "Current Context: %s", color.Green.Sprint(currentContext)); err != nil {
return err
......
package config
import "k8s.io/client-go/tools/clientcmd"
import (
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
func CurrentContext() (string, error) {
var (
config *clientcmdapi.Config
)
func init() {
var err error
pathOptions := clientcmd.NewDefaultPathOptions()
config, err := pathOptions.GetStartingConfig()
config, err = pathOptions.GetStartingConfig()
if err != nil {
return "", err
panic(err)
}
}
func CurrentContext() string {
return config.CurrentContext
}
func SetCurrentContext(context string) {
config.CurrentContext = context
}
if config.CurrentContext == "" {
return "current-context is not set", nil
func ListContexts() []string {
contexts := make([]string, 0)
for name, _ := range config.Contexts {
contexts = append(contexts, name)
}
return config.CurrentContext, nil
return contexts
}
......@@ -31,6 +31,7 @@ func init() {
type KubeCLI struct {
factory util.Factory
namespace *string
context *string
}
type Cmd struct {
......@@ -63,14 +64,17 @@ func (c *Cmd) SetFlag(name, value string) *Cmd {
func NewKubeCLI() *KubeCLI {
namespace := ""
context := config.CurrentContext()
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.Namespace = &namespace
kubeConfigFlags.Context = &context
matchVersionKubeConfigFlags := util.NewMatchVersionFlags(kubeConfigFlags)
k := &KubeCLI{
factory: util.NewFactory(matchVersionKubeConfigFlags),
namespace: &namespace,
context: &context,
}
return k
}
......@@ -78,12 +82,24 @@ func NewKubeCLI() *KubeCLI {
func (cli *KubeCLI) SetNamespace(namespace string) {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.Namespace = &namespace
kubeConfigFlags.Context = cli.context
matchVersionKubeConfigFlags := util.NewMatchVersionFlags(kubeConfigFlags)
cli.factory = util.NewFactory(matchVersionKubeConfigFlags)
cli.namespace = &namespace
}
func (cli *KubeCLI) SetCurrentContext(context string) {
config.SetCurrentContext(context)
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.Namespace = cli.namespace
kubeConfigFlags.Context = &context
matchVersionKubeConfigFlags := util.NewMatchVersionFlags(kubeConfigFlags)
cli.factory = util.NewFactory(matchVersionKubeConfigFlags)
cli.context = &context
}
func (cli *KubeCLI) WithNamespace(namespace string) *KubeCLI {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
kubeConfigFlags.Namespace = &namespace
......@@ -101,10 +117,14 @@ func (cli *KubeCLI) Namespace() string {
return *cli.namespace
}
func (cli *KubeCLI) CurrentContext() (string, error) {
func (cli *KubeCLI) CurrentContext() string {
return config.CurrentContext()
}
func (cli *KubeCLI) ListContexts() []string {
return config.ListContexts()
}
func (cli *KubeCLI) ClusterInfo() (string, error) {
return clusterinfo.ClusterInfo(cli.factory)
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment