Skip to content

Commit

Permalink
add advance checking
Browse files Browse the repository at this point in the history
  • Loading branch information
DexterYan committed Apr 26, 2024
1 parent f252c1e commit 36f4fd6
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions cmd/troubleshoot/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ func checkSpecStructure(path string) error {
return err
}

analyzerFields := listFieldNames(&troubleshootv1beta2.Analyze{})

analyzeFieldsInLowerCamelCase := strings.ToLower(strings.Join(analyzerFields, " "))

for _, n := range node.Content[0].Content { // Traverse the root map
if n.Kind == yaml.MappingNode && n.Tag == "!!map" {
for i := 0; i < len(n.Content); i += 2 {
Expand All @@ -404,11 +408,15 @@ func checkSpecStructure(path string) error {
for j := 0; j < len(specNode.Content); j += 2 {
analyzerKey := specNode.Content[j]
analyzerVal := specNode.Content[j+1]
if analyzerKey.Value == "distribution" {
if strings.Contains(analyzeFieldsInLowerCamelCase, strings.ToLower(analyzerKey.Value)) {
if len(analyzerVal.Content) == 0 {
fmt.Println("distribution is empty")
if specNode.Content[j+2] != nil && specNode.Content[j+2].Value == "outcomes" {
fmt.Println("outcomes is misaligned in distribution")
fmt.Println("====================")
fmt.Printf("%s analyzer is empty\n", analyzerKey.Value)
fmt.Println("--------------------")
for k := j + 2; k < len(specNode.Content); k += 2 {
if specNode.Content[k].Value != "" && !strings.Contains(analyzeFieldsInLowerCamelCase, strings.ToLower(specNode.Content[k].Value)) {
fmt.Printf("%s is misaligned in %s\n", specNode.Content[k].Value, analyzerKey.Value)
}
}
}
}
Expand All @@ -419,10 +427,18 @@ func checkSpecStructure(path string) error {
}
}
}

return nil
}

func listFieldNames(v interface{}) []string {
val := reflect.ValueOf(v).Elem()
fieldNames := make([]string, val.NumField())
for i := 0; i < val.NumField(); i++ {
fieldNames[i] = val.Type().Field(i).Name
}
return fieldNames
}

func isStructEmpty(s interface{}) bool {
val := reflect.ValueOf(s).Elem()
for i := 0; i < val.NumField(); i++ {
Expand Down

0 comments on commit 36f4fd6

Please sign in to comment.