Skip to main content

systemprompt_cli/shared/
mod.rs

1//! Cross-command CLI utilities shared across command modules.
2//!
3//! Aggregates the [`CommandOutput`] artifact model and its output types,
4//! profile resolution ([`resolve_profile_path`],
5//! [`resolve_profile_with_data`]), argument parsers, and text helpers. Also
6//! defines the `define_pool_command!` macro used by the log commands to
7//! generate pooled execution entry points.
8
9pub mod command_result;
10pub mod disk_logs;
11pub mod parsers;
12pub mod paths;
13pub mod profile;
14pub mod project;
15pub mod text;
16
17pub use command_result::{
18    ChartType, CommandOutput, KeyValueItem, KeyValueOutput, SuccessOutput, TableOutput, TextOutput,
19    render_result,
20};
21pub use parsers::{parse_email, parse_profile_name};
22pub use profile::{
23    ProfileResolutionError, is_path_input, resolve_profile_from_path, resolve_profile_path,
24    resolve_profile_with_data,
25};
26pub use text::truncate_with_ellipsis;
27
28#[must_use]
29pub fn database_scoped_command_error() -> anyhow::Error {
30    systemprompt_config::ProfileBootstrap::get().map_or_else(
31        |_| {
32            anyhow::anyhow!(
33                "This command requires full profile context.\nPass --profile <local-profile> to \
34                 target a local environment, or re-authenticate with 'systemprompt admin session \
35                 login'."
36            )
37        },
38        |profile| {
39            anyhow::anyhow!(
40                "Active profile '{}' routes to an external/cloud database, which this command does \
41                 not support.\nPass --profile <local-profile> to target a local environment, or \
42                 re-authenticate with 'systemprompt admin session login'.",
43                profile.name
44            )
45        },
46    )
47}
48
49#[macro_export]
50macro_rules! define_pool_command {
51    ($args_ty:ty => $ret_ty:ty, with_config) => {
52        pub(in $crate::commands::infrastructure::logs) async fn execute(
53            args: $args_ty,
54            ctx: &$crate::context::CommandContext,
55        ) -> ::anyhow::Result<$ret_ty> {
56            let pool = ctx.db_pool().await?.pool_arc()?;
57            execute_with_pool_inner(args, &pool, &ctx.cli).await
58        }
59    };
60    ($args_ty:ty => $ret_ty:ty, no_config) => {
61        pub(in $crate::commands::infrastructure::logs) async fn execute(
62            args: $args_ty,
63            ctx: &$crate::context::CommandContext,
64        ) -> ::anyhow::Result<$ret_ty> {
65            let pool = ctx.db_pool().await?.pool_arc()?;
66            execute_with_pool_inner(args, &pool).await
67        }
68    };
69}