Skip to main content

systemprompt_cli/commands/core/content/files/
featured.rs

1use anyhow::Result;
2use clap::Args;
3use systemprompt_database::DbPool;
4use systemprompt_files::FileRepository;
5use systemprompt_identifiers::{ContentId, FileId};
6use systemprompt_runtime::AppContext;
7
8use crate::CliConfig;
9use crate::commands::core::files::types::{FeaturedImageOutput, FileSummary};
10use crate::shared::CommandOutput;
11
12#[derive(Debug, Clone, Args)]
13pub struct FeaturedArgs {
14    #[arg(value_name = "CONTENT_ID", help = "Content ID")]
15    pub content: String,
16
17    #[arg(long, help = "Set featured image")]
18    pub set: Option<String>,
19}
20
21pub(super) async fn execute(args: FeaturedArgs, config: &CliConfig) -> Result<CommandOutput> {
22    let ctx = AppContext::new().await?;
23    execute_with_pool(args, ctx.db_pool(), config).await
24}
25
26pub async fn execute_with_pool(
27    args: FeaturedArgs,
28    pool: &DbPool,
29    _config: &CliConfig,
30) -> Result<CommandOutput> {
31    let service = FileRepository::new(pool)?;
32
33    let content_id = ContentId::new(args.content.clone());
34
35    if let Some(file_id_str) = args.set {
36        let file_id = FileId::new(file_id_str);
37        service.set_featured(&file_id, &content_id).await?;
38
39        let output = FeaturedImageOutput {
40            content_id,
41            file: None,
42            message: "Featured image set successfully".to_owned(),
43        };
44
45        return Ok(CommandOutput::card_value("Featured Image Set", &output));
46    }
47
48    let file = service.find_featured_image(&content_id).await?;
49
50    let file_summary = file.map(|f| FileSummary {
51        id: FileId::new(f.id.to_string()),
52        path: f.path,
53        public_url: f.public_url,
54        mime_type: f.mime_type,
55        size_bytes: f.size_bytes,
56        ai_content: f.ai_content,
57        created_at: f.created_at,
58    });
59
60    let message = file_summary.as_ref().map_or_else(
61        || "No featured image set".to_owned(),
62        |f| format!("Featured image: {}", f.path),
63    );
64
65    let output = FeaturedImageOutput {
66        content_id,
67        file: file_summary,
68        message,
69    };
70
71    Ok(CommandOutput::card_value("Featured Image", &output))
72}