Skip to main content

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

1use anyhow::Result;
2use clap::{Args, ValueEnum};
3use systemprompt_database::DbPool;
4use systemprompt_files::{FileRepository, FileRole};
5use systemprompt_identifiers::{ContentId, FileId};
6use systemprompt_runtime::AppContext;
7
8use crate::CliConfig;
9use crate::commands::core::files::types::ContentLinkOutput;
10use crate::shared::CommandOutput;
11
12#[derive(Debug, Clone, Copy, ValueEnum)]
13pub enum FileRoleArg {
14    Featured,
15    Attachment,
16    Inline,
17    OgImage,
18    Thumbnail,
19}
20
21impl From<FileRoleArg> for FileRole {
22    fn from(role: FileRoleArg) -> Self {
23        match role {
24            FileRoleArg::Featured => Self::Featured,
25            FileRoleArg::Attachment => Self::Attachment,
26            FileRoleArg::Inline => Self::Inline,
27            FileRoleArg::OgImage => Self::OgImage,
28            FileRoleArg::Thumbnail => Self::Thumbnail,
29        }
30    }
31}
32
33#[derive(Debug, Clone, Args)]
34pub struct LinkArgs {
35    #[arg(value_name = "FILE_ID", help = "File ID")]
36    pub file: String,
37
38    #[arg(long, help = "Content ID")]
39    pub content: String,
40
41    #[arg(long, value_enum, help = "File role")]
42    pub role: FileRoleArg,
43
44    #[arg(long, default_value = "0", help = "Display order")]
45    pub order: i32,
46}
47
48pub(super) async fn execute(args: LinkArgs, config: &CliConfig) -> Result<CommandOutput> {
49    let ctx = AppContext::new().await?;
50    execute_with_pool(args, ctx.db_pool(), config).await
51}
52
53pub async fn execute_with_pool(
54    args: LinkArgs,
55    pool: &DbPool,
56    _config: &CliConfig,
57) -> Result<CommandOutput> {
58    let service = FileRepository::new(pool)?;
59
60    let file_id = FileId::new(args.file.clone());
61    let content_id = ContentId::new(args.content.clone());
62    let role: FileRole = args.role.into();
63
64    service
65        .link_to_content(&content_id, &file_id, role, args.order)
66        .await?;
67
68    let output = ContentLinkOutput {
69        file_id,
70        content_id,
71        role: role.as_str().to_owned(),
72        message: "File linked to content successfully".to_owned(),
73    };
74
75    Ok(CommandOutput::card_value("File Linked", &output))
76}