make minikube addons list tabular

pull/6274/head
Medya Gh 2020-01-10 15:39:22 -08:00
parent 0a9e8b03b8
commit 6c7cadb371
2 changed files with 34 additions and 27 deletions

View File

@ -22,17 +22,17 @@ import (
"os" "os"
"sort" "sort"
"strings" "strings"
"text/template"
"github.com/golang/glog"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/assets" "k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/exit" "k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/minikube/out"
) )
const defaultAddonListFormat = "- {{.AddonName}}: {{.AddonStatus}}\n"
var addonListFormat string
var addonListOutput string var addonListOutput string
// AddonListTemplate represents the addon list template // AddonListTemplate represents the addon list template
@ -50,10 +50,6 @@ var addonsListCmd = &cobra.Command{
exit.UsageT("usage: minikube addons list") exit.UsageT("usage: minikube addons list")
} }
if addonListOutput != "list" && addonListFormat != defaultAddonListFormat {
exit.UsageT("Cannot use both --output and --format options")
}
switch strings.ToLower(addonListOutput) { switch strings.ToLower(addonListOutput) {
case "list": case "list":
printAddonsList() printAddonsList()
@ -66,14 +62,6 @@ var addonsListCmd = &cobra.Command{
} }
func init() { func init() {
addonsListCmd.Flags().StringVarP(
&addonListFormat,
"format",
"f",
defaultAddonListFormat,
`Go template format string for the addon list output. The format for Go templates can be found here: https://golang.org/pkg/text/template/
For the list of accessible variables for the template, see the struct values here: https://godoc.org/k8s.io/minikube/cmd/minikube/cmd/config#AddonListTemplate`)
addonsListCmd.Flags().StringVarP( addonsListCmd.Flags().StringVarP(
&addonListOutput, &addonListOutput,
"output", "output",
@ -84,6 +72,13 @@ For the list of accessible variables for the template, see the struct values her
AddonsCmd.AddCommand(addonsListCmd) AddonsCmd.AddCommand(addonsListCmd)
} }
var iconFromStatus = func(addonStatus bool) string {
if addonStatus {
return "✅"
}
return " " // because emoji indentation is different
}
var stringFromStatus = func(addonStatus bool) string { var stringFromStatus = func(addonStatus bool) string {
if addonStatus { if addonStatus {
return "enabled" return "enabled"
@ -97,6 +92,13 @@ var printAddonsList = func() {
addonNames = append(addonNames, addonName) addonNames = append(addonNames, addonName)
} }
sort.Strings(addonNames) sort.Strings(addonNames)
var tData [][]string
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Addon Name", "Profile", "Status"})
table.SetAutoFormatHeaders(true)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")
pName := viper.GetString(config.MachineProfile)
for _, addonName := range addonNames { for _, addonName := range addonNames {
addonBundle := assets.Addons[addonName] addonBundle := assets.Addons[addonName]
@ -104,20 +106,25 @@ var printAddonsList = func() {
if err != nil { if err != nil {
exit.WithError("Error getting addons status", err) exit.WithError("Error getting addons status", err)
} }
tmpl, err := template.New("list").Parse(addonListFormat) tData = append(tData, []string{addonName, pName, fmt.Sprintf("%s %s", stringFromStatus(addonStatus), iconFromStatus(addonStatus))})
if err != nil {
exit.WithError("Error creating list template", err)
}
listTmplt := AddonListTemplate{addonName, stringFromStatus(addonStatus)}
err = tmpl.Execute(os.Stdout, listTmplt)
if err != nil {
exit.WithError("Error executing list template", err)
}
} }
table.AppendBulk(tData)
table.Render()
v, _, err := config.ListProfiles()
if err != nil {
glog.Infof("error getting list of porfiles: %v", err)
}
if len(v) > 1 {
out.T(out.Tip, "To see addons list for other profiles use: `minikube addons -p name list`")
}
} }
var printAddonsJSON = func() { var printAddonsJSON = func() {
addonNames := make([]string, 0, len(assets.Addons)) addonNames := make([]string, 0, len(assets.Addons))
pName := viper.GetString(config.MachineProfile)
for addonName := range assets.Addons { for addonName := range assets.Addons {
addonNames = append(addonNames, addonName) addonNames = append(addonNames, addonName)
} }
@ -134,7 +141,8 @@ var printAddonsJSON = func() {
} }
addonsMap[addonName] = map[string]interface{}{ addonsMap[addonName] = map[string]interface{}{
"Status": stringFromStatus(addonStatus), "Status": stringFromStatus(addonStatus),
"Profile": pName,
} }
} }
jsonString, _ := json.Marshal(addonsMap) jsonString, _ := json.Marshal(addonsMap)

View File

@ -59,7 +59,6 @@ var profileListCmd = &cobra.Command{
var printProfilesTable = func() { var printProfilesTable = func() {
var validData [][]string var validData [][]string
table := tablewriter.NewWriter(os.Stdout) table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Profile", "VM Driver", "NodeIP", "Node Port", "Kubernetes Version", "Status"}) table.SetHeader([]string{"Profile", "VM Driver", "NodeIP", "Node Port", "Kubernetes Version", "Status"})
table.SetAutoFormatHeaders(false) table.SetAutoFormatHeaders(false)