Add json output for profile list

pull/5554/head
Josh Woodcock 2019-10-07 08:20:08 -05:00
parent f52daf9834
commit 4b2f962f0c
1 changed files with 86 additions and 31 deletions

View File

@ -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,12 +31,30 @@ 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) {
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() {
var validData [][]string
table := tablewriter.NewWriter(os.Stdout)
@ -65,12 +85,47 @@ var profileListCmd = &cobra.Command{
}
}
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)
}