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"
|
"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)
|
finished, err := os.Create(outfile)
|
||||||
defer finished.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "CatFiles create: ")
|
return errors.Wrap(err, "CatFiles create: ")
|
||||||
}
|
}
|
||||||
for i := range files {
|
defer finished.Close()
|
||||||
fh, err := os.Open(files[i])
|
for _, file := range files {
|
||||||
|
fh, err := os.Open(file)
|
||||||
if err != nil {
|
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)
|
_, err = io.Copy(finished, fh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "CatFiles copy: ")
|
return errors.Wrap(err, "CatFiles copy: ")
|
||||||
}
|
}
|
||||||
fh.Close()
|
if remove {
|
||||||
if len(remove) > 0 && remove[0] {
|
os.Remove(file)
|
||||||
os.Remove(files[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue