Save last context name in state file

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2020-04-10 14:27:52 -07:00
parent 7a40a5ed07
commit 74a30a60e0
No known key found for this signature in database
GPG Key ID: 441833503E604E2C
3 changed files with 27 additions and 10 deletions

View File

@ -19,15 +19,22 @@ func kubeconfigPath() (string, error) {
return v, nil return v, nil
} }
home := homeDir()
if home == "" {
return "", errors.New("HOME or USERPROFILE environment variable not set")
}
// return default path // return default path
return filepath.Join(home, ".kube", "config"), nil
}
func homeDir() string {
// TODO move tests out of kubeconfigPath to TestHomeDir()
home := os.Getenv("HOME") home := os.Getenv("HOME")
if home == "" { if home == "" {
home = os.Getenv("USERPROFILE") // windows home = os.Getenv("USERPROFILE") // windows
} }
if home == "" { return home
return "", errors.New("HOME or USERPROFILE environment variable not set")
}
return filepath.Join(home, ".kube", "config"), nil
} }
func parseKubeconfig(path string) (kubeconfig, error) { func parseKubeconfig(path string) (kubeconfig, error) {

View File

@ -21,9 +21,9 @@ func main() {
printListContexts(os.Stdout) printListContexts(os.Stdout)
case SwitchOp: case SwitchOp:
if v.Target == "-" { if v.Target == "-" {
// TODO implement swap // TODO implement swap
panic("not implemented") panic("not implemented")
} }
newCtx, err := switchContext(v.Target) newCtx, err := switchContext(v.Target)
if err != nil { if err != nil {
printError("faield to switch context: %v", err) printError("faield to switch context: %v", err)

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"io" "io"
"os" "os"
@ -11,6 +10,11 @@ import (
// switchContext switches to specified context name. // switchContext switches to specified context name.
func switchContext(name string) (string, error) { func switchContext(name string) (string, error) {
stateFile, err := kubectxFilePath()
if err != nil {
return "", errors.Wrap(err, "failed to determine state file")
}
cfgPath, err := kubeconfigPath() cfgPath, err := kubeconfigPath()
if err != nil { if err != nil {
return "", errors.Wrap(err, "cannot determine kubeconfig path") return "", errors.Wrap(err, "cannot determine kubeconfig path")
@ -26,8 +30,7 @@ func switchContext(name string) (string, error) {
return "", errors.Wrap(err, "yaml parse error") return "", errors.Wrap(err, "yaml parse error")
} }
cur := getCurrentContext(kc) prev := getCurrentContext(kc)
fmt.Printf("current-context=%s\n", cur)
if err := modifyCurrentContext(kc, name); err != nil { if err := modifyCurrentContext(kc, name); err != nil {
return "", err return "", err
@ -44,6 +47,13 @@ func switchContext(name string) (string, error) {
if err := saveKubeconfigRaw(f, kc); err != nil { if err := saveKubeconfigRaw(f, kc); err != nil {
return "", errors.Wrap(err, "failed to save kubeconfig") return "", errors.Wrap(err, "failed to save kubeconfig")
} }
if prev != name {
if err := writeLastContext(stateFile, prev); err != nil {
return "", errors.Wrap(err, "failed to save previous context name")
}
}
return name, nil return name, nil
} }