Skip to content

Commit

Permalink
fix: selfupdate command
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Gammon <[email protected]>
  • Loading branch information
sgammon committed Oct 7, 2023
1 parent 0993c3d commit fb62724
Showing 1 changed file with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import lukfor.progress.TaskService
import lukfor.progress.tasks.DownloadTask
import lukfor.progress.tasks.ITaskRunnable
import lukfor.progress.tasks.TaskFailureStrategy.CANCEL_TASKS
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipFile
import org.apache.commons.io.IOUtils
Expand Down Expand Up @@ -147,7 +149,7 @@ internal class SelfUpdateCommand : AbstractSubcommand<ToolState, CommandContext>
val tag = "$osName-$archName"
val eligibleExtensions = when {
platform.os == WINDOWS -> "zip"
else -> "gz"
else -> "tgz"
}
val asset = release.listAssets().find {
it.name.contains(tag) && eligibleExtensions.any { ext -> it.name.endsWith(ext) }
Expand All @@ -169,17 +171,36 @@ internal class SelfUpdateCommand : AbstractSubcommand<ToolState, CommandContext>
}
}

private fun unpackFile(archive: TarArchiveInputStream, entry: TarArchiveEntry, dest: File) {
if (entry.isDirectory) {
dest.resolve(entry.name).mkdirs()
} else if (archive.canReadEntryData(entry)) {
dest.resolve(entry.name).outputStream().use { out ->
if (archive.canReadEntryData(entry)) {
entry.file.inputStream().use { `in` ->
IOUtils.copy(`in`, out)
}
}
}
}
}

private fun unpackArchive(ext: String, archive: File, dest: File) {
when (ext.trim().lowercase()) {
"zip" -> ZipFile(archive).use {
val entries = it.entries.toList()

entries.stream().forEach { entry ->
it.entries.toList().stream().forEach { entry ->
unpackFile(it, entry, dest)
}
}

"gz" -> GZIPInputStream(archive.inputStream()).use { `in` ->
"tgz" -> GZIPInputStream(archive.inputStream()).use { `in` ->
TarArchiveInputStream(`in`).use {
var entry = it.currentEntry
while (entry != null) {
unpackFile(it, entry, dest)
entry = it.nextTarEntry
}
}
dest.resolve("elide.bin").outputStream().use { out ->
IOUtils.copy(`in`, out)
}
Expand Down

0 comments on commit fb62724

Please sign in to comment.