@@ -17,9 +17,11 @@ limitations under the License.
17
17
package config
18
18
19
19
import (
20
+ "encoding/json"
20
21
"fmt"
21
22
"os"
22
23
"strconv"
24
+ "strings"
23
25
24
26
"k8s.io/minikube/pkg/minikube/config"
25
27
"k8s.io/minikube/pkg/minikube/exit"
@@ -29,48 +31,101 @@ import (
29
31
"github.com/spf13/cobra"
30
32
)
31
33
34
+ var (
35
+ output string
36
+ )
37
+
32
38
var profileListCmd = & cobra.Command {
33
39
Use : "list" ,
34
40
Short : "Lists all minikube profiles." ,
35
41
Long : "Lists all valid minikube profiles and detects all possible invalid profiles." ,
36
42
Run : func (cmd * cobra.Command , args []string ) {
37
43
38
- var validData [][]string
44
+ switch strings .ToLower (output ) {
45
+ case "json" :
46
+ printProfilesJSON ()
47
+ case "table" :
48
+ printProfilesTable ()
49
+ default :
50
+ exit .WithCodeT (exit .BadUsage , fmt .Sprintf ("invalid output format: %s. Valid values: 'table', 'json'" , output ))
51
+ }
52
+
53
+ },
54
+ }
55
+
56
+ var printProfilesTable = func () {
39
57
40
- table := tablewriter .NewWriter (os .Stdout )
41
- table .SetHeader ([]string {"Profile" , "VM Driver" , "NodeIP" , "Node Port" , "Kubernetes Version" })
42
- table .SetAutoFormatHeaders (false )
43
- table .SetBorders (tablewriter.Border {Left : true , Top : true , Right : true , Bottom : true })
44
- table .SetCenterSeparator ("|" )
45
- validProfiles , invalidProfiles , err := config .ListProfiles ()
58
+ var validData [][]string
46
59
47
- if len ( validProfiles ) == 0 || err != nil {
48
- exit . UsageT ( "No minikube profile was found. You can create one using `minikube start`." )
49
- }
50
- for _ , p := range validProfiles {
51
- validData = append ( validData , [] string { p . Name , p . Config . MachineConfig . VMDriver , p . Config . KubernetesConfig . NodeIP , strconv . Itoa ( p . Config . KubernetesConfig . NodePort ), p . Config . KubernetesConfig . KubernetesVersion } )
52
- }
60
+ table := tablewriter . NewWriter ( os . Stdout )
61
+ table . SetHeader ([] string { "Profile" , "VM Driver" , "NodeIP" , "Node Port" , "Kubernetes Version" } )
62
+ table . SetAutoFormatHeaders ( false )
63
+ table . SetBorders (tablewriter. Border { Left : true , Top : true , Right : true , Bottom : true })
64
+ table . SetCenterSeparator ( "|" )
65
+ validProfiles , invalidProfiles , err := config . ListProfiles ()
53
66
54
- table .AppendBulk (validData )
55
- table .Render ()
67
+ if len (validProfiles ) == 0 || err != nil {
68
+ exit .UsageT ("No minikube profile was found. You can create one using `minikube start`." )
69
+ }
70
+ for _ , p := range validProfiles {
71
+ validData = append (validData , []string {p .Name , p .Config .MachineConfig .VMDriver , p .Config .KubernetesConfig .NodeIP , strconv .Itoa (p .Config .KubernetesConfig .NodePort ), p .Config .KubernetesConfig .KubernetesVersion })
72
+ }
56
73
57
- if invalidProfiles != nil {
58
- out .T (out .WarningType , "Found {{.number}} invalid profile(s) ! " , out.V {"number" : len (invalidProfiles )})
59
- for _ , p := range invalidProfiles {
60
- out .T (out .Empty , "\t " + p .Name )
61
- }
62
- out .T (out .Tip , "You can delete them using the following command(s): " )
63
- for _ , p := range invalidProfiles {
64
- out .String (fmt .Sprintf ("\t $ minikube delete -p %s \n " , p .Name ))
65
- }
74
+ table .AppendBulk (validData )
75
+ table .Render ()
66
76
77
+ if invalidProfiles != nil {
78
+ out .T (out .WarningType , "Found {{.number}} invalid profile(s) ! " , out.V {"number" : len (invalidProfiles )})
79
+ for _ , p := range invalidProfiles {
80
+ out .T (out .Empty , "\t " + p .Name )
67
81
}
68
- if err != nil {
69
- exit .WithCodeT (exit .Config , fmt .Sprintf ("error loading profiles: %v" , err ))
82
+ out .T (out .Tip , "You can delete them using the following command(s): " )
83
+ for _ , p := range invalidProfiles {
84
+ out .String (fmt .Sprintf ("\t $ minikube delete -p %s \n " , p .Name ))
70
85
}
71
- },
86
+
87
+ }
88
+
89
+ if err != nil {
90
+ exit .WithCodeT (exit .Config , fmt .Sprintf ("error loading profiles: %v" , err ))
91
+ }
92
+
93
+ }
94
+
95
+ var printProfilesJSON = func () {
96
+ validProfiles , invalidProfiles , err := config .ListProfiles ()
97
+
98
+ var valid []* config.Profile
99
+ var invalid []* config.Profile
100
+
101
+ if validProfiles != nil {
102
+ valid = validProfiles
103
+ } else {
104
+ valid = []* config.Profile {}
105
+ }
106
+
107
+ if invalidProfiles != nil {
108
+ invalid = invalidProfiles
109
+ } else {
110
+ invalid = []* config.Profile {}
111
+ }
112
+
113
+ var body = map [string ]interface {}{}
114
+
115
+ if err == nil {
116
+ body ["valid" ] = valid
117
+ body ["invalid" ] = invalid
118
+ jsonString , _ := json .Marshal (body )
119
+ out .String (string (jsonString ))
120
+ } else {
121
+ body ["error" ] = err
122
+ jsonString , _ := json .Marshal (body )
123
+ out .String (string (jsonString ))
124
+ os .Exit (exit .Failure )
125
+ }
72
126
}
73
127
74
128
func init () {
129
+ profileListCmd .Flags ().StringVarP (& output , "output" , "o" , "table" , "The output format. One of 'json', 'table'" )
75
130
ProfileCmd .AddCommand (profileListCmd )
76
131
}
0 commit comments