diff --git a/fileutil/file.go b/fileutil/file.go index 11812a4..9fe7a28 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -13,6 +13,7 @@ import ( "io/ioutil" "net/http" "os" + "path" "path/filepath" "strings" ) @@ -40,6 +41,11 @@ func CreateFile(path string) bool { return true } +// CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/ +func CreateDir(absPath string) error { + return os.MkdirAll(path.Dir(absPath), os.ModePerm) +} + // IsDir checks if the path is directory or not func IsDir(path string) bool { file, err := os.Stat(path) diff --git a/fileutil/file_test.go b/fileutil/file_test.go index b841d21..73b2eb5 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -33,6 +33,27 @@ func TestCreateFile(t *testing.T) { os.Remove(f) } +func TestCreateDir(t *testing.T) { + assert := internal.NewAssert(t, "TestCreateDir") + + pwd, err := os.Getwd() + if err != nil { + t.Error(err) + t.FailNow() + } + + dirPath := pwd + "/a/" + err = CreateDir(dirPath) + if err != nil { + t.Error(err) + t.FailNow() + } + + assert.Equal(true, IsExist(dirPath)) + os.Remove(dirPath) + assert.Equal(false, IsExist(dirPath)) +} + func TestIsDir(t *testing.T) { assert := internal.NewAssert(t, "TestIsDir")