Skip to content

Commit

Permalink
github: Check an error to close io.ReadCloser as well
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
  • Loading branch information
zchee committed Jan 23, 2024
1 parent 017242d commit 5d24c20
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
}
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 5d24c20

Please sign in to comment.