Skip to content

Commit f026626

Browse files
author
Josh Woodcock
committed
Add json output for addons list
1 parent f39ecda commit f026626

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

cmd/minikube/cmd/config/addons_list.go

+50-6
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ limitations under the License.
1717
package config
1818

1919
import (
20+
"encoding/json"
2021
"os"
2122
"sort"
23+
"strings"
2224
"text/template"
2325

2426
"github.com/spf13/cobra"
2527
"k8s.io/minikube/pkg/minikube/assets"
2628
"k8s.io/minikube/pkg/minikube/exit"
29+
"k8s.io/minikube/pkg/minikube/out"
2730
)
2831

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

3134
var addonListFormat string
35+
var addonListOutput string
3236

3337
// AddonListTemplate represents the addon list template
3438
type AddonListTemplate struct {
@@ -44,9 +48,16 @@ var addonsListCmd = &cobra.Command{
4448
if len(args) != 0 {
4549
exit.UsageT("usage: minikube addons list")
4650
}
47-
err := addonList()
48-
if err != nil {
49-
exit.WithError("addon list failed", err)
51+
52+
if addonListOutput != "list" && addonListFormat != defaultAddonListFormat {
53+
exit.UsageT("Cannot use both --output and --format options")
54+
}
55+
56+
switch strings.ToLower(addonListOutput) {
57+
case "list":
58+
PrintAddonsListList()
59+
case "json":
60+
PrintAddonsListJSON()
5061
}
5162
},
5263
}
@@ -59,6 +70,14 @@ func init() {
5970
defaultAddonListFormat,
6071
`Go template format string for the addon list output. The format for Go templates can be found here: https://golang.org/pkg/text/template/
6172
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`)
73+
74+
addonsListCmd.Flags().StringVarP(
75+
&addonListOutput,
76+
"output",
77+
"o",
78+
"list",
79+
`minikube addons list --output OUTPUT. json, list`)
80+
6281
AddonsCmd.AddCommand(addonsListCmd)
6382
}
6483

@@ -69,7 +88,7 @@ func stringFromStatus(addonStatus bool) string {
6988
return "disabled"
7089
}
7190

72-
func addonList() error {
91+
func PrintAddonsListList() {
7392
addonNames := make([]string, 0, len(assets.Addons))
7493
for addonName := range assets.Addons {
7594
addonNames = append(addonNames, addonName)
@@ -80,7 +99,7 @@ func addonList() error {
8099
addonBundle := assets.Addons[addonName]
81100
addonStatus, err := addonBundle.IsEnabled()
82101
if err != nil {
83-
return err
102+
exit.WithError("Error getting addons status", err)
84103
}
85104
tmpl, err := template.New("list").Parse(addonListFormat)
86105
if err != nil {
@@ -92,5 +111,30 @@ func addonList() error {
92111
exit.WithError("Error executing list template", err)
93112
}
94113
}
95-
return nil
114+
}
115+
116+
func PrintAddonsListJSON() {
117+
addonNames := make([]string, 0, len(assets.Addons))
118+
for addonName := range assets.Addons {
119+
addonNames = append(addonNames, addonName)
120+
}
121+
sort.Strings(addonNames)
122+
123+
addonsMap := map[string]map[string]interface{}{}
124+
125+
for _, addonName := range addonNames {
126+
addonBundle := assets.Addons[addonName]
127+
128+
addonStatus, err := addonBundle.IsEnabled()
129+
if err != nil {
130+
exit.WithError("Error getting addons status", err)
131+
}
132+
133+
addonsMap[addonName] = map[string]interface{}{
134+
"Status": stringFromStatus(addonStatus),
135+
}
136+
}
137+
jsonString, _ := json.Marshal(addonsMap)
138+
139+
out.String(string(jsonString))
96140
}

test/integration/functional_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,17 @@ func validateAddonsCmd(ctx context.Context, t *testing.T, profile string) {
360360
t.Errorf("Plugin output did not match expected custom format. Got: %s", line)
361361
}
362362
}
363+
364+
// Json output
365+
rr, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "addons", "list", "-o", "json"))
366+
if err != nil {
367+
t.Errorf("%s failed: %v", rr.Args, err)
368+
}
369+
var jsonObject map[string]interface{}
370+
err = json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
371+
if err != nil {
372+
t.Errorf("%s failed: %v", rr.Args, err)
373+
}
363374
}
364375

365376
// validateSSHCmd asserts basic "ssh" command functionality

0 commit comments

Comments
 (0)