-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
explain.go
251 lines (218 loc) · 7.17 KB
/
explain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubectl
import (
"fmt"
"io"
"strings"
"github.com/emicklei/go-restful/swagger"
"k8s.io/kubernetes/pkg/api/meta"
apiutil "k8s.io/kubernetes/pkg/api/util"
)
var allModels = make(map[string]*swagger.NamedModel)
var recursive = false // this is global for convenience, can become int for multiple levels
// SplitAndParseResourceRequest separates the users input into a model and fields
func SplitAndParseResourceRequest(inResource string, mapper meta.RESTMapper) (string, []string, error) {
inResource, fieldsPath := splitDotNotation(inResource)
inResource, _ = mapper.ResourceSingularizer(inResource)
return inResource, fieldsPath, nil
}
// PrintModelDescription prints the description of a specific model or dot path
func PrintModelDescription(inModel string, fieldsPath []string, w io.Writer, swaggerSchema *swagger.ApiDeclaration, r bool) error {
recursive = r // this is global for convenience
apiVer := apiutil.GetVersion(swaggerSchema.ApiVersion) + "."
var pointedModel *swagger.NamedModel
for i := range swaggerSchema.Models.List {
name := swaggerSchema.Models.List[i].Name
allModels[name] = &swaggerSchema.Models.List[i]
if strings.ToLower(name) == strings.ToLower(apiVer+inModel) {
pointedModel = &swaggerSchema.Models.List[i]
}
}
if pointedModel == nil {
return fmt.Errorf("requested resource %q is not defined", inModel)
}
if len(fieldsPath) == 0 {
return printTopLevelResourceInfo(w, pointedModel)
}
var pointedModelAsProp *swagger.NamedModelProperty
for _, field := range fieldsPath {
if prop, nextModel, isModel := getField(pointedModel, field); prop != nil {
if isModel {
pointedModelAsProp = prop
pointedModel = allModels[nextModel]
} else {
return printPrimitive(w, prop)
}
} else {
return fmt.Errorf("field %q does not exist", field)
}
}
return printModelInfo(w, pointedModel, pointedModelAsProp)
}
func splitDotNotation(model string) (string, []string) {
var fieldsPath []string
dotModel := strings.Split(model, ".")
if len(dotModel) >= 1 {
fieldsPath = dotModel[1:]
}
return dotModel[0], fieldsPath
}
func getPointedModel(prop *swagger.ModelProperty) (string, bool) {
if prop.Ref != nil {
return *prop.Ref, true
} else if *prop.Type == "array" && prop.Items.Ref != nil {
return *prop.Items.Ref, true
}
return "", false
}
func getField(model *swagger.NamedModel, sField string) (*swagger.NamedModelProperty, string, bool) {
for _, prop := range model.Model.Properties.List {
if prop.Name == sField {
pointedModel, isModel := getPointedModel(&prop.Property)
return &prop, pointedModel, isModel
}
}
return nil, "", false
}
func printModelInfo(w io.Writer, model *swagger.NamedModel, modelProp *swagger.NamedModelProperty) error {
t, _ := getFieldType(&modelProp.Property)
fmt.Fprintf(w, "RESOURCE: %s <%s>\n\n", modelProp.Name, t)
fieldDesc, _ := wrapAndIndentText(modelProp.Property.Description, " ", 80)
fmt.Fprintf(w, "DESCRIPTION:\n%s\n\n%s\n", fieldDesc, indentText(model.Model.Description, " "))
return printFields(w, model)
}
func printPrimitive(w io.Writer, field *swagger.NamedModelProperty) error {
t, _ := getFieldType(&field.Property)
fmt.Fprintf(w, "FIELD: %s <%s>\n\n", field.Name, t)
d, _ := wrapAndIndentText(field.Property.Description, " ", 80)
fmt.Fprintf(w, "DESCRIPTION:\n%s\n", d)
return nil
}
func printTopLevelResourceInfo(w io.Writer, model *swagger.NamedModel) error {
fmt.Fprintf(w, "DESCRIPTION:\n%s\n", model.Model.Description)
return printFields(w, model)
}
func printFields(w io.Writer, model *swagger.NamedModel) error {
fmt.Fprint(w, "\nFIELDS:\n")
for _, field := range model.Model.Properties.List {
fieldType, err := getFieldType(&field.Property)
if err != nil {
return err
}
if arrayContains(model.Model.Required, field.Name) {
fmt.Fprintf(w, " %s\t<%s> -required-\n", field.Name, fieldType)
} else {
fmt.Fprintf(w, " %s\t<%s>\n", field.Name, fieldType)
}
if recursive {
pointedModel, isModel := getPointedModel(&field.Property)
if isModel {
for _, nestedField := range allModels[pointedModel].Model.Properties.List {
t, _ := getFieldType(&nestedField.Property)
fmt.Fprintf(w, " %s\t<%s>\n", nestedField.Name, t)
}
}
} else {
fieldDesc, _ := wrapAndIndentText(field.Property.Description, " ", 80)
fmt.Fprintf(w, "%s\n\n", fieldDesc)
}
}
fmt.Fprint(w, "\n")
return nil
}
func getFieldType(prop *swagger.ModelProperty) (string, error) {
if prop.Type == nil {
return "Object", nil
} else if *prop.Type == "any" {
// Swagger Spec doesn't return information for maps.
return "map[string]string", nil
} else if *prop.Type == "array" {
if prop.Items == nil {
return "", fmt.Errorf("error in swagger spec. Property: %v contains an array without type", prop)
}
if prop.Items.Ref != nil {
fieldType := "[]Object"
return fieldType, nil
}
fieldType := "[]" + *prop.Items.Type
return fieldType, nil
}
return *prop.Type, nil
}
func wrapAndIndentText(desc, indent string, lim int) (string, error) {
words := strings.Split(strings.Replace(strings.TrimSpace(desc), "\n", " ", -1), " ")
n := len(words)
for i := 0; i < n; i++ {
if len(words[i]) > lim {
if strings.Contains(words[i], "/") {
s := breakURL(words[i])
words = append(words[:i], append(s, words[i+1:]...)...)
i = i + len(s) - 1
} else {
fmt.Println(len(words[i]))
return "", fmt.Errorf("there are words longer that the break limit is")
}
}
}
var lines []string
line := []string{indent}
lineL := len(indent)
for i := 0; i < len(words); i++ {
w := words[i]
if strings.HasSuffix(w, "/") && lineL+len(w)-1 < lim {
prev := line[len(line)-1]
if strings.HasSuffix(prev, "/") {
if i+1 < len(words)-1 && !strings.HasSuffix(words[i+1], "/") {
w = strings.TrimSuffix(w, "/")
}
line[len(line)-1] = prev + w
lineL += len(w)
} else {
line = append(line, w)
lineL += len(w) + 1
}
} else if lineL+len(w) < lim {
line = append(line, w)
lineL += len(w) + 1
} else {
lines = append(lines, strings.Join(line, " "))
line = []string{indent, w}
lineL = len(indent) + len(w)
}
}
lines = append(lines, strings.Join(line, " "))
return strings.Join(lines, "\n"), nil
}
func breakURL(url string) []string {
var buf []string
for _, part := range strings.Split(url, "/") {
buf = append(buf, part+"/")
}
return buf
}
func indentText(text, indent string) string {
lines := strings.Split(text, "\n")
for i := range lines {
lines[i] = indent + lines[i]
}
return strings.Join(lines, "\n")
}
func arrayContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}