Skip to main content

systemprompt_cloud/
logout.rs

1//! Logout cleanup of all locally persisted cloud state.
2//!
3//! Removes `credentials.json`, the operator sign-in, `tenants.json`, and every
4//! tenant-scoped CLI 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 operator_credentials_path: Option<PathBuf>,
19    pub tenants_path: Option<PathBuf>,
20    pub tenant_sessions_removed: usize,
21}
22
23pub fn clear_cloud_state(cloud_paths: &CloudPaths) -> CloudResult<ClearedCloudState> {
24    let mut cleared = ClearedCloudState::default();
25
26    let credentials_path = cloud_paths.resolve(CloudPath::Credentials);
27    if credentials_path.exists() {
28        std::fs::remove_file(&credentials_path)?;
29        cleared.credentials_path = Some(credentials_path);
30    }
31
32    let operator_path = cloud_paths.resolve(CloudPath::OperatorCredentials);
33    if operator_path.exists() {
34        std::fs::remove_file(&operator_path)?;
35        cleared.operator_credentials_path = Some(operator_path);
36    }
37
38    let tenants_path = cloud_paths.resolve(CloudPath::Tenants);
39    if tenants_path.exists() {
40        std::fs::remove_file(&tenants_path)?;
41        cleared.tenants_path = Some(tenants_path);
42    }
43
44    let sessions_dir = cloud_paths.resolve(CloudPath::SessionsDir);
45    match SessionStore::load(&sessions_dir) {
46        Ok(Some(mut store)) => {
47            let removed = store.remove_tenant_sessions();
48            if removed > 0 {
49                store.save(&sessions_dir)?;
50                cleared.tenant_sessions_removed = removed;
51            }
52        },
53        Ok(None) => {},
54        Err(e) => {
55            tracing::warn!(error = %e, "Skipping session cleanup for unreadable session store");
56        },
57    }
58
59    Ok(cleared)
60}