Skip to content

Commit c036230

Browse files
committed
handle file.close error
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent f5cb3e4 commit c036230

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

internal/extgen/moduleParser.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,27 @@ type phpModule struct {
2222
type ModuleParser struct{}
2323

2424
// parse parses the source file for PHP module directives
25-
func (mp *ModuleParser) parse(filename string) (*phpModule, error) {
25+
func (mp *ModuleParser) parse(filename string) (module *phpModule, err error) {
2626
file, err := os.Open(filename)
2727
if err != nil {
2828
return nil, err
2929
}
30-
defer file.Close()
30+
defer func() {
31+
e := file.Close()
32+
if err == nil {
33+
err = e
34+
}
35+
}()
3136

3237
scanner := bufio.NewScanner(file)
33-
module := &phpModule{}
38+
module = &phpModule{}
3439
var currentDirective string
3540
var lineNumber int
3641

3742
for scanner.Scan() {
3843
lineNumber++
3944
line := strings.TrimSpace(scanner.Text())
40-
45+
4146
if matches := phpModuleParser.FindStringSubmatch(line); matches != nil {
4247
directiveType := matches[1]
4348
currentDirective = directiveType
@@ -87,23 +92,23 @@ func (mp *ModuleParser) extractGoFunction(scanner *bufio.Scanner, firstLine stri
8792
return "", "", fmt.Errorf("could not extract function name from line: %s", firstLine)
8893
}
8994
funcName := matches[1]
90-
95+
9196
// Collect the function code
9297
goFunc := firstLine + "\n"
9398
braceCount := 0
94-
99+
95100
// Count opening braces in the first line
96101
for _, char := range firstLine {
97102
if char == '{' {
98103
braceCount++
99104
}
100105
}
101-
106+
102107
// Continue reading until we find the matching closing brace
103108
for braceCount > 0 && scanner.Scan() {
104109
line := scanner.Text()
105110
goFunc += line + "\n"
106-
111+
107112
for _, char := range line {
108113
switch char {
109114
case '{':
@@ -112,11 +117,11 @@ func (mp *ModuleParser) extractGoFunction(scanner *bufio.Scanner, firstLine stri
112117
braceCount--
113118
}
114119
}
115-
120+
116121
if braceCount == 0 {
117122
break
118123
}
119124
}
120-
125+
121126
return funcName, goFunc, nil
122127
}

0 commit comments

Comments
 (0)