From 18f289fe90bcf2437aa92b253e2170bc2323162f Mon Sep 17 00:00:00 2001 From: amorozov Date: Tue, 25 Jun 2024 15:45:57 +0300 Subject: [PATCH] feature: added GetFirstIndexedNonExistingFilePath to files --- pkg/io/files/file_exists_checker.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/io/files/file_exists_checker.go b/pkg/io/files/file_exists_checker.go index 1629370..4f7a131 100644 --- a/pkg/io/files/file_exists_checker.go +++ b/pkg/io/files/file_exists_checker.go @@ -2,7 +2,9 @@ package files import ( "errors" + "fmt" "os" + "path/filepath" ) func IsFileExists(fileLocation string) bool { @@ -12,3 +14,19 @@ func IsFileExists(fileLocation string) bool { return true } + +func GetFirstIndexedNonExistingFilePath(parentDir string, fileBaseName string) string { + filePath := filepath.Join(parentDir, fileBaseName) + index := 0 + for { + if !IsFileExists(filePath) { + break + } + + index++ + + filePath = filepath.Join(parentDir, fmt.Sprintf("%s-%d", fileBaseName, index)) + } + + return filePath +}