diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Go.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Go.kt new file mode 100644 index 0000000..c46fb75 --- /dev/null +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Go.kt @@ -0,0 +1,54 @@ +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.* +import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.isPackageInstalledCheckCommand +import org.domaindrivenarchitecture.provs.framework.ubuntu.web.base.downloadFromURL + +fun Prov.installGo( + //from https://go.dev/dl/ + version: String = "1.23.5", + enforceVersion: Boolean = false, + sha256sum: String = "cbcad4a6482107c7c7926df1608106c189417163428200ce357695cc7e01d091" +) = taskWithResult { + if (isPackageInstalledCheckCommand("go") && !enforceVersion) { + return@taskWithResult ProvResult(true) + } + + if (checkGoVersion(version)) { + return@taskWithResult ProvResult(true, out = "Version $version of go is already installed.") + } + + val downloadUrl = "https://go.dev/dl/go$version.linux-amd64.tar.gz" + val filename = "go$version.linux-amd64.tar.gz" + val target = "${userHome()}tmp" + val result = downloadFromURL( + downloadUrl, + filename, + target, + sha256sum = sha256sum + ) + + if (result.success) { + cmd("tar -C /usr/local -xzf $target/go1.23.5.linux-amd64.tar.gz", sudo = true) + configureBashForUser() + createFile("~/.bashrc.d/go.sh", "export PATH=\$PATH:/usr/local/go/bin\n") + //cmd(". /usr/local/go/bin/go && go version") + // Cross-check if installation was successful + deleteFile("$target/$filename") + return@taskWithResult ProvResult(checkGoVersion(version)) + } else { + return@taskWithResult ProvResult(false, err = "Go $version could not be installed. " + result.err) + } +} + +internal fun Prov.checkGoVersion(version: String): Boolean { + val installedGoVersion = goVersion() + return installedGoVersion != null && installedGoVersion.startsWith("go version go" + version) +} + +internal fun Prov.goVersion(): String? { + val result = cmdNoEval("/usr/local/go/bin/go version") + return if (!result.success) null else result.out +} \ No newline at end of file diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GoKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GoKtTest.kt new file mode 100644 index 0000000..5fe6127 --- /dev/null +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/GoKtTest.kt @@ -0,0 +1,22 @@ +package org.domaindrivenarchitecture.provs.desktop.infrastructure + +import org.domaindrivenarchitecture.provs.framework.core.processors.ContainerStartMode +import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInstall +import org.domaindrivenarchitecture.provs.test.defaultTestContainer +import org.domaindrivenarchitecture.provs.test.tags.ContainerTest +import org.junit.jupiter.api.Assertions.assertTrue + +class GoKtTest { + +@ContainerTest + fun installGo() { + // given + val container = defaultTestContainer() + // when + val res01 = container.installGo() + val res02 = container.installGo() + // then + assertTrue(res01.success) + assertTrue(res02.success) + } +} \ No newline at end of file