add function checkDir, make dirExists deprecated

merge-requests/1/merge
ansgarz 2 years ago
parent d778c75937
commit 2cd5ae5c7c

@ -5,7 +5,7 @@ import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.core.getResourceAsText
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.addTextToFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDir
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.dirExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkDir
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import java.io.File
@ -17,7 +17,7 @@ fun Prov.configureBash() = task {
fun Prov.configureBashForUser(): ProvResult = task {
val dirname = "~/.bashrc.d"
if(!dirExists(dirname)) {
if(!checkDir(dirname)) {
createDir(dirname)
cmd("chmod 755 " + dirname)
aptInstall("bash-completion screen")

@ -91,7 +91,7 @@ fun Prov.installKubectlAndTools(): ProvResult = task {
fun Prov.installTerraform(): ProvResult = task {
val dir = "/usr/lib/tfenv/"
if (!dirExists(dir)) {
if (!checkDir(dir)) {
createDirs(dir, sudo = true)
cmd("git clone https://github.com/tfutils/tfenv.git " + dir, sudo = true)
cmd("rm " + dir + ".git/ -rf", sudo = true)
@ -108,7 +108,7 @@ fun Prov.installAwsCredentials(id: String = "REPLACE_WITH_YOUR_ID", key: String
task {
val dir = "~/.aws"
if (!dirExists(dir)) {
if (!checkDir(dir)) {
createDirs(dir)
createFile("~/.aws/config", awsConfig())
createFile("~/.aws/credentials", awsCredentials(id, key))

@ -227,6 +227,14 @@ fun Prov.insertTextInFile(file: String, textBehindWhichToInsert: Regex, textToIn
// ============================= folder operations ==========================
fun Prov.checkDir(dir: String, path: String? = null, sudo: Boolean = false): Boolean {
val effectivePath = if (path != null) path else
(if (dir.startsWith(File.separator)) File.separator else "~" + File.separator)
val cmd = "cd $effectivePath && test -d $dir"
return cmdNoEval(if (sudo) cmd.sudoizeCommand() else cmd).success
}
@Deprecated("Use checkDir instead.", replaceWith = ReplaceWith("checkDir(dir)"))
fun Prov.dirExists(dir: String, path: String? = null, sudo: Boolean = false): Boolean {
val effectivePath = if (path != null) path else
(if (dir.startsWith(File.separator)) File.separator else "~" + File.separator)
@ -241,7 +249,7 @@ fun Prov.createDir(
failIfExisting: Boolean = false,
sudo: Boolean = false
): ProvResult = task {
if (!failIfExisting && dirExists(dir, path, sudo)) {
if (!failIfExisting && checkDir(dir, path, sudo)) {
ProvResult(true)
} else {
val cmd = "cd $path && mkdir $dir"
@ -256,7 +264,7 @@ fun Prov.createDirs(
failIfExisting: Boolean = false,
sudo: Boolean = false
): ProvResult = task {
if (!failIfExisting && dirExists(dirs, path, sudo)) {
if (!failIfExisting && checkDir(dirs, path, sudo)) {
ProvResult(true)
} else {
val cmd = "cd $path && mkdir -p $dirs"

@ -18,7 +18,7 @@ fun Prov.gitClone(repo: String, path: String, pullIfExisting: Boolean = true): P
}
val pathToDir = if (path.endsWith("/")) path + dir else path + "/" + dir
if (dirExists(pathToDir + "/.git/")) {
if (checkDir(pathToDir + "/.git/")) {
if (pullIfExisting) {
cmd("cd $pathToDir && git pull")
} else {

@ -5,7 +5,7 @@ import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDir
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createSecretFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.dirExists
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkDir
import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall
import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.KeyPair
import org.domaindrivenarchitecture.provs.framework.core.echoCommandForText
@ -51,7 +51,7 @@ fun Prov.configureGpgKeys(gpgKeys: KeyPair, trust: Boolean = false, skipIfExisti
private fun Prov.configureGPGAgent() = task {
if (dirExists(".gnupg")) {
if (checkDir(".gnupg")) {
createDir(".gnupg", "~/")
}
val content = """

@ -136,15 +136,15 @@ internal class FilesystemKtTest {
val prov = defaultTestContainer()
// when
val res1 = prov.dirExists("testdir")
val res1 = prov.checkDir("testdir")
val res2 = prov.createDir("testdir", "~/")
val res3 = prov.dirExists("testdir")
val res3 = prov.checkDir("testdir")
val res4 = prov.deleteDir("testdir", "~/")
val res5 = prov.dirExists("testdir")
val res5 = prov.checkDir("testdir")
val res6 = prov.dirExists("testdir", "~/test")
val res6 = prov.checkDir("testdir", "~/test")
val res7 = prov.createDirs("test/testdir")
val res8 = prov.dirExists("testdir", "~/test")
val res8 = prov.checkDir("testdir", "~/test")
prov.deleteDir("testdir", "~/test/")
// then
@ -166,11 +166,11 @@ internal class FilesystemKtTest {
val prov = defaultTestContainer()
// when
val res1 = prov.dirExists("/testdir", sudo = true)
val res1 = prov.checkDir("/testdir", sudo = true)
val res2 = prov.createDir("testdir", "/", sudo = true)
val res3 = prov.dirExists("/testdir", sudo = true)
val res3 = prov.checkDir("/testdir", sudo = true)
val res4 = prov.deleteDir("testdir", "/", true)
val res5 = prov.dirExists("testdir", sudo = true)
val res5 = prov.checkDir("testdir", sudo = true)
// then
assertFalse(res1)

Loading…
Cancel
Save