2019-11-02 14:32:58 +00:00
|
|
|
package utils
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
import (
|
2021-05-04 22:06:05 +00:00
|
|
|
"errors"
|
2019-11-02 14:32:58 +00:00
|
|
|
"os"
|
|
|
|
)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2021-05-04 22:06:05 +00:00
|
|
|
// FileExists returns true if the given path exists and is a file.
|
|
|
|
func FileExists(path string) (exists bool, err error) {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
if info.IsDir() {
|
|
|
|
return false, errors.New("path is a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DirectoryExists returns true if the given path exists and is a directory.
|
|
|
|
func DirectoryExists(path string) (exists bool, err error) {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
if info.IsDir() {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errors.New("path is a file")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PathExists returns true if the given path exists.
|
|
|
|
func PathExists(path string) (exists bool, err error) {
|
|
|
|
_, err = os.Stat(path)
|
2019-10-29 20:54:47 +00:00
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
return true, err
|
|
|
|
}
|