Skip to main content

systemprompt_cli/commands/admin/config/
profile_io.rs

1//! Shared profile load/save for the `admin config` setter sub-trees.
2//!
3//! Every setter follows the same shape: deserialize the on-disk profile,
4//! mutate a typed field, write it back. `save_profile` revalidates before
5//! writing so a config edit can never persist a profile the loader would reject
6//! at boot — drift surfaces at the edit, not at the next service start.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11use std::path::Path;
12
13use anyhow::{Context, Result};
14use systemprompt_models::Profile;
15
16pub(super) fn load_profile(path: &str) -> Result<Profile> {
17    let content = std::fs::read_to_string(path)
18        .with_context(|| format!("Failed to read profile: {}", path))?;
19    serde_yaml::from_str(&content).with_context(|| format!("Failed to parse profile: {}", path))
20}
21
22pub(super) fn save_profile(profile: &Profile, path: &str) -> Result<()> {
23    profile
24        .validate()
25        .context("profile is invalid after edit; refusing to write")?;
26    let content = serde_yaml::to_string(profile).context("Failed to serialize profile")?;
27    std::fs::write(path, content).with_context(|| format!("Failed to write profile: {}", path))?;
28    Ok(())
29}
30
31pub(super) fn profile_dir(path: &str) -> &Path {
32    Path::new(path).parent().unwrap_or_else(|| Path::new("."))
33}