From 6019beffe53bb049753fa5111d6ea42f771965e9 Mon Sep 17 00:00:00 2001 From: amorozov Date: Fri, 21 Jun 2024 18:39:35 +0300 Subject: [PATCH] feature: added 'GetParentDir' to files --- pkg/io/files/file_exists_checker.go | 11 +++++++++++ pkg/io/files/parent_file_creator.go | 13 ++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pkg/io/files/file_exists_checker.go b/pkg/io/files/file_exists_checker.go index 1629370..be92e3d 100644 --- a/pkg/io/files/file_exists_checker.go +++ b/pkg/io/files/file_exists_checker.go @@ -3,6 +3,7 @@ package files import ( "errors" "os" + "strings" ) func IsFileExists(fileLocation string) bool { @@ -12,3 +13,13 @@ func IsFileExists(fileLocation string) bool { return true } + +func GetParentDir(fileLocation string) string { + fileLocation = strings.ReplaceAll(fileLocation, "\\", "/") + indexOfSlash := strings.LastIndex(fileLocation, "/") + if indexOfSlash > 0 { + return fileLocation[:indexOfSlash] + } + + return "" +} diff --git a/pkg/io/files/parent_file_creator.go b/pkg/io/files/parent_file_creator.go index 835863e..73a86c6 100644 --- a/pkg/io/files/parent_file_creator.go +++ b/pkg/io/files/parent_file_creator.go @@ -1,16 +1,11 @@ package files -import ( - "os" - "strings" -) +import "os" 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) + parentDir := GetParentDir(fileLocation) + if parentDir != "" { + parentDirCreationErr := os.MkdirAll(parentDir, os.ModeDir) if parentDirCreationErr != nil { return parentDirCreationErr