diff --git a/pkg/io/files/file_open_helper.go b/pkg/io/files/file_open_helper.go new file mode 100644 index 0000000..8bd95d4 --- /dev/null +++ b/pkg/io/files/file_open_helper.go @@ -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) +} diff --git a/pkg/io/files/parent_file_creator.go b/pkg/io/files/parent_file_creator.go new file mode 100644 index 0000000..835863e --- /dev/null +++ b/pkg/io/files/parent_file_creator.go @@ -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 +}