@@ -17,18 +17,23 @@ limitations under the License.
17
17
package config
18
18
19
19
import (
20
+ "encoding/json"
21
+ "fmt"
20
22
"os"
21
23
"sort"
24
+ "strings"
22
25
"text/template"
23
26
24
27
"github.com/spf13/cobra"
25
28
"k8s.io/minikube/pkg/minikube/assets"
26
29
"k8s.io/minikube/pkg/minikube/exit"
30
+ "k8s.io/minikube/pkg/minikube/out"
27
31
)
28
32
29
33
const defaultAddonListFormat = "- {{.AddonName}}: {{.AddonStatus}}\n "
30
34
31
35
var addonListFormat string
36
+ var addonListOutput string
32
37
33
38
// AddonListTemplate represents the addon list template
34
39
type AddonListTemplate struct {
@@ -44,9 +49,18 @@ var addonsListCmd = &cobra.Command{
44
49
if len (args ) != 0 {
45
50
exit .UsageT ("usage: minikube addons list" )
46
51
}
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 ))
50
64
}
51
65
},
52
66
}
@@ -59,17 +73,25 @@ func init() {
59
73
defaultAddonListFormat ,
60
74
`Go template format string for the addon list output. The format for Go templates can be found here: https://golang.org/pkg/text/template/
61
75
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
+
62
84
AddonsCmd .AddCommand (addonsListCmd )
63
85
}
64
86
65
- func stringFromStatus (addonStatus bool ) string {
87
+ var stringFromStatus = func (addonStatus bool ) string {
66
88
if addonStatus {
67
89
return "enabled"
68
90
}
69
91
return "disabled"
70
92
}
71
93
72
- func addonList () error {
94
+ var printAddonsList = func () {
73
95
addonNames := make ([]string , 0 , len (assets .Addons ))
74
96
for addonName := range assets .Addons {
75
97
addonNames = append (addonNames , addonName )
@@ -80,7 +102,7 @@ func addonList() error {
80
102
addonBundle := assets .Addons [addonName ]
81
103
addonStatus , err := addonBundle .IsEnabled ()
82
104
if err != nil {
83
- return err
105
+ exit . WithError ( "Error getting addons status" , err )
84
106
}
85
107
tmpl , err := template .New ("list" ).Parse (addonListFormat )
86
108
if err != nil {
@@ -92,5 +114,30 @@ func addonList() error {
92
114
exit .WithError ("Error executing list template" , err )
93
115
}
94
116
}
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 ))
96
143
}
0 commit comments