systemprompt_cli/commands/cloud/profile/
mod.rs1mod api_keys;
8mod args;
9mod create;
10mod create_setup;
11mod create_tenant;
12pub(super) mod delete;
13mod edit;
14mod edit_secrets;
15mod edit_settings;
16mod list;
17mod profile_steps;
18mod show;
19mod show_display;
20mod show_types;
21pub mod templates;
22
23pub use api_keys::collect_api_keys;
24pub use args::{CreateArgs, DeleteArgs, EditArgs, ProfileCommands, ShowFilter, TenantTypeArg};
25pub use create::{CreatedProfile, create_profile_for_tenant};
26pub use create_setup::{get_cloud_user, handle_local_tenant_setup};
27
28use crate::context::CommandContext;
29use crate::shared::render_result;
30use anyhow::Result;
31use dialoguer::Select;
32use dialoguer::theme::ColorfulTheme;
33use systemprompt_cloud::{ProfilePath, ProjectContext};
34use systemprompt_logging::CliService;
35
36pub async fn execute(cmd: Option<ProfileCommands>, ctx: &CommandContext) -> Result<()> {
37 if let Some(cmd) = cmd {
38 execute_command(cmd, ctx).await.map(drop)
39 } else {
40 if !ctx.cli.is_interactive() {
41 return Err(anyhow::anyhow!(
42 "Profile subcommand required in non-interactive mode"
43 ));
44 }
45 while let Some(cmd) = select_operation()? {
46 if execute_command(cmd, ctx).await? {
47 break;
48 }
49 }
50 Ok(())
51 }
52}
53
54async fn execute_command(cmd: ProfileCommands, ctx: &CommandContext) -> Result<bool> {
55 match cmd {
56 ProfileCommands::Create(args) => create::execute(&args, &ctx.cli).await.map(|()| true),
57 ProfileCommands::List => {
58 let result = list::execute(ctx)?;
59 render_result(&result, &ctx.cli);
60 Ok(false)
61 },
62 ProfileCommands::Show {
63 name,
64 filter,
65 json,
66 yaml,
67 } => show::execute(name.as_deref(), filter, json, yaml, ctx).map(|()| false),
68 ProfileCommands::Delete(args) => {
69 let result = delete::execute(&args, &ctx.cli)?;
70 render_result(&result, &ctx.cli);
71 Ok(false)
72 },
73 ProfileCommands::Edit(args) => edit::execute(&args, ctx).map(|()| false),
74 }
75}
76
77fn select_operation() -> Result<Option<ProfileCommands>> {
78 let ctx = ProjectContext::discover();
79 let profiles_dir = ctx.profiles_dir();
80 let has_profiles = profiles_dir.exists()
81 && std::fs::read_dir(&profiles_dir).is_ok_and(|entries| {
82 entries
83 .filter_map(Result::ok)
84 .any(|e| e.path().is_dir() && ProfilePath::Config.resolve(&e.path()).exists())
85 });
86
87 let edit_label = if has_profiles {
88 "Edit".to_owned()
89 } else {
90 "Edit (unavailable - no profiles)".to_owned()
91 };
92 let delete_label = if has_profiles {
93 "Delete".to_owned()
94 } else {
95 "Delete (unavailable - no profiles)".to_owned()
96 };
97
98 let operations = vec![
99 "List".to_owned(),
100 edit_label,
101 delete_label,
102 "Done".to_owned(),
103 ];
104
105 let selection = Select::with_theme(&ColorfulTheme::default())
106 .with_prompt("Profile operation")
107 .items(&operations)
108 .default(0)
109 .interact()?;
110
111 let cmd = match selection {
112 0 => Some(ProfileCommands::List),
113 1 | 2 if !has_profiles => {
114 CliService::warning("No profiles found");
115 CliService::info(
116 "Run 'systemprompt cloud tenant create' (or 'just tenant') to create a tenant \
117 with a profile.",
118 );
119 return Ok(Some(ProfileCommands::List));
120 },
121 1 => Some(ProfileCommands::Edit(EditArgs {
122 name: None,
123 set_anthropic_key: None,
124 set_openai_key: None,
125 set_gemini_key: None,
126 set_github_token: None,
127 set_database_url: None,
128 set_external_url: None,
129 set_host: None,
130 set_port: None,
131 })),
132 2 => select_profile("Select profile to delete")?
133 .map(|name| ProfileCommands::Delete(DeleteArgs { name, yes: false })),
134 3 => None,
135 _ => unreachable!(),
136 };
137
138 Ok(cmd)
139}
140
141fn select_profile(prompt: &str) -> Result<Option<String>> {
142 let ctx = ProjectContext::discover();
143 let profiles_dir = ctx.profiles_dir();
144
145 if !profiles_dir.exists() {
146 CliService::warning("No profiles directory found.");
147 return Ok(None);
148 }
149
150 let profiles: Vec<String> = std::fs::read_dir(&profiles_dir)?
151 .filter_map(Result::ok)
152 .filter(|e| e.path().is_dir() && ProfilePath::Config.resolve(&e.path()).exists())
153 .filter_map(|e| e.file_name().to_str().map(String::from))
154 .collect();
155
156 if profiles.is_empty() {
157 CliService::warning("No profiles found.");
158 return Ok(None);
159 }
160
161 let selection = Select::with_theme(&ColorfulTheme::default())
162 .with_prompt(prompt)
163 .items(&profiles)
164 .default(0)
165 .interact()?;
166
167 Ok(Some(profiles[selection].clone()))
168}