Skip to content

Commit 23647d4

Browse files
committed
feat(config): persist settings in ~/.cortex on Linux/macOS
Updated the configuration directory to use ~/.cortex on Linux/macOS instead of ~/.config/cortex to match the convention used throughout the codebase. This makes it easier for users to find and manage their settings. Changes: - CortexConfig::config_dir() now uses ~/.cortex on Linux/macOS - AppDirs now uses ~/.cortex as the primary location - Added CORTEX_HOME environment variable override support - Updated legacy path detection to handle migration from XDG paths - Windows still uses %APPDATA%\cortex The last_model and last_provider are already persisted via the save_last_model() function whenever a model is changed.
1 parent ff00fc2 commit 23647d4

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

cortex-common/src/dirs.rs

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
//! Cross-platform application directories for Cortex CLI.
22
//!
33
//! Provides unified directory management following platform conventions:
4-
//! - Linux: XDG Base Directory Specification (~/.config/cortex, ~/.local/share/cortex, ~/.cache/cortex)
5-
//! - macOS: XDG-style for CLI tools (~/.config/cortex) - matches gh, aws, cargo behavior
6-
//! - Windows: %APPDATA%\cortex
4+
//! - Linux/macOS: `~/.cortex` (simple, easy to find)
5+
//! - Windows: `%APPDATA%\cortex`
6+
//!
7+
//! Can be overridden with `CORTEX_HOME` environment variable.
78
89
use std::path::PathBuf;
910

1011
/// Application name for directory paths
1112
pub const APP_NAME: &str = "cortex";
1213

13-
/// Legacy directory name (for migration)
14-
pub const LEGACY_APP_NAME: &str = ".cortex";
14+
/// Primary home directory name on Linux/macOS
15+
pub const HOME_DIR_NAME: &str = ".cortex";
1516

16-
/// Old cortex directory name (for migration)
17-
pub const CORTEX_LEGACY_NAME: &str = ".cortex";
17+
/// Legacy directory name (for migration from XDG paths)
18+
pub const LEGACY_XDG_NAME: &str = ".config/cortex";
1819

1920
/// Application directories structure
2021
#[derive(Debug, Clone)]
2122
pub struct AppDirs {
22-
/// Configuration directory (~/.config/cortex on Linux/macOS, %APPDATA%\cortex on Windows)
23+
/// Configuration directory (~/.cortex on Linux/macOS, %APPDATA%\cortex on Windows)
2324
pub config_dir: PathBuf,
24-
/// Data directory (~/.local/share/cortex on Linux/macOS, %APPDATA%\cortex on Windows)
25+
/// Data directory (same as config_dir for simplicity)
2526
pub data_dir: PathBuf,
26-
/// Cache directory (~/.cache/cortex on Linux/macOS, %LOCALAPPDATA%\cortex on Windows)
27+
/// Cache directory (~/.cortex/cache on Linux/macOS, %LOCALAPPDATA%\cortex on Windows)
2728
pub cache_dir: PathBuf,
28-
/// Legacy home directory (~/.cortex) for migration
29-
pub legacy_home: PathBuf,
30-
/// Old cortex directory (~/.cortex) for migration
31-
pub cortex_legacy: PathBuf,
29+
/// Legacy XDG home directory (~/.config/cortex) for migration
30+
pub legacy_xdg_home: PathBuf,
3231
}
3332

