mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 13:21:00 +02:00
refactored CatFiles function
This commit is contained in:
parent
979162ec94
commit
d94cc6f1d3
1 changed files with 10 additions and 9 deletions
19
utils.go
19
utils.go
|
@ -11,25 +11,26 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func CatFiles(files []string, outfile string, remove ...bool) error {
|
||||
// CatFiles copies data from n files to a single one and removes source files
|
||||
// if Debug mode is set to false
|
||||
func CatFiles(files []string, outfile string, remove bool) error {
|
||||
finished, err := os.Create(outfile)
|
||||
defer finished.Close()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "CatFiles create: ")
|
||||
}
|
||||
for i := range files {
|
||||
fh, err := os.Open(files[i])
|
||||
defer finished.Close()
|
||||
for _, file := range files {
|
||||
fh, err := os.Open(file)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "CatFiles open "+files[i]+": ")
|
||||
return errors.Wrap(err, fmt.Sprintf("CatFiles open %v: ", file))
|
||||
}
|
||||
|
||||
defer fh.Close()
|
||||
_, err = io.Copy(finished, fh)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "CatFiles copy: ")
|
||||
}
|
||||
fh.Close()
|
||||
if len(remove) > 0 && remove[0] {
|
||||
os.Remove(files[i])
|
||||
if remove {
|
||||
os.Remove(file)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue