MCPcopy Index your code
hub / github.com/devspace-sh/devspace / RunUseContext

Method RunUseContext

cmd/use/context.go:46–108  ·  view source on GitHub ↗

RunUseContext executes the functionality "devspace use namespace"

(f factory.Factory, args []string)

Source from the content-addressed store, hash-verified

44
45// RunUseContext executes the functionality "devspace use namespace"
46func (cmd *ContextCmd) RunUseContext(f factory.Factory, args []string) error {
47 // Load kube-config
48 log := f.GetLog()
49 kubeLoader := f.NewKubeConfigLoader()
50 kubeConfig, err := kubeLoader.LoadRawConfig()
51 if err != nil {
52 return errors.Wrap(err, "load kube config")
53 }
54
55 var context string
56 if len(args) > 0 {
57 // First arg is context name
58 context = args[0]
59 } else {
60 contexts := []string{}
61 for ctx := range kubeConfig.Contexts {
62 contexts = append(contexts, ctx)
63 }
64
65 context, err = log.Question(&survey.QuestionOptions{
66 Question: "Which context do you want to use?",
67 DefaultValue: kubeConfig.CurrentContext,
68 Options: contexts,
69 Sort: true,
70 })
71 if err != nil {
72 return err
73 }
74 }
75
76 // check if context exists
77 _, found := kubeConfig.Contexts[context]
78 if !found {
79 return fmt.Errorf("couldn't find context %s in kube config", context)
80 }
81
82 // Save old context
83 oldContext := kubeConfig.CurrentContext
84
85 // Set current kube-context
86 kubeConfig.CurrentContext = context
87
88 if oldContext != context {
89 // Save updated kube-config
90 _ = kubeLoader.SaveConfig(kubeConfig)
91
92 log.Infof("Your kube-context has been updated to '%s'", ansi.Color(kubeConfig.CurrentContext, "white+b"))
93 log.Infof("\r To revert this operation, run: %s\n", ansi.Color("devspace use context "+oldContext, "white+b"))
94 }
95
96 // clear project kube context
97 configLoader, err := f.NewConfigLoader(cmd.ConfigPath)
98 if err != nil {
99 return err
100 }
101 err = ClearProjectKubeContext(configLoader, log)
102 if err != nil {
103 return errors.Wrap(err, "clear generated kube context")

Callers 1

newContextCmdFunction · 0.95

Calls 10

ClearProjectKubeContextFunction · 0.85
GetLogMethod · 0.65
NewKubeConfigLoaderMethod · 0.65
LoadRawConfigMethod · 0.65
QuestionMethod · 0.65
SaveConfigMethod · 0.65
NewConfigLoaderMethod · 0.65
ErrorfMethod · 0.45
InfofMethod · 0.45
DonefMethod · 0.45

Tested by

no test coverage detected