worklog/internal/tools/filesystem.go
2023-01-27 17:11:22 +01:00

44 lines
736 B
Go

package tools
import (
"errors"
"os"
"path"
"path/filepath"
)
// exported function(s)
func MkDirs(destination string) error {
if len(destination) == 0 {
return nil
}
destination = filepath.Clean(destination)
stats, error := os.Stat(destination)
if error != nil && !errors.Is(error, os.ErrNotExist) {
return error
}
if stats != nil && stats.IsDir() {
return nil
}
return os.MkdirAll(destination, os.ModePerm)
}
func CreateFile(file string) error {
if Exists(file) {
return nil
}
error := MkDirs(path.Dir(file))
if error != nil {
return error
}
_, error = os.Create(file)
if error != nil {
return error
}
return nil
}
func Exists(path string) bool {
_, error := os.Stat(path)
return error == nil
}