gitea-release/src/cmd/download.rs

60 lines
1.7 KiB
Rust
Raw Normal View History

2020-05-30 18:27:49 +00:00
use crate::{gitea::*, *};
use anyhow::{anyhow, Result};
use cli_table::{Cell, Row, Table};
use std::fs::File;
use std::io::Write;
2020-05-30 18:50:21 +00:00
pub(crate) async fn run(common: Common, fname: Option<PathBuf>, tag: String) -> Result<()> {
2020-05-30 18:27:49 +00:00
let cli = client(&common)?;
2020-05-30 18:50:21 +00:00
let release =
get_release_by_tag(&cli, &common.server, &common.owner, &common.repo, &tag).await?;
2020-05-30 18:27:49 +00:00
let attachments = get_attachments_for_release(
&cli,
&common.server,
&common.owner,
&common.repo,
&release.id,
)
.await?;
match fname {
None => {
let mut rows: Vec<Row> = vec![Row::new(vec![
Cell::new(&"name", Default::default()),
Cell::new(&"size", Default::default()),
Cell::new(&"url", Default::default()),
])];
for attachment in attachments {
rows.push(attachment.row())
}
let table = Table::new(rows, Default::default())?;
table.print_stdout()?;
Ok(())
}
Some(fname) => {
let mut url: Option<String> = None;
let fname = fname.into_os_string().into_string().unwrap();
for attachment in attachments {
if &fname == &attachment.name {
url = Some(attachment.browser_download_url);
}
}
if url.is_none() {
return Err(anyhow!("no attachment named {}", fname));
}
let data = &cli.get(url.unwrap().as_str()).send().await?.bytes().await?;
let mut fout = File::create(&fname)?;
fout.write(data)?;
Ok(())
}
}
}