2019-11-02 21:32:58 +07:00
|
|
|
package utils
|
2019-10-30 03:54:47 +07:00
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
import (
|
2021-05-05 05:06:05 +07:00
|
|
|
"errors"
|
2019-11-02 21:32:58 +07:00
|
|
|
"os"
|
|
|
|
)
|
2019-10-30 03:54:47 +07:00
|
|
|
|
2021-05-05 05:06:05 +07: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-30 03:54:47 +07:00
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-10-30 03:54:47 +07:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2019-10-30 03:54:47 +07:00
|
|
|
return true, err
|
|
|
|
}
|