Skip to content

Commit

Permalink
Added current context view (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyal-solomon1 authored Aug 13, 2023
1 parent dfe4b8c commit 808b008
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
31 changes: 26 additions & 5 deletions cmd/cli/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/homedir"
)

Expand All @@ -39,11 +40,7 @@ func applyManifest(completion string) error {

var namespace string
if *kubernetesConfigFlags.Namespace == "" {
clientConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig},
&clientcmd.ConfigOverrides{
CurrentContext: "",
}).RawConfig()
clientConfig, err := getConfig(kubeConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,3 +109,27 @@ func getKubeConfig() string {
}
return kubeConfig
}

func getConfig(kubeConfig string) (api.Config, error) {
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig},
&clientcmd.ConfigOverrides{
CurrentContext: "",
}).RawConfig()
if err != nil {
return api.Config{}, err
}

return config, nil
}

func getCurrentContextName() (string, error) {
kubeConfig := getKubeConfig()
config, err := getConfig(kubeConfig)
if err != nil {
return "", err
}
currentContext := config.CurrentContext

return currentContext, nil
}
6 changes: 5 additions & 1 deletion cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ func userActionPrompt() (string, error) {
var result string
var err error
items := []string{apply, dontApply}
label := fmt.Sprintf("Would you like to apply this? [%s/%s/%s]", reprompt, apply, dontApply)
currentContext, err := getCurrentContextName()
label := fmt.Sprintf("Would you like to apply this? [%[1]s/%[2]s/%[3]s]", reprompt, apply, dontApply)
if err == nil {
label = fmt.Sprintf("(context: %[1]s) %[2]s", currentContext, label)
}

prompt := promptui.SelectWithAdd{
Label: label,
Expand Down

0 comments on commit 808b008

Please sign in to comment.