From 2cd5ae5c7cfec3e838f147297cb8c20e46f3ae5e Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 18 Mar 2022 22:29:01 +0100 Subject: [PATCH] add function checkDir, make dirExists deprecated --- .../provs/desktop/infrastructure/Bash.kt | 4 ++-- .../provs/desktop/infrastructure/DevOps.kt | 4 ++-- .../ubuntu/filesystem/base/Filesystem.kt | 12 ++++++++++-- .../provs/framework/ubuntu/git/base/Git.kt | 2 +- .../provs/framework/ubuntu/keys/base/Gpg.kt | 4 ++-- .../ubuntu/filesystem/base/FilesystemKtTest.kt | 16 ++++++++-------- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Bash.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Bash.kt index 98dcaab..468b996 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Bash.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Bash.kt @@ -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") diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt index 13949cc..74c663d 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/DevOps.kt @@ -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)) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt index 34f9731..ac83e85 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt @@ -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" diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/git/base/Git.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/git/base/Git.kt index 89b3381..1ec684e 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/git/base/Git.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/git/base/Git.kt @@ -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 { diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/Gpg.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/Gpg.kt index a147967..ed59b31 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/Gpg.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/keys/base/Gpg.kt @@ -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 = """ diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt index 123e116..71da9b8 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt @@ -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)