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 2a780c8..3d973f6 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 @@ -310,6 +310,6 @@ private fun String.sudoizeCommand(): String { /** * Returns path with a trailing fileSeparator if path not empty */ -private fun String.normalizePath(): String { +fun String.normalizePath(): String { return if (this == "" || this.endsWith(fileSeparatorChar())) this else this + fileSeparator() } \ No newline at end of file diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/Web.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/Web.kt index e7adb31..076fcae 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/Web.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/Web.kt @@ -2,41 +2,50 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.web.base import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.ProvResult -import org.domaindrivenarchitecture.provs.framework.core.tags.Api import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.deleteFile +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.fileExists +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.normalizePath import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall /** - * Downloads a file from the given URL using curl. + * Downloads a file from the given URL using curl. Skips download if file already exists and overwrite is false. * * ATTENTION: to check the checksum the locally installed version of sha256sum is used, which can differ in different versions of ubuntu; e.g. gopass download only works with sha256sum version 8.30 from ubuntu 20.04 ! */ -@Api fun Prov.downloadFromURL( url: String, filename: String? = null, path: String? = null, sudo: Boolean = false, followRedirect: Boolean = true, - sha256sum: String? = null -): ProvResult = def { + sha256sum: String? = null, + overwrite: Boolean = false +): ProvResult = task { + + val finalFilename: String = filename ?: url.substringAfterLast("/") + val fqFilename: String = (path?.normalizePath() ?: "") + finalFilename + + if (!overwrite && fileExists(fqFilename, sudo = sudo)) { + return@task ProvResult(true, out = "File $fqFilename already exists.") + } aptInstall("curl") val followRedirectOption = if (followRedirect) "-L" else "" - val filenameFromUrl = url.substringAfterLast("/") - - val finalFilename: String = filename ?: filenameFromUrl path?.let { createDirs(path) } cmd("curl $followRedirectOption $url -o $finalFilename", path, sudo) if (sha256sum != null) { - cmd("sha256sum --version") // log version (e.g. 8.30 for ubuntu 20.04) + cmd("sha256sum --version") // use cmd to log version (e.g. 8.30 for ubuntu 20.04) if (!cmd("echo \"$sha256sum $finalFilename\" | sha256sum --check", path).success) { - cmd("sha256sum $finalFilename", path) // log the actual checksum + + // use cmd to log the actual checksum + cmd("sha256sum $finalFilename", path) + + // delete file with wrong checksum deleteFile(finalFilename, path, sudo) } else { ProvResult(true, out = "Sha256sum is correct.") diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/WebKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/WebKtTest.kt index 8ec65a5..e09cf9e 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/WebKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/web/base/WebKtTest.kt @@ -20,12 +20,17 @@ internal class WebKtTest { // when val res = a.downloadFromURL("file:///tmp/" + file, "file2", "/tmp") + val res2 = a.downloadFromURL("file:///tmp/" + file, "file2", "/tmp") + val res3 = a.downloadFromURL("file:///tmp/" + file, "file2", "/tmp", overwrite = true) // then - val res2 = a.fileContent("/tmp/file2") + val res4 = a.fileContent("/tmp/file2") assertTrue(res.success) - assertEquals("hello", res2) + assertEquals("File /tmp/file2 already exists.", res2.out) + assertTrue(res3.success) + assertEquals(null, res3.out) + assertEquals("hello", res4) } @ContainerTest