RunRemoveContext executes the devspace remove context functionality
(f factory.Factory, cobraCmd *cobra.Command, args []string)
| 40 | |
| 41 | // RunRemoveContext executes the devspace remove context functionality |
| 42 | func (cmd *contextCmd) RunRemoveContext(f factory.Factory, cobraCmd *cobra.Command, args []string) error { |
| 43 | log := f.GetLog() |
| 44 | kubeLoader := f.NewKubeConfigLoader() |
| 45 | |
| 46 | // Load kube-config |
| 47 | kubeConfig, err := kubeLoader.LoadRawConfig() |
| 48 | if err != nil { |
| 49 | return errors.Wrap(err, "load kube config") |
| 50 | } |
| 51 | |
| 52 | var contextName string |
| 53 | if len(args) > 0 { |
| 54 | // First arg is context name |
| 55 | contextName = args[0] |
| 56 | |
| 57 | if _, contextExists := kubeConfig.Contexts[contextName]; !contextExists { |
| 58 | return errors.Errorf("Kube-context '%s' does not exist", contextName) |
| 59 | } |
| 60 | } else { |
| 61 | contexts := []string{} |
| 62 | for ctx := range kubeConfig.Contexts { |
| 63 | contexts = append(contexts, ctx) |
| 64 | } |
| 65 | |
| 66 | sort.Strings(contexts) |
| 67 | |
| 68 | contextName, err = log.Question(&survey.QuestionOptions{ |
| 69 | Question: "Which context do you want to remove?", |
| 70 | Options: contexts, |
| 71 | }) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | oldCurrentContext := kubeConfig.CurrentContext |
| 78 | |
| 79 | // Remove the context |
| 80 | err = kubeLoader.DeleteKubeContext(kubeConfig, contextName) |
| 81 | if err != nil { |
| 82 | return errors.Wrap(err, "delete kube context") |
| 83 | } |
| 84 | |
| 85 | // Save updated kube-config |
| 86 | err = kubeLoader.SaveConfig(kubeConfig) |
| 87 | if err != nil { |
| 88 | return errors.Wrap(err, "save kube config") |
| 89 | } |
| 90 | |
| 91 | if oldCurrentContext != kubeConfig.CurrentContext { |
| 92 | log.Infof("Your kube-context has been updated to '%s'", ansi.Color(kubeConfig.CurrentContext, "white+b")) |
| 93 | } |
| 94 | |
| 95 | log.Donef("Kube-context '%s' has been successfully removed", contextName) |
| 96 | return nil |
| 97 | } |
no test coverage detected