Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "-o json" option for "minikube addon images" #19364

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Changes from 1 commit
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
81 changes: 59 additions & 22 deletions cmd/minikube/cmd/config/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package config

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand All @@ -27,6 +30,8 @@
"k8s.io/minikube/pkg/minikube/reason"
)

var addonImagesOutput string

var addonsImagesCmd = &cobra.Command{
Use: "images ADDON_NAME",
Short: "List image names the addon w/ADDON_NAME used. For a list of available addons use: minikube addons list",
Expand All @@ -38,33 +43,65 @@
}

addon := args[0]
// allows for additional prompting of information when enabling addons
if conf, ok := assets.Addons[addon]; ok {
if conf.Images != nil {
out.Infof("{{.name}} has following images:", out.V{"name": addon})

var tData [][]string
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Image Name", "Default Image", "Default Registry"})
table.SetAutoFormatHeaders(true)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")

for imageName, defaultImage := range conf.Images {
tData = append(tData, []string{imageName, defaultImage, conf.Registries[imageName]})
}

table.AppendBulk(tData)
table.Render()
} else {
out.Infof("{{.name}} doesn't have images.", out.V{"name": addon})

switch strings.ToLower(addonImagesOutput) {
case "table":
printAddonImagesTable(addon)
case "json":
printAddonImagesJson(addon)
default:
exit.Message(reason.Usage, fmt.Sprintf("invalid output format: %s. Valid values: 'table', 'json'", addonImagesOutput))
}
},
}

func printAddonImagesTable(addon string) {
// allows for additional prompting of information when enabling addons
if conf, ok := assets.Addons[addon]; ok {
if conf.Images != nil {
out.Infof("{{.name}} has the following images:", out.V{"name": addon})

var tData [][]string
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Image Name", "Default Image", "Default Registry"})
table.SetAutoFormatHeaders(true)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")

for imageName, defaultImage := range conf.Images {
tData = append(tData, []string{imageName, defaultImage, conf.Registries[imageName]})
}

table.AppendBulk(tData)
table.Render()
} else {
out.FailureT("No such addon {{.name}}", out.V{"name": addon})
out.Infof("{{.name}} doesn't have images.", out.V{"name": addon})
thomasjm marked this conversation as resolved.
Show resolved Hide resolved
}
},
} else {
out.FailureT("No such addon {{.name}}", out.V{"name": addon})
}
}

func printAddonImagesJson(addon string) {

Check failure on line 85 in cmd/minikube/cmd/config/images.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func printAddonImagesJson should be printAddonImagesJSON (revive)
thomasjm marked this conversation as resolved.
Show resolved Hide resolved
if conf, ok := assets.Addons[addon]; ok {
if conf.Images != nil {
var data []string

for imageName, defaultImage := range conf.Images {
data = append(data, conf.Registries[imageName] + "/" + defaultImage)
}

jsonString, _ := json.Marshal(data)
out.String(string(jsonString))
} else {
out.String("[]")
}
} else {
out.FailureT("No such addon {{.name}}", out.V{"name": addon})
}
}

func init() {
addonsImagesCmd.Flags().StringVarP(&addonImagesOutput, "output", "o", "table", "minikube addons images ADDON_NAME --output OUTPUT. table, json")
AddonsCmd.AddCommand(addonsImagesCmd)
}
Loading