Skip to content

Commit

Permalink
Add json output for addons list
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Woodcock committed Oct 11, 2019
1 parent f39ecda commit f026626
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
56 changes: 50 additions & 6 deletions cmd/minikube/cmd/config/addons_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ limitations under the License.
package config

import (
"encoding/json"
"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 +48,16 @@ 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":
PrintAddonsListList()
case "json":
PrintAddonsListJSON()
}
},
}
Expand All @@ -59,6 +70,14 @@ 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)
}

Expand All @@ -69,7 +88,7 @@ func stringFromStatus(addonStatus bool) string {
return "disabled"
}

func addonList() error {
func PrintAddonsListList() {
addonNames := make([]string, 0, len(assets.Addons))
for addonName := range assets.Addons {
addonNames = append(addonNames, addonName)
Expand All @@ -80,7 +99,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 +111,30 @@ func addonList() error {
exit.WithError("Error executing list template", err)
}
}
return nil
}

func PrintAddonsListJSON() {
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

0 comments on commit f026626

Please sign in to comment.