Skip to content

Commit

Permalink
Add -o json option for "minikube addon images"
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed Aug 2, 2024
1 parent 34a974f commit efc9d22
Showing 1 changed file with 59 additions and 22 deletions.
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 @@ limitations under the License.
package config

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

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand All @@ -27,6 +30,8 @@ import (
"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 @@ var addonsImagesCmd = &cobra.Command{
}

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})
}
},
} 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)
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)
}

0 comments on commit efc9d22

Please sign in to comment.