trello-to-deck/src/trello/attachment.rs
Paul Campbell 0023e2aa70
Some checks failed
Test / build (map[name:nightly]) (push) Successful in 3m30s
Test / build (map[name:stable]) (push) Successful in 3m30s
Release Please / Release-plz (push) Failing after 43s
feat: write downloaded attachments to temp directory
2024-12-23 09:42:52 +00:00

70 lines
2.1 KiB
Rust

use std::path::PathBuf;
//
use clap::Parser;
use color_eyre::Result;
use crate::{execute::Execute, p, FullCtx};
use super::model::{attachment::TrelloAttachmentId, card::TrelloCardId};
#[derive(Parser, Debug)]
pub enum TrelloAttachmentCommand {
Get {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
card_id: String,
attachment_id: String,
},
Save {
card_id: String,
attachment_id: String,
file_name: Option<PathBuf>, // will use file name from attachment if not provided.
},
}
impl Execute for TrelloAttachmentCommand {
async fn execute(&self, ctx: &FullCtx) -> Result<()> {
match self {
Self::Get {
dump,
card_id,
attachment_id,
} => {
let trello_card_id = TrelloCardId::new(card_id);
let trello_attachment_id = TrelloAttachmentId::new(attachment_id);
let api_result = ctx
.trello_client()
.card_attachment(&trello_card_id, &trello_attachment_id)
.await;
if *dump {
p!(ctx.prt, "{}", api_result.text);
} else {
let attachment = api_result.result?;
p!(ctx.prt, "{}:{}", attachment.name, attachment.url);
}
Ok(())
}
Self::Save {
card_id,
attachment_id,
file_name,
} => {
let trello_card_id = TrelloCardId::new(card_id);
let trello_attachment_id = TrelloAttachmentId::new(attachment_id);
let file_name = ctx
.trello_client()
.save_attachment(
&trello_card_id,
&trello_attachment_id,
file_name.as_ref(),
&ctx.fs,
)
.await?;
p!(ctx.prt, "Wrote: {}", file_name.display());
Ok(())
}
}
}
}