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

Method RunListContexts

cmd/list/contexts.go:41–84  ·  view source on GitHub ↗

RunListContexts executes the functionality "devspace list contexts"

(f factory.Factory, cobraCmd *cobra.Command, args []string)

Source from the content-addressed store, hash-verified

39
40// RunListContexts executes the functionality "devspace list contexts"
41func (cmd *contextsCmd) RunListContexts(f factory.Factory, cobraCmd *cobra.Command, args []string) error {
42 logger := f.GetLog()
43 kubeLoader := f.NewKubeConfigLoader()
44 // Load kube-config
45 kubeConfig, err := kubeLoader.LoadRawConfig()
46 if err != nil {
47 return errors.Wrap(err, "load kube config")
48 }
49
50 headerColumnNames := []string{
51 "Name",
52 "Active",
53 }
54
55 contexts := []string{}
56 for ctx := range kubeConfig.Contexts {
57 contexts = append(contexts, ctx)
58 }
59
60 sort.Strings(contexts)
61
62 contextRows := make([][]string, 0, len(contexts))
63 defaultFound := false
64 for _, context := range contexts {
65 contextRows = append(contextRows, []string{
66 context,
67 strconv.FormatBool(context == kubeConfig.CurrentContext),
68 })
69
70 if context == kubeConfig.CurrentContext {
71 defaultFound = true
72 }
73 }
74
75 if !defaultFound {
76 contextRows = append(contextRows, []string{
77 kubeConfig.CurrentContext,
78 "true",
79 })
80 }
81
82 log.PrintTable(logger, headerColumnNames, contextRows)
83 return nil
84}

Callers 1

newContextsCmdFunction · 0.95

Calls 4

PrintTableFunction · 0.92
GetLogMethod · 0.65
NewKubeConfigLoaderMethod · 0.65
LoadRawConfigMethod · 0.65

Tested by

no test coverage detected