Skip to content

Commit b585dfd

Browse files
committed
chore: address feedback and simplify
Signed-off-by: Alan Clucas <[email protected]>
1 parent ce9f1e9 commit b585dfd

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

hack/featuregen/contents.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ type feature struct {
1818
Details string
1919
}
2020

21-
// Combined regex patterns for metadata fields
22-
var metadataPatterns = map[string]*regexp.Regexp{
23-
"component": regexp.MustCompile(`Component:\s*(.*?)(?:\n|$)`),
24-
"issues": regexp.MustCompile(`Issues:\s*(.*?)(?:\n|$)`),
25-
"description": regexp.MustCompile(`Description:\s*(.*?)(?:\n|$)`),
26-
"author": regexp.MustCompile(`Author:\s*(.*?)(?:\n|$)`),
21+
// Metadata field definitions
22+
var (
23+
metadataFields = []string{"Component", "Issues", "Description", "Author"}
24+
)
25+
26+
func getMetadataPattern(field string) *regexp.Regexp {
27+
return regexp.MustCompile(field + `:\s*(.*?)(?:\n|$)`)
2728
}
2829

2930
func validateHeaders(content string) (bool, string) {
@@ -49,11 +50,14 @@ func validateMetadataOrder(content string) (bool, string) {
4950
metadataEnd := 0
5051
for i := 0; i < len(lines); i++ {
5152
line := lines[i]
52-
if !strings.HasPrefix(line, "Component:") &&
53-
!strings.HasPrefix(line, "Issues:") &&
54-
!strings.HasPrefix(line, "Description:") &&
55-
!strings.HasPrefix(line, "Author:") &&
56-
line != "" {
53+
isMetadata := false
54+
for _, field := range metadataFields {
55+
if strings.HasPrefix(line, field+":") {
56+
isMetadata = true
57+
break
58+
}
59+
}
60+
if !isMetadata && line != "" {
5761
metadataEnd = i
5862
break
5963
}
@@ -62,11 +66,10 @@ func validateMetadataOrder(content string) (bool, string) {
6266
// Check if any metadata fields appear after this point
6367
for i := metadataEnd; i < len(lines); i++ {
6468
line := lines[i]
65-
if strings.HasPrefix(line, "Component:") ||
66-
strings.HasPrefix(line, "Issues:") ||
67-
strings.HasPrefix(line, "Description:") ||
68-
strings.HasPrefix(line, "Author:") {
69-
return false, fmt.Sprintf("Metadata field '%s' must appear before any other content", line)
69+
for _, field := range metadataFields {
70+
if strings.HasPrefix(line, field+":") {
71+
return false, fmt.Sprintf("Metadata field '%s' must appear before any other content", line)
72+
}
7073
}
7174
}
7275

@@ -75,11 +78,10 @@ func validateMetadataOrder(content string) (bool, string) {
7578

7679
func parseContent(source string, content string) (bool, feature, error) {
7780
// Check required sections
78-
requiredSections := []string{"Component:", "Issues:", "Description:", "Author:"}
7981
isValid := true
80-
for _, section := range requiredSections {
81-
if !strings.Contains(content, section) {
82-
fmt.Printf("Error: Missing required section '%s' in %s\n", section, source)
82+
for _, field := range metadataFields {
83+
if !strings.Contains(content, field+":") {
84+
fmt.Printf("Error: Missing required section '%s:' in %s\n", field, source)
8385
isValid = false
8486
}
8587
}
@@ -96,7 +98,7 @@ func parseContent(source string, content string) (bool, feature, error) {
9698

9799
// Extract metadata fields
98100
component := ""
99-
if matches := metadataPatterns["component"].FindStringSubmatch(content); len(matches) > 1 {
101+
if matches := getMetadataPattern("Component").FindStringSubmatch(content); len(matches) > 1 {
100102
component = strings.TrimSpace(matches[1])
101103
if !isValidComponent(component) {
102104
fmt.Printf("Error: Invalid component '%s' in %s. Valid components are: %s. Add more in hack/featuregen/components.go\n", component, source, listValidComponents())
@@ -105,7 +107,7 @@ func parseContent(source string, content string) (bool, feature, error) {
105107
}
106108

107109
issuesSection := ""
108-
if matches := metadataPatterns["issues"].FindStringSubmatch(content); len(matches) > 1 {
110+
if matches := getMetadataPattern("Issues").FindStringSubmatch(content); len(matches) > 1 {
109111
issuesSection = matches[1]
110112
}
111113
issues := regexp.MustCompile(`(\d+)`).FindAllStringSubmatch(issuesSection, -1)
@@ -119,18 +121,19 @@ func parseContent(source string, content string) (bool, feature, error) {
119121
}
120122

121123
description := ""
122-
if matches := metadataPatterns["description"].FindStringSubmatch(content); len(matches) > 1 {
124+
if matches := getMetadataPattern("Description").FindStringSubmatch(content); len(matches) > 1 {
123125
description = strings.TrimSpace(matches[1])
124126
}
125127

126128
author := ""
127-
if matches := metadataPatterns["author"].FindStringSubmatch(content); len(matches) > 1 {
129+
if matches := getMetadataPattern("Author").FindStringSubmatch(content); len(matches) > 1 {
128130
author = strings.TrimSpace(matches[1])
129131
}
130132

131133
// Extract details (everything after metadata)
132134
details := ""
133-
if detailsMatch := regexp.MustCompile(`(?s)(?:Component:|Issues:|Description:|Author:).*?\n\n(.*)`).FindStringSubmatch(content); len(detailsMatch) > 1 {
135+
pattern := `(?s)(?:` + strings.Join(metadataFields, ":|") + `:).*?\n\n(.*)`
136+
if detailsMatch := regexp.MustCompile(pattern).FindStringSubmatch(content); len(detailsMatch) > 1 {
134137
details = strings.TrimSpace(detailsMatch[1])
135138
}
136139

0 commit comments

Comments
 (0)