add parameter overwrite to downloadFromUrl

This commit is contained in:
ansgarz 2022-02-12 17:45:53 +01:00
parent 628008ff61
commit 7f1400fd1f
3 changed files with 27 additions and 13 deletions

View file

@ -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()
}

View file

@ -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.")

View file

@ -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