use std::path::PathBuf;
use anyhow::Result;
use cleanlib_client::{config, transport};
use tokio::io::AsyncWriteExt;
pub async fn run(
ecosystem: String,
package: String,
version: String,
output: Option<PathBuf>,
) -> Result<()> {
let path = config::default_path();
let cfg = config::load_with_env_overrides(path.as_deref())?;
let client = transport::Client::from_config(&cfg)?;
match output {
Some(p) => {
if let Some(parent) = p.parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
let file = tokio::fs::File::create(&p).await?;
let mut writer = tokio::io::BufWriter::new(file);
let total = client
.fetch_artifact_stream(&ecosystem, &package, &version, &mut writer)
.await?;
writer.flush().await?;
eprintln!("wrote {} ({} bytes)", p.display(), total);
}
None => {
let stdout = tokio::io::stdout();
let mut writer = tokio::io::BufWriter::new(stdout);
client
.fetch_artifact_stream(&ecosystem, &package, &version, &mut writer)
.await?;
writer.flush().await?;
}
}
Ok(())
}