Skip to content

Commit

Permalink
Update documentation, make CLI nicer
Browse files Browse the repository at this point in the history
Signed-off-by: Kozova1 <mug66kk@gmail.com>
  • Loading branch information
Kozova1 committed Jul 10, 2021
1 parent a5f85b7 commit a50f37b
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 43 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "net.vogman"
version = "4.0"
version = "4.1"

repositories {
mavenCentral()
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/net/vogman/mcdeploy/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ data class ServerConfig(
)

data class ExternalFile(val URL: URL, val FileName: String, val Sha1Sum: String) {
suspend fun fetchWrite(client: HttpClient, targetPath: Path, progressBar: ProgressBar): Either<Error, Unit> {
suspend fun fetchWrite(targetPath: Path, progressBar: ProgressBar): Either<Error, Unit> = progressBar.use { pbar ->
val file = targetPath.resolve(FileName)
when (val cnfRes = Either.catch { file.createFile() }.mapLeft {
Error.FilesystemError(it.localizedMessage)
Expand All @@ -63,8 +63,8 @@ data class ExternalFile(val URL: URL, val FileName: String, val Sha1Sum: String)
HttpClient().use { client ->
val res: HttpResponse = client.get(URL) {
onDownload { bytesSentTotal, contentLength ->
progressBar.maxHint(contentLength / 1024)
progressBar.stepTo(bytesSentTotal / 1024)
pbar.maxHint(contentLength)
pbar.stepTo(bytesSentTotal)
}
}
res.receive()
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/net/vogman/mcdeploy/DeployServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ object DeployServer : Command {
return Either.Left(Error.AlreadyExists(serverJarFile))
}

println("Deploying server...")

val serverJar = when (val res = config.Server.JarSource.fetch(config)) {
is Either.Left -> return res.map { }
is Either.Right -> res.value
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/net/vogman/mcdeploy/DownloadURL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import arrow.core.Either
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.features.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlin.io.path.Path

Expand All @@ -13,7 +12,7 @@ suspend fun ServerJarFetcher.DownloadURL.fetchImpl(config: Config): Either<Error
HttpClient().use { client ->
assert(config.Server.JarSource is ServerJarFetcher.DownloadURL)
println("Downloading server.jar from $ServerJarURL")
val response: HttpResponse = client.downloadWithProgressBar(ServerJarURL, "server.jar")
val response: HttpResponse = client.downloadWithProgressBar(ServerJarURL, "server.jar", "MiB", 1024 * 1024)
val serverJar: ByteArray = response.receive()

val actualHash = sha1sum(serverJar)
Expand Down
14 changes: 6 additions & 8 deletions src/main/kotlin/net/vogman/mcdeploy/LauncherManifest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import arrow.core.Either
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.features.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import me.tongfei.progressbar.ProgressBarBuilder
import me.tongfei.progressbar.ProgressBarStyle
import java.net.URL
import java.time.temporal.ChronoUnit
import kotlin.io.path.Path

suspend fun ServerJarFetcher.LauncherManifest.fetchImpl(config: Config): Either<Error, ByteArray> =
try {
HttpClient().use { client ->
assert(config.Server.JarSource is ServerJarFetcher.LauncherManifest)
println("Downloading launcher manifest...")
val versionResponse: HttpResponse = client.downloadWithProgressBar(LauncherManifestURL, "Launcher Manifest")
val versionResponse: HttpResponse =
client.downloadWithProgressBar(LauncherManifestURL, "Launcher Manifest", "KiB", 1024)

val versions: Versions = Json { ignoreUnknownKeys = true }.decodeFromString(versionResponse.receive())
val versionUrl = versions.findURI(
Expand All @@ -36,7 +32,8 @@ suspend fun ServerJarFetcher.LauncherManifest.fetchImpl(config: Config): Either<


println("Downloading manifest for selected version...")
val responseManifest: HttpResponse = client.downloadWithProgressBar(versionUrl, "Version Manifest")
val responseManifest: HttpResponse =
client.downloadWithProgressBar(versionUrl, "Version Manifest", "KiB", 1024)
val manifest: VersionManifest =
Json { ignoreUnknownKeys = true }.decodeFromString(responseManifest.receive())
logOk("Received manifest for version $Version")
Expand All @@ -47,7 +44,8 @@ suspend fun ServerJarFetcher.LauncherManifest.fetchImpl(config: Config): Either<

// Download server.jar for the selected version
println("Downloading server.jar...")
val serverResponse: HttpResponse = client.downloadWithProgressBar(manifest.downloads.server.url, "server.jar")
val serverResponse: HttpResponse =
client.downloadWithProgressBar(manifest.downloads.server.url, "server.jar", "MiB", 1024 * 1024)
val serverJar: ByteArray = serverResponse.receive()
logOk("Downloaded server.jar")
println("Verifying server.jar...")
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/net/vogman/mcdeploy/UpdateServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object UpdateServer : Command {
val response = readLine() ?: continue
when (response) {
"y", "Y" -> {
logOk("Starting server update...")
logOk("Updating server...")
break
}
"n", "N" -> {
Expand Down
39 changes: 16 additions & 23 deletions src/main/kotlin/net/vogman/mcdeploy/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.vogman.mcdeploy

import arrow.core.Either
import arrow.core.flatMap
import arrow.core.flatten
import io.ktor.client.*
import io.ktor.client.features.*
import io.ktor.client.request.*
Expand Down Expand Up @@ -39,15 +37,17 @@ fun File.writeNewText(text: String, charset: Charset): Either<Error, Unit> {
}

fun logErr(msg: String) {
val colorReset = "\u001B[0m"
val reset = "\u001B[0m"
val bold = "\u001B[1m"
val colorRed = "\u001B[31m"
println("[${colorRed}X${colorReset}] $msg")
System.err.println("[${colorRed}${bold}X${reset}]${colorRed} $msg${reset}")
}

fun logOk(msg: String) {
val colorReset = "\u001B[0m"
val reset = "\u001B[0m"
val bold = "\u001B[1m"
val colorGreen = "\u001B[32m"
println("[${colorGreen}${colorReset}] $msg")
println("[${colorGreen}${bold}${reset}]${colorGreen} $msg${reset}")
}

fun sha1sum(bytes: ByteArray): String {
Expand Down Expand Up @@ -80,7 +80,7 @@ suspend fun List<ExternalFile>.fetchAll(targetDir: Path, overwrite: Boolean = fa
ProgressBarBuilder()
.setStyle(ProgressBarStyle.ASCII)
.setInitialMax(0)
.setUnit("KiB", 1)
.setUnit("KiB", 1024)
.setSpeedUnit(ChronoUnit.SECONDS)
.showSpeed()
.setTaskName(it.FileName)
Expand All @@ -89,14 +89,7 @@ suspend fun List<ExternalFile>.fetchAll(targetDir: Path, overwrite: Boolean = fa

val resultList: List<Either<Error, Unit>> =
this.zip(progressBars).parMap { (it, progressBar) ->
Either.catch {
HttpClient().use { client ->
it.fetchWrite(client, targetDir, progressBar)
}
}.mapLeft {
val status = (it as ClientRequestException).response.status
Error.RequestFailed("${status.value} (${status.description})")
}.flatten()
it.fetchWrite(targetDir, progressBar)
}

return resultList.fold(Either.Right(Unit) as Either<Error, Unit>) { folded, toFold ->
Expand All @@ -110,38 +103,38 @@ suspend fun List<ExternalFile>.fetchAll(targetDir: Path, overwrite: Boolean = fa
}


suspend fun HttpClient.downloadWithProgressBar(url: String, progressBarName: String): HttpResponse {
suspend fun HttpClient.downloadWithProgressBar(url: String, progressBarName: String, unitName: String, unitSize: Long): HttpResponse {
return ProgressBarBuilder()
.setStyle(ProgressBarStyle.ASCII)
.setInitialMax(0)
.setUnit("KiB", 1)
.setUnit(unitName, unitSize)
.setSpeedUnit(ChronoUnit.SECONDS)
.showSpeed()
.setTaskName(progressBarName)
.build().use { pbar ->
get(url) {
onDownload { bytesSentTotal, contentLength ->
pbar.maxHint(contentLength / 1024)
pbar.stepTo(bytesSentTotal / 1024)
pbar.maxHint(contentLength)
pbar.stepTo(bytesSentTotal)
}
}
}
}


suspend fun HttpClient.downloadWithProgressBar(url: URL, progressBarName: String): HttpResponse =
suspend fun HttpClient.downloadWithProgressBar(url: URL, progressBarName: String, unitName: String, unitSize: Long): HttpResponse =
ProgressBarBuilder()
.setStyle(ProgressBarStyle.ASCII)
.setInitialMax(0)
.setUnit("KiB", 1)
.setUnit(unitName, unitSize)
.setSpeedUnit(ChronoUnit.SECONDS)
.showSpeed()
.setTaskName(progressBarName)
.build().use { pbar ->
get(url) {
onDownload { bytesSentTotal, contentLength ->
pbar.maxHint(contentLength / 1024)
pbar.stepTo(bytesSentTotal / 1024)
pbar.maxHint(contentLength)
pbar.stepTo(bytesSentTotal)
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/net/vogman/mcdeploy/VersionPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.vogman.mcdeploy
import arrow.core.Either

object VersionPrinter : Command{
const val VERSION: String = "v4.0"
const val VERSION: String = "v4.1"
override suspend fun run(args: Array<String>): Either<Error, Unit> {
logOk("Version: $VERSION")
return Either.Right(Unit)
Expand Down
5 changes: 1 addition & 4 deletions src/main/resources/Help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@ CONFIG

RETURN VALUE
0 - Success
1 - User error
2 - Server error
4 - Hash mismatch
8 - Filesystem error
1 - Error encountered
Other - Java / Kotlin error, please submit bug report

0 comments on commit a50f37b

Please sign in to comment.