Skip to content

Commit 79be0f5

Browse files
committed
add advance checking
1 parent 324f069 commit 79be0f5

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

cmd/troubleshoot/cli/run.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ func checkSpecStructure(path string) error {
394394
return err
395395
}
396396

397+
analyzerFields := listFieldNames(&troubleshootv1beta2.Analyze{})
398+
399+
analyzeFieldsInLowerCamelCase := strings.ToLower(strings.Join(analyzerFields, " "))
400+
397401
for _, n := range node.Content[0].Content { // Traverse the root map
398402
if n.Kind == yaml.MappingNode && n.Tag == "!!map" {
399403
for i := 0; i < len(n.Content); i += 2 {
@@ -404,11 +408,15 @@ func checkSpecStructure(path string) error {
404408
for j := 0; j < len(specNode.Content); j += 2 {
405409
analyzerKey := specNode.Content[j]
406410
analyzerVal := specNode.Content[j+1]
407-
if analyzerKey.Value == "distribution" {
411+
if strings.Contains(analyzeFieldsInLowerCamelCase, strings.ToLower(analyzerKey.Value)) {
408412
if len(analyzerVal.Content) == 0 {
409-
fmt.Println("distribution is empty")
410-
if specNode.Content[j+2] != nil && specNode.Content[j+2].Value == "outcomes" {
411-
fmt.Println("outcomes is misaligned in distribution")
413+
fmt.Println("====================")
414+
fmt.Printf("%s analyzer is empty\n", analyzerKey.Value)
415+
fmt.Println("--------------------")
416+
for k := j + 2; k < len(specNode.Content); k += 2 {
417+
if specNode.Content[k].Value != "" && !strings.Contains(analyzeFieldsInLowerCamelCase, strings.ToLower(specNode.Content[k].Value)) {
418+
fmt.Printf("%s is misaligned in %s\n", specNode.Content[k].Value, analyzerKey.Value)
419+
}
412420
}
413421
}
414422
}
@@ -419,10 +427,18 @@ func checkSpecStructure(path string) error {
419427
}
420428
}
421429
}
422-
423430
return nil
424431
}
425432

433+
func listFieldNames(v interface{}) []string {
434+
val := reflect.ValueOf(v).Elem()
435+
fieldNames := make([]string, val.NumField())
436+
for i := 0; i < val.NumField(); i++ {
437+
fieldNames[i] = val.Type().Field(i).Name
438+
}
439+
return fieldNames
440+
}
441+
426442
func isStructEmpty(s interface{}) bool {
427443
val := reflect.ValueOf(s).Elem()
428444
for i := 0; i < val.NumField(); i++ {

0 commit comments

Comments
 (0)