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
8use std::path::Path;
9
10use anyhow::{Context, Result};
11use systemprompt_models::Profile;
12
13pub(super) fn load_profile(path: &str) -> Result<Profile> {
14    let content = std::fs::read_to_string(path)
15        .with_context(|| format!("Failed to read profile: {}", path))?;
16    serde_yaml::from_str(&content).with_context(|| format!("Failed to parse profile: {}", path))
17}
18
19pub(super) fn save_profile(profile: &Profile, path: &str) -> Result<()> {
20    profile
21        .validate()
22        .context("profile is invalid after edit; refusing to write")?;
23    let content = serde_yaml::to_string(profile).context("Failed to serialize profile")?;
24    std::fs::write(path, content).with_context(|| format!("Failed to write profile: {}", path))?;
25    Ok(())
26}
27
28pub(super) fn profile_dir(path: &str) -> &Path {
29    Path::new(path).parent().unwrap_or_else(|| Path::new("."))
30}