systemprompt_cli/commands/core/content/
edit.rs1use super::edit_apply::{
7 ContentEditState, apply_body_flags, apply_set_value_flags, apply_visibility_flags,
8};
9use super::types::UpdateOutput;
10use crate::cli_settings::CliConfig;
11use crate::context::CommandContext;
12use crate::interactive::{Prompter, resolve_required};
13use crate::shared::CommandOutput;
14use anyhow::{Result, anyhow};
15use clap::Args;
16use systemprompt_content::{CategoryIdUpdate, ContentRepository};
17use systemprompt_database::DbPool;
18use systemprompt_identifiers::{ContentId, LocaleCode, SourceId};
19use systemprompt_logging::CliService;
20
21#[derive(Debug, Args)]
22pub struct EditArgs {
23 #[arg(help = "Content ID or slug to edit")]
24 pub identifier: Option<String>,
25
26 #[arg(long, help = "Source ID (required when using slug)")]
27 pub source: Option<String>,
28
29 #[arg(long = "set", value_name = "KEY=VALUE", help = "Set a field value")]
30 pub set_values: Vec<String>,
31
32 #[arg(long, help = "Make content public", conflicts_with = "private")]
33 pub public: bool,
34
35 #[arg(long, help = "Make content private", conflicts_with = "public")]
36 pub private: bool,
37
38 #[arg(long, help = "Body content")]
39 pub body: Option<String>,
40
41 #[arg(long, help = "File containing body content")]
42 pub body_file: Option<String>,
43}
44
45pub(super) async fn execute(args: EditArgs, ctx: &CommandContext) -> Result<CommandOutput> {
46 let app = ctx.app_context().await?;
47 execute_with_pool(args, ctx.prompter(), app.db_pool(), &ctx.cli).await
48}
49
50pub async fn execute_with_pool(
51 args: EditArgs,
52 prompter: &dyn Prompter,
53 pool: &DbPool,
54 config: &CliConfig,
55) -> Result<CommandOutput> {
56 let repo = ContentRepository::new(pool)?;
57
58 let identifier = resolve_required(args.identifier.clone(), "identifier", config, || {
59 prompt_content_selection(prompter, &repo, args.source.as_deref())
60 })?;
61
62 let content = if identifier.starts_with("content_")
63 || identifier.contains('-') && identifier.len() > 30
64 {
65 let id = ContentId::new(identifier.clone());
66 repo.get_by_id(&id)
67 .await?
68 .ok_or_else(|| anyhow!("Content not found: {}", identifier))?
69 } else {
70 let source_id = args
71 .source
72 .as_ref()
73 .ok_or_else(|| anyhow!("Source ID required when using slug"))?;
74 let source = SourceId::new(source_id.clone());
75 repo.get_by_source_and_slug(&source, &identifier, &LocaleCode::new("en"))
76 .await?
77 .ok_or_else(|| anyhow!("Content not found: {} in source {}", identifier, source_id))?
78 };
79
80 let mut changes = Vec::new();
81 let mut state = ContentEditState {
82 title: content.title.clone(),
83 description: content.description.clone(),
84 body: content.body.clone(),
85 keywords: content.keywords.clone(),
86 image: content.image.clone(),
87 category_id: CategoryIdUpdate::Unchanged,
88 public_value: None,
89 kind_value: None,
90 };
91 apply_visibility_flags(&args, &mut state, &mut changes);
92 apply_body_flags(&args, &mut state, &mut changes)?;
93 apply_set_value_flags(&args, &mut state, &mut changes, &repo).await?;
94
95 if changes.is_empty() {
96 return Err(anyhow!(
97 "No changes specified. Use --set, --public, --private, --body, or --body-file"
98 ));
99 }
100
101 CliService::info(&format!("Updating content '{}'...", content.slug));
102
103 let params = systemprompt_content::UpdateContentParams::new(
104 content.id.clone(),
105 state.title,
106 state.description,
107 state.body,
108 )
109 .with_keywords(state.keywords)
110 .with_image(state.image)
111 .with_version_hash(content.version_hash.clone())
112 .with_category_id(state.category_id)
113 .with_public(state.public_value)
114 .with_kind(state.kind_value);
115
116 repo.update(¶ms).await?;
117
118 CliService::success(&format!("Content '{}' updated successfully", content.slug));
119
120 let output = UpdateOutput {
121 content_id: content.id,
122 slug: content.slug,
123 updated_fields: changes,
124 success: true,
125 };
126
127 Ok(CommandOutput::card_value("Content Updated", &output))
128}
129
130fn prompt_content_selection(
131 prompter: &dyn Prompter,
132 repo: &ContentRepository,
133 source: Option<&str>,
134) -> Result<String> {
135 let rt = tokio::runtime::Handle::current();
136 let contents = rt.block_on(async {
137 if let Some(source) = source {
138 let source = SourceId::new(source.to_owned());
139 repo.list_by_source_limited(&source, &LocaleCode::new("en"), 50)
140 .await
141 } else {
142 repo.list(50, 0).await
143 }
144 })?;
145
146 if contents.is_empty() {
147 return Err(anyhow!("No content found"));
148 }
149
150 let items: Vec<String> = contents
151 .iter()
152 .map(|c| format!("{} - {} ({})", c.id.as_str(), c.title, c.source_id.as_str()))
153 .collect();
154
155 let selection = prompter.select("Select content to edit", &items)?;
156
157 Ok(contents[selection].id.as_str().to_owned())
158}