yolk/directory/directory.go

48 lines
756 B
Go
Raw Normal View History

2021-04-26 23:54:51 +08:00
/*
* @Date: 2021-03-22 10:45:37
* @LastEditors: viletyy
2021-06-10 14:29:56 +08:00
* @LastEditTime: 2021-06-10 14:25:28
* @FilePath: /yolk/directory/directory.go
2021-04-26 23:54:51 +08:00
*/
2021-06-10 14:29:56 +08:00
package directory
2021-04-26 23:54:51 +08:00
import (
"fmt"
"os"
)
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
2021-04-30 17:38:27 +08:00
func IsDir(f string) bool {
fi, err := os.Stat(f)
2021-06-10 14:29:56 +08:00
if err != nil {
2021-04-30 17:38:27 +08:00
return false
}
return fi.IsDir()
}
2021-04-26 23:54:51 +08:00
func CreateDir(dirs ...string) (err error) {
for _, v := range dirs {
exist, err := PathExists(v)
if err != nil {
return err
}
if !exist {
err = os.MkdirAll(v, os.ModePerm)
if err != nil {
2021-04-27 16:16:53 +08:00
return fmt.Errorf("create directory: %v", v)
2021-04-26 23:54:51 +08:00
}
}
}
return err
}