2019-11-07 09:17:42 +02:00
|
|
|
package piperutils
|
|
|
|
|
|
|
|
import (
|
2019-12-05 15:22:38 +02:00
|
|
|
"errors"
|
|
|
|
"io"
|
2019-11-07 09:17:42 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FileExists ...
|
2019-12-11 11:13:23 +02:00
|
|
|
func FileExists(filename string) (bool, error) {
|
2019-11-07 09:17:42 +02:00
|
|
|
info, err := os.Stat(filename)
|
2019-12-11 11:13:23 +02:00
|
|
|
|
2019-11-07 09:17:42 +02:00
|
|
|
if os.IsNotExist(err) {
|
2019-12-11 11:13:23 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-11-07 09:17:42 +02:00
|
|
|
}
|
2019-12-11 11:13:23 +02:00
|
|
|
|
|
|
|
return !info.IsDir(), nil
|
2019-11-07 09:17:42 +02:00
|
|
|
}
|
2019-12-05 15:22:38 +02:00
|
|
|
|
|
|
|
// Copy ...
|
|
|
|
func Copy(src, dst string) (int64, error) {
|
|
|
|
|
2019-12-11 11:13:23 +02:00
|
|
|
exists, err := FileExists(src)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2019-12-11 09:17:16 +02:00
|
|
|
return 0, errors.New("Source file '" + src + "' does not exist")
|
2019-12-05 15:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
source, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
destination, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
nBytes, err := io.Copy(destination, source)
|
|
|
|
return nBytes, err
|
|
|
|
}
|