fix: CopyFromFs should not exit on error

This commit is contained in:
Mustafa Gezen 2021-09-07 13:30:50 +02:00
parent 360c4d5849
commit 5d8fdcc702
Signed by: mustafa
GPG Key ID: DCDF010D946438C1
2 changed files with 16 additions and 7 deletions

View File

@ -26,6 +26,7 @@ import (
"crypto/sha256" "crypto/sha256"
"crypto/sha512" "crypto/sha512"
"encoding/hex" "encoding/hex"
"fmt"
"github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5"
"hash" "hash"
"io" "io"
@ -34,10 +35,10 @@ import (
"path/filepath" "path/filepath"
) )
func CopyFromFs(from billy.Filesystem, to billy.Filesystem, path string) { func CopyFromFs(from billy.Filesystem, to billy.Filesystem, path string) error {
read, err := from.ReadDir(path) read, err := from.ReadDir(path)
if err != nil { if err != nil {
log.Fatalf("could not read dir: %v", err) return fmt.Errorf("could not read dir: %v", err)
} }
for _, fi := range read { for _, fi := range read {
@ -45,26 +46,31 @@ func CopyFromFs(from billy.Filesystem, to billy.Filesystem, path string) {
if fi.IsDir() { if fi.IsDir() {
_ = to.MkdirAll(fullPath, 0755) _ = to.MkdirAll(fullPath, 0755)
CopyFromFs(from, to, fullPath) err := CopyFromFs(from, to, fullPath)
if err != nil {
return err
}
} else { } else {
_ = to.Remove(fullPath) _ = to.Remove(fullPath)
f, err := to.OpenFile(fullPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fi.Mode()) f, err := to.OpenFile(fullPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fi.Mode())
if err != nil { if err != nil {
log.Fatalf("could not open file: %v", err) return fmt.Errorf("could not open file: %v", err)
} }
oldFile, err := from.Open(fullPath) oldFile, err := from.Open(fullPath)
if err != nil { if err != nil {
log.Fatalf("could not open from file: %v", err) return fmt.Errorf("could not open from file: %v", err)
} }
_, err = io.Copy(f, oldFile) _, err = io.Copy(f, oldFile)
if err != nil { if err != nil {
log.Fatalf("could not copy from oldFile to new: %v", err) return fmt.Errorf("could not copy from oldFile to new: %v", err)
} }
} }
} }
return nil
} }
func IgnoredContains(a []*IgnoredSource, b string) bool { func IgnoredContains(a []*IgnoredSource, b string) bool {

View File

@ -424,7 +424,10 @@ func ProcessRPM(pd *data.ProcessData) (map[string]string, error) {
return nil, err return nil, err
} }
data.CopyFromFs(md.Worktree.Filesystem, w.Filesystem, ".") err = data.CopyFromFs(md.Worktree.Filesystem, w.Filesystem, ".")
if err != nil {
return nil, err
}
md.Repo = repo md.Repo = repo
md.Worktree = w md.Worktree = w