feature: added FS utils

master
amorozov 2024-06-20 15:06:24 +03:00
parent e89429a93c
commit 72cc77ef77
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package files
import (
"os"
)
func OpenFileForWriting(fileLocation string) (*os.File, error) {
err := MkdirParent(fileLocation)
if err != nil {
return nil, err
}
return os.Create(fileLocation)
}

View File

@ -0,0 +1,21 @@
package files
import (
"os"
"strings"
)
func MkdirParent(fileLocation string) error {
fileLocation = strings.ReplaceAll(fileLocation, "\\", "/")
indexOfSlash := strings.LastIndex(fileLocation, "/")
if indexOfSlash > 0 {
parentDirLocation := fileLocation[:indexOfSlash]
parentDirCreationErr := os.MkdirAll(parentDirLocation, os.ModeDir)
if parentDirCreationErr != nil {
return parentDirCreationErr
}
}
return nil
}