use anyhow::Result;
use clap::Parser;
use io_gmail::v1::rest::{
drafts::{GmailDraft, update::GmailDraftUpdate},
messages::{GmailMessage, encode_raw},
};
use pimalaya_cli::printer::Printer;
use crate::{
gmail::{client::GmailClient, drafts::GmailDraftWriteOutput},
shared::message::arg::MessageArg,
};
#[derive(Debug, Parser)]
pub struct GmailDraftUpdateCommand {
#[arg(value_name = "ID")]
pub id: String,
#[arg(long = "thread-id", value_name = "ID")]
pub thread_id: Option<String>,
#[command(flatten)]
pub message: MessageArg,
}
impl GmailDraftUpdateCommand {
pub fn execute(self, printer: &mut impl Printer, client: &mut GmailClient) -> Result<()> {
let raw = self.message.parse()?.into_bytes();
let draft = GmailDraft {
id: self.id.clone(),
message: Some(GmailMessage {
raw: Some(encode_raw(&raw)),
thread_id: self.thread_id.clone(),
..Default::default()
}),
};
let draft = {
let c = GmailDraftUpdate::new(&client.auth, &client.user_id, &draft)?;
client.run(c)?
}
.response;
let (message_id, thread_id) = match draft.message {
Some(message) => (Some(message.id), message.thread_id),
None => (None, None),
};
printer.out(GmailDraftWriteOutput {
id: draft.id,
message_id,
thread_id,
action: "updated",
})
}
}