use crate::client::ClickUpClient;
use crate::commands::auth::resolve_token;
use crate::error::CliError;
use crate::git;
use crate::output::OutputConfig;
use crate::Cli;
use clap::Subcommand;
#[derive(Subcommand)]
pub enum AttachmentCommands {
Upload {
file: std::path::PathBuf,
#[arg(long)]
task: Option<String>,
},
List {
#[arg(long)]
task: Option<String>,
},
}
const ATTACHMENT_FIELDS: &[&str] = &["id", "title", "url", "date"];
pub async fn execute(command: AttachmentCommands, cli: &Cli) -> Result<(), CliError> {
let token = resolve_token(cli)?;
let client = ClickUpClient::new(&token, cli.timeout)?;
let output = OutputConfig::from_cli(&cli.output, &cli.fields, cli.no_header, cli.quiet);
match command {
AttachmentCommands::Upload { task, file } => {
let task = git::require_task(cli, task.as_deref(), true)?;
let resp = client
.upload_file(&format!("/v2/task/{}/attachment", task.id), &file)
.await?;
output.print_single(&resp, ATTACHMENT_FIELDS, "id");
Ok(())
}
AttachmentCommands::List { task } => {
let task = git::require_task(cli, task.as_deref(), true)?;
let resp = client.get(&format!("/v2/task/{}", task.id)).await?;
let attachments = resp
.get("attachments")
.and_then(|a| a.as_array())
.cloned()
.unwrap_or_default();
output.print_items(&attachments, ATTACHMENT_FIELDS, "id");
Ok(())
}
}
}