Skip to content

Commit

Permalink
Add "none" option
Browse files Browse the repository at this point in the history
  • Loading branch information
ebarped committed Jan 5, 2022
1 parent d688524 commit 03df8e3
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ func rootFunc(cmd *cobra.Command, args []string) {
name := args[0]
path := kcRootDir + name
log.Debug().Str("name", name).Str("path", path).Msgf("loading kubeconfig...")

// "deselect" kubeconfig
if name == "none" {
err := deleteDefaultKubeconfig()
if err != nil {
log.Error().Str("name", "none").Str("path", path).Str("error", err.Error()).Msg("error setting kubeconfig to none...")
retcode = 1
}
retcode = 0
return
}

kc, err := kubeconfig.New(name, path)
if err != nil {
log.Error().Str("name", name).Str("path", path).Str("error", err.Error()).Msg("error loading kubeconfig")
Expand Down Expand Up @@ -164,6 +176,21 @@ func rootFunc(cmd *cobra.Command, args []string) {
}
}

func deleteDefaultKubeconfig() error {
path := kcRootDir + "config"

_, err := os.Stat(path)
if err != nil {
// .kube/config does not exist, just exit
return nil
}
err = os.Remove(path)
if err != nil {
return err
}
return nil
}

func useKubeconfig(kc *kubeconfig.Kubeconfig) error {
_, err := copy(kc.Path, kcRootDir+"config")
if err != nil {
Expand Down

0 comments on commit 03df8e3

Please sign in to comment.