trello-to-deck/src/trello/attachment.rs
Paul Campbell 7af4dae96d
Some checks failed
Test / build (map[name:stable]) (push) Successful in 1m51s
Test / build (map[name:nightly]) (push) Successful in 2m4s
Release Please / Release-plz (push) Failing after 20s
refactor: Execute::execute passes itself by ref
2024-12-16 06:41:29 +00:00

65 lines
2 KiB
Rust

use std::path::PathBuf;
//
use clap::Parser;
use color_eyre::Result;
use crate::{execute::Execute, p, FullCtx};
use super::model::{TrelloAttachmentId, TrelloCardId};
#[derive(Parser, Debug)]
pub(crate) 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())
.await?;
p!(ctx.prt, "Wrote: {}", file_name.display());
Ok(())
}
}
}
}