Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 59 additions & 26 deletions cmd/minikube/cmd/config/addons_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/tw"
"github.com/spf13/cobra"
"github.com/fatih/color"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/config"
Expand Down Expand Up @@ -93,62 +94,94 @@ var stringFromStatus = func(addonStatus bool) string {
var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
addonNames := slices.Sorted(maps.Keys(assets.Addons))
table := tablewriter.NewWriter(os.Stdout)
table.Options(tablewriter.WithHeaderAutoFormat(tw.On))

table.Options(
tablewriter.WithHeaderAutoFormat(tw.On),
)

// Create table header
var tHeader []string
if cc == nil {
tHeader = []string{"Addon Name", "Maintainer"}
} else {
tHeader = []string{"Addon Name", "Profile", "Status", "Maintainer"}
// Table header
header := []string{"Addon Name", "Maintainer"}
if cc != nil {
header = []string{"Addon Name", "Enabled", "Maintainer"}
}
if printDocs {
tHeader = append(tHeader, "Docs")
header = append(header, "Docs")
}
table.Header(tHeader)
table.Header(header)

var rows [][]string

// Create table data
var tData [][]string
var temp []string
for _, addonName := range addonNames {
addonBundle := assets.Addons[addonName]

maintainer := addonBundle.Maintainer
if maintainer == "" {
maintainer = "3rd party (unknown)"
}

docs := addonBundle.Docs
if docs == "" {
docs = "n/a"
}

// Step 1: build row
var row []string
if cc == nil {
temp = []string{addonName, maintainer}
row = []string{addonName, maintainer}
} else {
enabled := addonBundle.IsEnabled(cc)
temp = []string{addonName, cc.Name, fmt.Sprintf("%s %s", stringFromStatus(enabled), iconFromStatus(enabled)), maintainer}
status := iconFromStatus(enabled)
row = []string{addonName, status, maintainer}
if printDocs {
row = append(row, docs)
}

// Step 2: apply coloring
switch {
case enabled:
ColorRow(row, color.GreenString)
default:
ColorRow(row, color.WhiteString)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too complicated.

We need 2 steps:

  • Create list of strings for the table writer
  • Color the strings

For creating list of strings we can modify the previous code a little bit:

enabled := addonBundle.IsEnabled(cc)
row = []string{addonName, cc.Name, iconFromStatus(enabled), maintainer}

iconForStatus() should return empty string if enabled is false. If it does not do this create a function that does this.

Since the list is so simple, we don't really need a loop to color it:

if enabled {
    row[0] = color.Enabled(row[0])
    row[2] = color.Enabled(row[2])
}

But since wan to color longer lists (like profile list), we can add a function to color rows:

type colorFunc func(string) string

func ColorRow(row []string, colored colorFunc) {
    for i := range row {
        text := row[i]
        if text == "" || isEmoji(text) {
            continue
        }
        row[i] = colored(text)
    }
}

With this we can do:

switch status {
case Broken:
    color.ColorRow(row, color.Broken)
case Error:
    color.ColorRow(row, color.Error)
case Enabled:
    color.ColorRow(row, color.Enabled)
}

}
if printDocs {
temp = append(temp, docs)

if cc == nil && printDocs {
row = append(row, docs)
}
tData = append(tData, temp)

rows = append(rows, row)
}
if err := table.Bulk(tData); err != nil {

if err := table.Bulk(rows); err != nil {
klog.Error("Error rendering table (bulk)", err)
}
if err := table.Render(); err != nil {
klog.Error("Error rendering table", err)
}
v, _, err := config.ListProfiles()
if err != nil {
klog.Errorf("list profiles returned error: %v", err)
}
if len(v) > 1 {

// Profiles hint
if v, _, err := config.ListProfiles(); err == nil && len(v) > 1 {
out.Styled(style.Tip, "To see addons list for other profiles use: `minikube addons -p name list`")
}
}

// ----------------
// Helpers
// ----------------

// colorFunc allows generic coloring
type colorFunc func(string, ...interface{}) string

func isEmoji(s string) bool {
return strings.Contains(s, "✅")
}

func ColorRow(row []string, colored colorFunc) {
for i := range row {
if row[i] == "" || isEmoji(row[i]) {
continue
}
row[i] = colored("%s", row[i])
}
}

var printAddonsJSON = func(cc *config.ClusterConfig) {
addonNames := slices.Sorted(maps.Keys(assets.Addons))
addonsMap := map[string]map[string]interface{}{}
Expand Down
22 changes: 18 additions & 4 deletions cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/minikube/pkg/minikube/style"

"github.com/docker/machine/libmachine"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/tw"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -178,13 +179,26 @@ func profilesToTableData(profiles []*config.Profile) [][]string {
if p.ActiveKubeContext {
k = "*"
}
var row []string
if isDetailed {
data = append(data, []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime,
cpIP, strconv.Itoa(cpPort), k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c, k})
row = []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime,
cpIP, strconv.Itoa(cpPort), k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c, k}
} else {
data = append(data, []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime,
cpIP, k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c, k})
row = []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime,
cpIP, k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c, k}
}

// Apply coloring based on status
switch p.Status {
case "OK":
ColorRow(row, color.GreenString)
case "Stopped", "Paused":
ColorRow(row, color.YellowString)
default:
ColorRow(row, color.WhiteString)
}

data = append(data, row)
}
return data
}
Expand Down