All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.core.Curl.kt Maven / Gradle / Ivy

There is a newer version: 0.0.87
Show newest version
package pl.mareklangiewicz.kommand.core

import kotlin.math.absoluteValue
import kotlin.random.Random
import okio.Path
import pl.mareklangiewicz.annotations.DelicateApi
import pl.mareklangiewicz.kground.io.*
import pl.mareklangiewicz.kground.*
import pl.mareklangiewicz.kommand.*
import pl.mareklangiewicz.ulog.*

// TODO: Lazy/fast implementation; implement sth solid instead.
@DelicateApi("TODO: implement some versatile version of downloading with curl instead.")
suspend fun curlDownloadTmpFile(
  url: String,
  name: String = "tmp${Random.nextLong().absoluteValue}.txt",
): Path {
  val fs = implictx()
  val dir = fs.pathToSomeTmpOrHome
  val path = dir / name
  fs.createDirectories(dir)
  curlDownload(url, path)
  return path
}


// TODO: Lazy/fast implementation; implement sth solid instead.
@DelicateApi("TODO: implement some versatile version of downloading with curl instead.")
suspend fun curlDownload(url: String, to: Path) {
  val cli = implictx()
  val log = implictx()
  // TODO: Add curl as Kommand, then use it here
  // -s so no progress bars on error stream; -S to report actual errors on error stream
  val k = kommand("curl", "-s", "-S", "-o", to.toString(), url)
  val result = cli.lx(k).waitForResult()
  result.unwrap { err ->
    if (err.isNotEmpty()) {
      log.e("FAIL: Error stream was not empty:")
      err.logEach(log, ULogLevel.ERROR)
      false
    } else true
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy