refactor: use os.ReadFile instead of os.Open + io.ReadAll (#1254)

This commit is contained in:
Oleksandr Redko authored and GitHub committed 2023-08-23 16:08:38 -05:00
1 parent 163ec21120
commit 963a6976d1
1 file changed
+2 -16
+2 -16
View File
@@ -3,11 +3,8 @@ package detect
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"os" "os"
"github.com/rs/zerolog/log"
"github.com/zricethezav/gitleaks/v8/report" "github.com/zricethezav/gitleaks/v8/report"
) )
@@ -39,23 +36,12 @@ func IsNew(finding report.Finding, baseline []report.Finding) bool {
} }
func LoadBaseline(baselinePath string) ([]report.Finding, error) { func LoadBaseline(baselinePath string) ([]report.Finding, error) {
var previousFindings []report.Finding bytes, err := os.ReadFile(baselinePath)
jsonFile, err := os.Open(baselinePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not open %s", baselinePath) return nil, fmt.Errorf("could not open %s", baselinePath)
} }
defer func() { var previousFindings []report.Finding
if cerr := jsonFile.Close(); cerr != nil {
log.Warn().Err(cerr).Msg("problem closing jsonFile handle")
}
}()
bytes, err := io.ReadAll(jsonFile)
if err != nil {
return nil, fmt.Errorf("could not read data from the file %s", baselinePath)
}
err = json.Unmarshal(bytes, &previousFindings) err = json.Unmarshal(bytes, &previousFindings)
if err != nil { if err != nil {
return nil, fmt.Errorf("the format of the file %s is not supported", baselinePath) return nil, fmt.Errorf("the format of the file %s is not supported", baselinePath)