Skip to content

Commit 6e50bf5

Browse files
authored
Merge pull request #256 from zchee/fix-for-loop-defer
github: fix defer in for loop
2 parents 13ec9aa + 5d24c20 commit 6e50bf5

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

github/client_repository_file.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package github
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"io"
2324

@@ -58,6 +59,8 @@ func (c *FileClient) Get(ctx context.Context, path, branch string, optFns ...git
5859

5960
files := make([]*gitprovider.CommitFile, 0)
6061

62+
// For handling to close the output [io.ReadCloser] errors.
63+
var errs error
6164
for _, file := range directoryContent {
6265
filePath := file.Path
6366
output, _, err := c.c.Client().Repositories.DownloadContents(ctx, c.ref.GetIdentity(), c.ref.GetRepository(), *filePath, opts)
@@ -68,14 +71,17 @@ func (c *FileClient) Get(ctx context.Context, path, branch string, optFns ...git
6871
if err != nil {
6972
return nil, err
7073
}
71-
defer output.Close()
74+
// Don't use defer in the for loop. Checks errors lazily.
75+
errs = errors.Join(errs, output.Close())
7276

7377
contentStr := string(content)
7478
files = append(files, &gitprovider.CommitFile{
7579
Path: filePath,
7680
Content: &contentStr,
7781
})
78-
82+
}
83+
if errs != nil {
84+
return nil, errs
7985
}
8086

8187
return files, nil

0 commit comments

Comments
 (0)