Skip to content

Commit b132b04

Browse files
author
Josh Woodcock
committed
Add json output for addons list
1 parent 6d63e43 commit b132b04

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

Diff for: cmd/minikube/cmd/config/addons_list.go

+54-7
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ limitations under the License.
1717
package config
1818

1919
import (
20+
"encoding/json"
21+
"fmt"
2022
"os"
2123
"sort"
24+
"strings"
2225
"text/template"
2326

2427
"github.com/spf13/cobra"
2528
"k8s.io/minikube/pkg/minikube/assets"
2629
"k8s.io/minikube/pkg/minikube/exit"
30+
"k8s.io/minikube/pkg/minikube/out"
2731
)
2832

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

3135
var addonListFormat string
36+
var addonListOutput string
3237

3338
// AddonListTemplate represents the addon list template
3439
type AddonListTemplate struct {
@@ -44,9 +49,18 @@ var addonsListCmd = &cobra.Command{
4449
if len(args) != 0 {
4550
exit.UsageT("usage: minikube addons list")
4651
}
47-
err := addonList()
48-
if err != nil {
49-
exit.WithError("addon list failed", err)
52+
53+
if addonListOutput != "list" && addonListFormat != defaultAddonListFormat {
54+
exit.UsageT("Cannot use both --output and --format options")
55+
}
56+
57+
switch strings.ToLower(addonListOutput) {
58+
case "list":
59+
printAddonsList()
60+
case "json":
61+
printAddonsJSON()
62+
default:
63+
exit.WithCodeT(exit.BadUsage, fmt.Sprintf("invalid output format: %s. Valid values: 'list', 'json'", addonListOutput))
5064
}
5165
},
5266
}
@@ -59,17 +73,25 @@ func init() {
5973
defaultAddonListFormat,
6074
`Go template format string for the addon list output. The format for Go templates can be found here: https://golang.org/pkg/text/template/
6175
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`)
76+
77+
addonsListCmd.Flags().StringVarP(
78+
&addonListOutput,
79+
"output",
80+
"o",
81+
"list",
82+
`minikube addons list --output OUTPUT. json, list`)
83+
6284
AddonsCmd.AddCommand(addonsListCmd)
6385
}
6486

65-
func stringFromStatus(addonStatus bool) string {
87+
var stringFromStatus = func(addonStatus bool) string {
6688
if addonStatus {
6789
return "enabled"
6890
}
6991
return "disabled"
7092
}
7193

72-
func addonList() error {
94+
var printAddonsList = func() {
7395
addonNames := make([]string, 0, len(assets.Addons))
7496
for addonName := range assets.Addons {
7597
addonNames = append(addonNames, addonName)
@@ -80,7 +102,7 @@ func addonList() error {
80102
addonBundle := assets.Addons[addonName]
81103
addonStatus, err := addonBundle.IsEnabled()
82104
if err != nil {
83-
return err
105+
exit.WithError("Error getting addons status", err)
84106
}
85107
tmpl, err := template.New("list").Parse(addonListFormat)
86108
if err != nil {
@@ -92,5 +114,30 @@ func addonList() error {
92114
exit.WithError("Error executing list template", err)
93115
}
94116
}
95-
return nil
117+
}
118+
119+
var printAddonsJSON = func() {
120+
addonNames := make([]string, 0, len(assets.Addons))
121+
for addonName := range assets.Addons {
122+
addonNames = append(addonNames, addonName)
123+
}
124+
sort.Strings(addonNames)
125+
126+
addonsMap := map[string]map[string]interface{}{}
127+
128+
for _, addonName := range addonNames {
129+
addonBundle := assets.Addons[addonName]
130+
131+
addonStatus, err := addonBundle.IsEnabled()
132+
if err != nil {
133+
exit.WithError("Error getting addons status", err)
134+
}
135+
136+
addonsMap[addonName] = map[string]interface{}{
137+
"Status": stringFromStatus(addonStatus),
138+
}
139+
}
140+
jsonString, _ := json.Marshal(addonsMap)
141+
142+
out.String(string(jsonString))
96143
}

Diff for: 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)