add targetFolderPath to gitClone and createDirs if dirs not yet existing

merge-requests/1/merge
az 2 years ago
parent e644683947
commit ba9b73be43

@ -8,22 +8,33 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.base.trustHost
import java.io.File import java.io.File
fun Prov.gitClone(repo: String, path: String, pullIfExisting: Boolean = true): ProvResult = task { /**
val dir = cmdNoEval("basename $repo .git").out?.trim() * Clones a git repository in the specified targetPath or tries git pull if repo already existing and parameter pullIfExisting is true.
* If specified, the targetFolderName is used as basename for the repo, otherwise the basename (directory) is retrieved from repoSource
if (dir == null) { */
return@task ProvResult(false, err = "$repo is not a valid git repository") fun Prov.gitClone(
} repoSource: String,
targetPath: String = "",
val pathToDir = if (path.endsWith("/")) path + dir else path + "/" + dir pullIfExisting: Boolean = true,
if (checkDir(pathToDir + "/.git/")) { targetFolderName: String? = null
): ProvResult = task {
// if specified, use targetFolderName as basename or otherwise retrieve basename from repoSource
val basename = targetFolderName ?: cmdNoEval("basename $repoSource .git").out?.trim()
// return err if basename could not be retrieved from repoSource
?: return@task ProvResult(false, err = "$repoSource is not a valid git repository source path.")
val pathWithBasename = targetPath.normalizePath() + basename
if (checkDir(pathWithBasename + "/.git/")) {
if (pullIfExisting) { if (pullIfExisting) {
cmd("cd $pathToDir && git pull") cmd("cd $pathWithBasename && git pull")
} else { } else {
ProvResult(true, out = "Repo $repo is already existing") ProvResult(true, out = "Repo [$pathWithBasename] already exists, but might not be up-to-date.")
} }
} else { } else {
cmd("cd $path && git clone $repo") if (!checkDir(targetPath)) {
createDirs(targetPath)
}
cmd("cd $targetPath && git clone $repoSource ${targetFolderName ?: ""}")
} }
} }

@ -1,11 +1,12 @@
package org.domaindrivenarchitecture.provs.framework.ubuntu.git.base package org.domaindrivenarchitecture.provs.framework.ubuntu.git.base
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkDir
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.base.isHostKnown import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.base.isHostKnown
import org.domaindrivenarchitecture.provs.test.defaultTestContainer import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest
import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@ -34,14 +35,31 @@ internal class GitKtTest {
@ExtensiveContainerTest @ExtensiveContainerTest
fun gitClone() { fun gitClone() {
// given // given
val repo = "https://gitlab.com/domaindrivenarchitecture/overview.git"
val prov = defaultTestContainer() val prov = defaultTestContainer()
prov.aptInstall("openssh-client ssh git") prov.aptInstall("git")
// when // when
prov.trustGithub() prov.trustGithub()
val res = prov.gitClone("https://gitlab.com/domaindrivenarchitecture/overview.git", "~/") val res1 = prov.gitClone("https://gitlab.com/domaindrivenarchitecture/not a valid basename.git", "~/")
val res2 = prov.gitClone(repo)
val res3 = prov.gitClone(repo, pullIfExisting = false)
val res4 = prov.gitClone(repo, "pathtocreate")
val res5 = prov.gitClone(repo, "pathtocreate", targetFolderName = "alternativeBasename")
val res6 = prov.gitClone(repo, "pathtocreate", targetFolderName = "alternativeBasename")
val res7 = prov.gitClone(repo, "pathtocreate", false, targetFolderName = "alternativeBasename")
// then // then
assertTrue(res.success) assertFalse(res1.success)
assertTrue(res2.success)
assertTrue(res3.success)
assertEquals("Repo [overview] already exists, but might not be up-to-date.", res3.out)
assertTrue(res4.success)
assertTrue(prov.checkDir("pathtocreate/overview"))
assertTrue(res5.success)
assertTrue(prov.checkDir("pathtocreate/alternativeBasename/.git"))
assertTrue(res6.success)
assertTrue(res7.success)
assertEquals("Repo [pathtocreate/alternativeBasename] already exists, but might not be up-to-date.", res7.out)
} }
} }
Loading…
Cancel
Save