RunListContexts executes the functionality "devspace list contexts"
(f factory.Factory, cobraCmd *cobra.Command, args []string)
| 39 | |
| 40 | // RunListContexts executes the functionality "devspace list contexts" |
| 41 | func (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 | } |
no test coverage detected