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 JSON output for addons list #5601

Merged
Merged
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
61 changes: 54 additions & 7 deletions cmd/minikube/cmd/config/addons_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ limitations under the License.
package config

import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"text/template"

"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out"
)

const defaultAddonListFormat = "- {{.AddonName}}: {{.AddonStatus}}\n"

var addonListFormat string
var addonListOutput string

// AddonListTemplate represents the addon list template
type AddonListTemplate struct {
Expand All @@ -44,9 +49,18 @@ var addonsListCmd = &cobra.Command{
if len(args) != 0 {
exit.UsageT("usage: minikube addons list")
}
err := addonList()
if err != nil {
exit.WithError("addon list failed", err)

if addonListOutput != "list" && addonListFormat != defaultAddonListFormat {
exit.UsageT("Cannot use both --output and --format options")
}

switch strings.ToLower(addonListOutput) {
case "list":
printAddonsList()
case "json":
printAddonsJSON()
default:
exit.WithCodeT(exit.BadUsage, fmt.Sprintf("invalid output format: %s. Valid values: 'list', 'json'", addonListOutput))
}
},
}
Expand All @@ -59,17 +73,25 @@ func init() {
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(
&addonListOutput,
"output",
"o",
"list",
`minikube addons list --output OUTPUT. json, list`)

AddonsCmd.AddCommand(addonsListCmd)
}

func stringFromStatus(addonStatus bool) string {
var stringFromStatus = func(addonStatus bool) string {
if addonStatus {
return "enabled"
}
return "disabled"
}

func addonList() error {
var printAddonsList = func() {
addonNames := make([]string, 0, len(assets.Addons))
for addonName := range assets.Addons {
addonNames = append(addonNames, addonName)
Expand All @@ -80,7 +102,7 @@ func addonList() error {
addonBundle := assets.Addons[addonName]
addonStatus, err := addonBundle.IsEnabled()
if err != nil {
return err
exit.WithError("Error getting addons status", err)
}
tmpl, err := template.New("list").Parse(addonListFormat)
if err != nil {
Expand All @@ -92,5 +114,30 @@ func addonList() error {
exit.WithError("Error executing list template", err)
}
}
return nil
}

var printAddonsJSON = func() {
addonNames := make([]string, 0, len(assets.Addons))
for addonName := range assets.Addons {
addonNames = append(addonNames, addonName)
}
sort.Strings(addonNames)

addonsMap := map[string]map[string]interface{}{}

for _, addonName := range addonNames {
addonBundle := assets.Addons[addonName]

addonStatus, err := addonBundle.IsEnabled()
if err != nil {
exit.WithError("Error getting addons status", err)
}

addonsMap[addonName] = map[string]interface{}{
"Status": stringFromStatus(addonStatus),
}
}
jsonString, _ := json.Marshal(addonsMap)

out.String(string(jsonString))
}
11 changes: 11 additions & 0 deletions test/integration/functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,17 @@ func validateAddonsCmd(ctx context.Context, t *testing.T, profile string) {
t.Errorf("Plugin output did not match expected custom format. Got: %s", line)
}
}

// Json output
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "addons", "list", "-o", "json"))
if err != nil {
t.Errorf("%s failed: %v", rr.Args, err)
}
var jsonObject map[string]interface{}
err = json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
if err != nil {
t.Errorf("%s failed: %v", rr.Args, err)
}
}

// validateSSHCmd asserts basic "ssh" command functionality
Expand Down