diff --git a/cmd/minikube/cmd/config/profile_list.go b/cmd/minikube/cmd/config/profile_list.go index b6aa8b91e74d..854a3ab07fa0 100644 --- a/cmd/minikube/cmd/config/profile_list.go +++ b/cmd/minikube/cmd/config/profile_list.go @@ -17,9 +17,11 @@ limitations under the License. package config import ( + "encoding/json" "fmt" "os" "strconv" + "strings" "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/exit" @@ -29,48 +31,101 @@ import ( "github.com/spf13/cobra" ) +var ( + output string +) + var profileListCmd = &cobra.Command{ Use: "list", Short: "Lists all minikube profiles.", Long: "Lists all valid minikube profiles and detects all possible invalid profiles.", Run: func(cmd *cobra.Command, args []string) { - var validData [][]string + switch strings.ToLower(output) { + case "json": + PrintProfilesJSON() + case "table": + PrintProfilesTable() + default: + exit.WithCodeT(exit.BadUsage, fmt.Sprintf("invalid output format: %s. Valid values: 'table', 'json'", output)) + } + + }, +} + +func PrintProfilesTable() { - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"Profile", "VM Driver", "NodeIP", "Node Port", "Kubernetes Version"}) - table.SetAutoFormatHeaders(false) - table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true}) - table.SetCenterSeparator("|") - validProfiles, invalidProfiles, err := config.ListProfiles() + var validData [][]string - if len(validProfiles) == 0 || err != nil { - exit.UsageT("No minikube profile was found. You can create one using `minikube start`.") - } - for _, p := range validProfiles { - validData = append(validData, []string{p.Name, p.Config.MachineConfig.VMDriver, p.Config.KubernetesConfig.NodeIP, strconv.Itoa(p.Config.KubernetesConfig.NodePort), p.Config.KubernetesConfig.KubernetesVersion}) - } + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"Profile", "VM Driver", "NodeIP", "Node Port", "Kubernetes Version"}) + table.SetAutoFormatHeaders(false) + table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true}) + table.SetCenterSeparator("|") + validProfiles, invalidProfiles, err := config.ListProfiles() - table.AppendBulk(validData) - table.Render() + if len(validProfiles) == 0 || err != nil { + exit.UsageT("No minikube profile was found. You can create one using `minikube start`.") + } + for _, p := range validProfiles { + validData = append(validData, []string{p.Name, p.Config.MachineConfig.VMDriver, p.Config.KubernetesConfig.NodeIP, strconv.Itoa(p.Config.KubernetesConfig.NodePort), p.Config.KubernetesConfig.KubernetesVersion}) + } - if invalidProfiles != nil { - out.T(out.WarningType, "Found {{.number}} invalid profile(s) ! ", out.V{"number": len(invalidProfiles)}) - for _, p := range invalidProfiles { - out.T(out.Empty, "\t "+p.Name) - } - out.T(out.Tip, "You can delete them using the following command(s): ") - for _, p := range invalidProfiles { - out.String(fmt.Sprintf("\t $ minikube delete -p %s \n", p.Name)) - } + table.AppendBulk(validData) + table.Render() + if invalidProfiles != nil { + out.T(out.WarningType, "Found {{.number}} invalid profile(s) ! ", out.V{"number": len(invalidProfiles)}) + for _, p := range invalidProfiles { + out.T(out.Empty, "\t "+p.Name) } - if err != nil { - exit.WithCodeT(exit.Config, fmt.Sprintf("error loading profiles: %v", err)) + out.T(out.Tip, "You can delete them using the following command(s): ") + for _, p := range invalidProfiles { + out.String(fmt.Sprintf("\t $ minikube delete -p %s \n", p.Name)) } - }, + + } + + if err != nil { + exit.WithCodeT(exit.Config, fmt.Sprintf("error loading profiles: %v", err)) + } + +} + +func PrintProfilesJSON() { + validProfiles, invalidProfiles, err := config.ListProfiles() + + var valid []*config.Profile + var invalid []*config.Profile + + if validProfiles != nil { + valid = validProfiles + } else { + valid = []*config.Profile{} + } + + if invalidProfiles != nil { + invalid = invalidProfiles + } else { + invalid = []*config.Profile{} + } + + var body = map[string]interface{}{} + + if err == nil { + body["valid"] = valid + body["invalid"] = invalid + jsonString, _ := json.Marshal(body) + out.String(string(jsonString)) + } else { + body["error"] = err + jsonString, _ := json.Marshal(body) + out.String(string(jsonString)) + os.Exit(exit.Failure) + } } func init() { + profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'") ProfileCmd.AddCommand(profileListCmd) }