Skip to main content

systemprompt_cloud/
logout.rs

1//! Logout cleanup of all locally persisted cloud state.
2//!
3//! Removes `credentials.json`, `tenants.json`, and every tenant-scoped CLI
4//! session while leaving local sessions untouched.
5//!
6//! Copyright (c) systemprompt.io — Business Source License 1.1.
7//! See <https://systemprompt.io> for licensing details.
8
9use std::path::PathBuf;
10
11use crate::cli_session::SessionStore;
12use crate::error::CloudResult;
13use crate::paths::{CloudPath, CloudPaths};
14
15#[derive(Debug, Clone, Default)]
16pub struct ClearedCloudState {
17    pub credentials_path: Option<PathBuf>,
18    pub tenants_path: Option<PathBuf>,
19    pub tenant_sessions_removed: usize,
20}
21
22pub fn clear_cloud_state(cloud_paths: &CloudPaths) -> CloudResult<ClearedCloudState> {
23    let mut cleared = ClearedCloudState::default();
24
25    let credentials_path = cloud_paths.resolve(CloudPath::Credentials);
26    if credentials_path.exists() {
27        std::fs::remove_file(&credentials_path)?;
28        cleared.credentials_path = Some(credentials_path);
29    }
30
31    let tenants_path = cloud_paths.resolve(CloudPath::Tenants);
32    if tenants_path.exists() {
33        std::fs::remove_file(&tenants_path)?;
34        cleared.tenants_path = Some(tenants_path);
35    }
36
37    let sessions_dir = cloud_paths.resolve(CloudPath::SessionsDir);
38    match SessionStore::load(&sessions_dir) {
39        Ok(Some(mut store)) => {
40            let removed = store.remove_tenant_sessions();
41            if removed > 0 {
42                store.save(&sessions_dir)?;
43                cleared.tenant_sessions_removed = removed;
44            }
45        },
46        Ok(None) => {},
47        Err(e) => {
48            tracing::warn!(error = %e, "Skipping session cleanup for unreadable session store");
49        },
50    }
51
52    Ok(cleared)
53}