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 5507db0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
59 changes: 53 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,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,6 +73,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 +91,7 @@ func stringFromStatus(addonStatus bool) string {
return "disabled"
}

func addonList() error {
func PrintAddonsList() {
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
}

func PrintAddonsJSON() {
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 5507db0

Please sign in to comment.