From 494e1bd8d612747870ab05e15e7f617d906c0a16 Mon Sep 17 00:00:00 2001 From: patdyn Date: Thu, 30 May 2024 13:20:23 +0200 Subject: [PATCH] Finish hugo install and test --- .../provs/desktop/infrastructure/Hugo.kt | 88 +++++++++++++++++++ .../provs/desktop/infrastructure/HugoTest.kt | 40 +++++++++ 2 files changed, 128 insertions(+) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Hugo.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Hugo.kt index bf709aa..2b26c1b 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Hugo.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Hugo.kt @@ -1,2 +1,90 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure +import org.domaindrivenarchitecture.provs.framework.core.Prov +import org.domaindrivenarchitecture.provs.framework.core.ProvResult +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDir +import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.userHome +import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall +import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptPurge +import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFromURL + +fun Prov.installHugoByDeb() = taskWithResult { + val sha256sum = "46692ac9b79d5bc01b0f847f6dcf651d8630476de63e598ef61a8da9461d45cd" + val requiredHugoVersion = "0.125.5" + val filename = "hugo_extended_0.125.5_linux-amd64.deb" + val downloadUrl = "-L https://github.com/gohugoio/hugo/releases/download/v$requiredHugoVersion/$filename" + val downloadDir = "${userHome()}Downloads" + val currentHugoVersion = cmdNoEval("hugo version").out + + if (needsHugoInstall(currentHugoVersion, requiredHugoVersion)) { + if (isHugoInstalled(currentHugoVersion)) { + if (currentHugoVersion!!.contains("snap")) { + cmd("snap remove hugo", sudo = true) + } else { + aptPurge("hugo") + } + } + aptInstall("gnupg2") + createDir(downloadDir) + downloadFromURL(downloadUrl, filename, downloadDir, sha256sum = sha256sum) + cmd("dpkg -i $downloadDir/$filename", sudo = true) + } else { + ProvResult(true) + } +} + +fun needsHugoInstall(currentHugoVersion: String?, requiredHugoVersion: String) : Boolean { + if (currentHugoVersion == null) { + return true + } + if (!isHugoInstalled(currentHugoVersion)) { + return true + } + if (!isHugoExtended(currentHugoVersion)) { + return true + } + if (isLowerHugoVersion(requiredHugoVersion, currentHugoVersion)) { + return true + } + return false +} + +fun isHugoInstalled(hugoVersion: String?) : Boolean { + if (hugoVersion == null) { + return false + } + return hugoVersion.contains("hugo") +} + +fun isHugoExtended(hugoVersion: String) : Boolean { + return hugoVersion.contains("extended") +} + +fun isLowerHugoVersion(requiredHugoVersion: String, currentHugoVersion: String ) : Boolean { + val reqVersionNo = getHugoVersionNo(requiredHugoVersion) + val currentVersionNo = getHugoVersionNo(currentHugoVersion) + return when { + compareVersions(currentVersionNo, reqVersionNo).contains("lower") -> true + else -> false + } +} + +fun compareVersions(firstVersion : List, secondVersion: List) : String { + var result = "" + for (i in 0..2) { + when { + firstVersion[i] > secondVersion[i] -> result += " higher" + firstVersion[i] < secondVersion[i] -> result += " lower" + firstVersion[i] == secondVersion[i] -> result += " equal" + } + } + return result +} + +fun getHugoVersionNo(hugoVersion: String) : List { + // hugo v0.126.1-3d40ab+extended linux/amd64 BuildDate=2024-05-15T10:42:34Z VendorInfo=snap:0.126.1 + var result = hugoVersion.split(" ")[1] + result = result.split("-")[0].removePrefix("v") + return result.split(".").map { it.toInt() } +} + diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/HugoTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/HugoTest.kt index bf709aa..0405f50 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/HugoTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/HugoTest.kt @@ -1,2 +1,42 @@ package org.domaindrivenarchitecture.provs.desktop.infrastructure +import org.domaindrivenarchitecture.provs.framework.core.docker.exitAndRmContainer +import org.domaindrivenarchitecture.provs.framework.core.local +import org.domaindrivenarchitecture.provs.test.defaultTestContainer +import org.domaindrivenarchitecture.provs.test.tags.ExtensiveContainerTest +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test + +internal class HugoTest { + @ExtensiveContainerTest + fun test_installHugoByDeb() { + // given + local().exitAndRmContainer("provs_test") + val prov = defaultTestContainer() + + // when + val res = prov.installHugoByDeb() + + // then + assertTrue(res.success) + } + + @Test + fun test_needsHugoInstall() { + // given + val hugoNull = null + val hugoLow = "hugo v0.0.0-abc+extended linux/amd64 BuildDate=0000-00-00 VendorInfo=snap:0.0.0" + val hugoMajHigh = "hugo v1.0.0-abc+extended linux/amd64 BuildDate=0000-00-00 VendorInfo=snap:1.0.0" + val hugoMinHigh = "hugo v0.1.0-abc+extended linux/amd64 BuildDate=0000-00-00 VendorInfo=snap:0.1.0" + val hugoPatHigh = "hugo v0.0.1-abc+extended linux/amd64 BuildDate=0000-00-00 VendorInfo=snap:0.0.1" + + assertTrue(needsHugoInstall(hugoNull, hugoPatHigh)) + assertTrue(needsHugoInstall(hugoLow, hugoPatHigh)) + assertTrue(needsHugoInstall(hugoLow, hugoMinHigh)) + assertTrue(needsHugoInstall(hugoLow, hugoMajHigh)) + assertFalse(needsHugoInstall(hugoMajHigh, hugoLow)) + assertFalse(needsHugoInstall(hugoMinHigh, hugoLow)) + assertFalse(needsHugoInstall(hugoPatHigh, hugoLow)) + } +} \ No newline at end of file