3433
impl AppDirs {
@@ -40,20 +39,19 @@ impl AppDirs {
4039
/// - `CORTEX_DATA_DIR`: Override data directory only
4140
/// - `CORTEX_CACHE_DIR`: Override cache directory only
4241
pub fn new() -> Option<Self> {
42+
let home_dir = dirs::home_dir()?;
43+
4344
// Check for CORTEX_HOME override first
4445
if let Ok(home) = std::env::var("CORTEX_HOME") {
4546
let home = PathBuf::from(home);
4647
return Some(Self {
4748
config_dir: home.clone(),
4849
data_dir: home.clone(),
49-
cache_dir: home.clone(),
50-
legacy_home: dirs::home_dir()?.join(LEGACY_APP_NAME),
51-
cortex_legacy: dirs::home_dir()?.join(CORTEX_LEGACY_NAME),
50+
cache_dir: home.join("cache"),
51+
legacy_xdg_home: home_dir.join(LEGACY_XDG_NAME),
5252
});
5353
}
5454

55-
let home_dir = dirs::home_dir()?;
56-
5755
// Platform-specific defaults
5856
#[cfg(target_os = "windows")]
5957
let (config_dir, data_dir, cache_dir) = {
@@ -68,20 +66,12 @@ impl AppDirs {
6866

6967
#[cfg(not(target_os = "windows"))]
7068
let (config_dir, data_dir, cache_dir) = {
71-
// XDG-style for both Linux and macOS (CLI convention)
72-
let xdg_config = std::env::var("XDG_CONFIG_HOME")
73-
.map(PathBuf::from)
74-
.unwrap_or_else(|_| home_dir.join(".config"));
75-
let xdg_data = std::env::var("XDG_DATA_HOME")
76-
.map(PathBuf::from)
77-
.unwrap_or_else(|_| home_dir.join(".local").join("share"));
78-
let xdg_cache = std::env::var("XDG_CACHE_HOME")
79-
.map(PathBuf::from)
80-
.unwrap_or_else(|_| home_dir.join(".cache"));
69+
// Use ~/.cortex for Linux/macOS (simple, easy to find)
70+
let cortex_home = home_dir.join(HOME_DIR_NAME);
8171
(
82-
xdg_config.join(APP_NAME),
83-
xdg_data.join(APP_NAME),
84-
xdg_cache.join(APP_NAME),
72+
cortex_home.clone(),
73+
cortex_home.clone(),
74+
cortex_home.join("cache"),
8575
)
8676
};
8777

@@ -100,8 +90,7 @@ impl AppDirs {
10090
config_dir,
10191
data_dir,
10292
cache_dir,
103-
legacy_home: home_dir.join(LEGACY_APP_NAME),
104-
cortex_legacy: home_dir.join(CORTEX_LEGACY_NAME),
93+
legacy_xdg_home: home_dir.join(LEGACY_XDG_NAME),
10594
})
10695
}
10796

@@ -145,20 +134,18 @@ impl AppDirs {
145134
Ok(())
146135
}
147136

148-
/// Check if legacy directories exist and need migration
137+
/// Check if legacy XDG directories exist and need migration
149138
pub fn has_legacy_data(&self) -> bool {
150-
self.legacy_home.exists() || self.cortex_legacy.exists()
139+
self.legacy_xdg_home.exists()
151140
}
152141

153142
/// Get the effective home directory (for backward compatibility)
154-
/// Prefers new XDG location, falls back to legacy if it exists
143+
/// Prefers new ~/.cortex location, falls back to legacy XDG if it exists
155144
pub fn effective_home(&self) -> PathBuf {
156145
if self.config_dir.exists() {
157146
self.config_dir.clone()
158-
} else if self.legacy_home.exists() {
159-
self.legacy_home.clone()
160-
} else if self.cortex_legacy.exists() {
161-
self.cortex_legacy.clone()
147+
} else if self.legacy_xdg_home.exists() {
148+
self.legacy_xdg_home.clone()
162149
} else {
163150
self.config_dir.clone()
164151
}

cortex-tui/src/providers/config.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,28 @@ impl Default for CortexConfig {
207207

208208
impl CortexConfig {
209209
/// Gets the Cortex configuration directory.
210+
///
211+
/// Uses platform-specific conventions:
212+
/// - Linux/macOS: `~/.cortex`
213+
/// - Windows: `%APPDATA%\cortex`
214+
///
215+
/// Can be overridden with the `CORTEX_HOME` environment variable.
210216
pub fn config_dir() -> Result<PathBuf> {
217+
// Check for CORTEX_HOME override first
218+
if let Ok(home) = std::env::var("CORTEX_HOME") {
219+
return Ok(PathBuf::from(home));
220+
}
221+
211222
let config_dir = if cfg!(windows) {
223+
// Windows: %APPDATA%\cortex
212224
dirs::config_dir()
213225
.context("Could not find config directory")?
214226
.join("cortex")
215227
} else {
228+
// Linux/macOS: ~/.cortex
216229
dirs::home_dir()
217230
.context("Could not find home directory")?
218-
.join(".config")
219-
.join("cortex")
231+
.join(".cortex")
220232
};
221233
Ok(config_dir)
222234
}

0 commit comments

Comments
 (0)