add function checkDir, make dirExists deprecated

This commit is contained in:
ansgarz 2022-03-18 22:29:01 +01:00
parent d778c75937
commit 2cd5ae5c7c
6 changed files with 25 additions and 17 deletions

View file

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

View file

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

View file

@ -227,6 +227,14 @@ fun Prov.insertTextInFile(file: String, textBehindWhichToInsert: Regex, textToIn
// ============================= folder operations ========================== // ============================= 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 { fun Prov.dirExists(dir: String, path: String? = null, sudo: Boolean = false): Boolean {
val effectivePath = if (path != null) path else val effectivePath = if (path != null) path else
(if (dir.startsWith(File.separator)) File.separator else "~" + File.separator) (if (dir.startsWith(File.separator)) File.separator else "~" + File.separator)
@ -241,7 +249,7 @@ fun Prov.createDir(
failIfExisting: Boolean = false, failIfExisting: Boolean = false,
sudo: Boolean = false sudo: Boolean = false
): ProvResult = task { ): ProvResult = task {
if (!failIfExisting && dirExists(dir, path, sudo)) { if (!failIfExisting && checkDir(dir, path, sudo)) {
ProvResult(true) ProvResult(true)
} else { } else {
val cmd = "cd $path && mkdir $dir" val cmd = "cd $path && mkdir $dir"
@ -256,7 +264,7 @@ fun Prov.createDirs(
failIfExisting: Boolean = false, failIfExisting: Boolean = false,
sudo: Boolean = false sudo: Boolean = false
): ProvResult = task { ): ProvResult = task {
if (!failIfExisting && dirExists(dirs, path, sudo)) { if (!failIfExisting && checkDir(dirs, path, sudo)) {
ProvResult(true) ProvResult(true)
} else { } else {
val cmd = "cd $path && mkdir -p $dirs" val cmd = "cd $path && mkdir -p $dirs"

View file

@ -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 val pathToDir = if (path.endsWith("/")) path + dir else path + "/" + dir
if (dirExists(pathToDir + "/.git/")) { if (checkDir(pathToDir + "/.git/")) {
if (pullIfExisting) { if (pullIfExisting) {
cmd("cd $pathToDir && git pull") cmd("cd $pathToDir && git pull")
} else { } else {

View file

@ -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.createDir
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile 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.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.install.base.aptInstall
import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.KeyPair import org.domaindrivenarchitecture.provs.framework.ubuntu.keys.KeyPair
import org.domaindrivenarchitecture.provs.framework.core.echoCommandForText 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 { private fun Prov.configureGPGAgent() = task {
if (dirExists(".gnupg")) { if (checkDir(".gnupg")) {
createDir(".gnupg", "~/") createDir(".gnupg", "~/")
} }
val content = """ val content = """

View file

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