33 lines
606 B
Go
33 lines
606 B
Go
|
package fs
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
// IsDir Tells whether the filename is a directory
|
||
|
func IsDir(filename string) (bool, error) {
|
||
|
fd, err := os.Stat(filename)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
fm := fd.Mode()
|
||
|
return fm.IsDir(), nil
|
||
|
}
|
||
|
|
||
|
// DirectoryOrCreate checking directory, is not exists will create
|
||
|
func DirectoryOrCreate(dirname string) error {
|
||
|
if fi, err := os.Stat(dirname); err != nil {
|
||
|
if errors.Is(err, os.ErrNotExist) {
|
||
|
return os.MkdirAll(dirname, 0755)
|
||
|
} else {
|
||
|
return err
|
||
|
}
|
||
|
} else {
|
||
|
if fi.IsDir() {
|
||
|
return nil
|
||
|
}
|
||
|
return errors.New("file not directory")
|
||
|
}
|
||
|
}
|