yolk/directory/directory.go

48 lines
756 B
Go

/*
* @Date: 2021-03-22 10:45:37
* @LastEditors: viletyy
* @LastEditTime: 2021-06-10 14:25:28
* @FilePath: /yolk/directory/directory.go
*/
package directory
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
}
func IsDir(f string) bool {
fi, err := os.Stat(f)
if err != nil {
return false
}
return fi.IsDir()
}
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 {
return fmt.Errorf("create directory: %v", v)
}
}
}
return err
}