Skip to main content

systemprompt_cli/
paths.rs

1//! Resolved filesystem paths the CLI operates against.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use std::path::{Path, PathBuf};
7
8use systemprompt_cloud::ProjectContext;
9use systemprompt_cloud::paths::{CloudPath, get_cloud_paths};
10
11#[derive(Debug)]
12pub struct ResolvedPaths {
13    project_ctx: ProjectContext,
14    has_local_dir: bool,
15}
16
17impl ResolvedPaths {
18    pub fn for_root(root: &Path) -> Self {
19        let project_ctx = ProjectContext::discover_from(root);
20        let has_local_dir = project_ctx.systemprompt_dir().exists();
21        Self {
22            project_ctx,
23            has_local_dir,
24        }
25    }
26
27    pub fn from_profile(profile: &systemprompt_models::Profile) -> Self {
28        Self::for_root(Path::new(&profile.paths.system))
29    }
30
31    pub fn discover() -> Self {
32        let project_ctx = ProjectContext::discover();
33        let has_local_dir = project_ctx.systemprompt_dir().exists();
34        tracing::debug!(
35            root = %project_ctx.root().display(),
36            has_local_dir,
37            "Resolved project root by walking up from the current directory"
38        );
39        Self {
40            project_ctx,
41            has_local_dir,
42        }
43    }
44
45    pub fn sessions_dir(&self) -> PathBuf {
46        if self.has_local_dir {
47            self.project_ctx.sessions_dir()
48        } else {
49            let cloud_paths = get_cloud_paths();
50            cloud_paths.resolve(CloudPath::SessionsDir)
51        }
52    }
53
54    pub fn tenants_path(&self) -> PathBuf {
55        if self.has_local_dir {
56            self.project_ctx.local_tenants()
57        } else {
58            let cloud_paths = get_cloud_paths();
59            cloud_paths.resolve(CloudPath::Tenants)
60        }
61    }
62
63    pub fn profiles_dir(&self) -> PathBuf {
64        self.project_ctx.profiles_dir()
65    }
66}