use crate::{gitea::*, *}; use anyhow::{anyhow, Result}; use cli_table::{Cell, Row, Table}; use std::fs::File; use std::io::Write; pub(crate) async fn run(common: Common, fname: Option, tag: String) -> Result<()> { let cli = client(&common)?; let release = get_release_by_tag(&cli, &common.server, &common.owner, &common.repo, &tag).await?; let attachments = get_attachments_for_release( &cli, &common.server, &common.owner, &common.repo, &release.id, ) .await?; match fname { None => { let mut rows: Vec = 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 = 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(()) } } }