Skip to content

Commit

Permalink
Merge pull request #256 from zchee/fix-for-loop-defer
Browse files Browse the repository at this point in the history
github: fix defer in for loop
  • Loading branch information
souleb authored Jan 23, 2024
2 parents 13ec9aa + 5d24c20 commit 6e50bf5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions github/client_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package github

import (
"context"
"errors"
"fmt"
"io"

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

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

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

contentStr := string(content)
files = append(files, &gitprovider.CommitFile{
Path: filePath,
Content: &contentStr,
})

}
if errs != nil {
return nil, errs
}

return files, nil
Expand Down

0 comments on commit 6e50bf5

Please sign in to comment.