Skip to content

Commit df09235

Browse files
committed
feat: Centralize configuration in ~/.codegraph directory
Changed configuration system to use a centralized user-level directory: ## Primary Changes - Updated ConfigManager to prioritize ~/.codegraph/ as primary config directory - Added fallback to ./config/ for backward compatibility - Added fallback to current directory as last resort ## New Features - `ConfigManager::init_user_config_dir()` - Initialize ~/.codegraph with defaults - `ConfigManager::get_config_dir()` - Get config dir with custom override - Automatic directory priority: ~/.codegraph → ./config → current dir - Creates README.txt in ~/.codegraph explaining the directory structure ## Benefits - **Centralized**: All CodeGraph configs in one uniform location - **User-level**: Config follows user across projects - **Standard practice**: Follows Unix/Linux convention (~/.config pattern) - **Backward compatible**: Existing ./config setups continue to work - **Cleaner projects**: Keeps project directories focused on code ## Documentation - Created comprehensive CONFIGURATION_GUIDE.md - Updated README.md to reflect new config directory - Added config/README.md explaining migration - Updated SURREALDB_GUIDE.md with new paths ## Configuration Loading Order 1. ~/.codegraph/default.toml (base) 2. ~/.codegraph/{environment}.toml (dev/staging/prod) 3. ~/.codegraph/local.toml (machine-specific overrides) 4. ./config/ (backward compatibility) 5. Environment variables (CODEGRAPH__* prefix) Users can initialize ~/.codegraph by: - Calling ConfigManager::init_user_config_dir(true) - Manually: mkdir -p ~/.codegraph && cp config/*.toml ~/.codegraph/ Existing projects using ./config will continue to work seamlessly.
1 parent f4d79a1 commit df09235

File tree

5 files changed

+587
-11
lines changed

5 files changed

+587
-11
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,18 @@ cargo build --release --bin codegraph-setup --features all-cloud-providers
323323

324324
### Manual Configuration
325325

326+
**Configuration directory: `~/.codegraph/`**
327+
328+
All configuration files are stored in `~/.codegraph/` in TOML format.
329+
326330
Configuration is loaded from (in order):
327-
1. `./.codegraph.toml` (project-specific)
328-
2. `~/.codegraph/config.toml` (global)
329-
3. Environment variables
331+
1. `~/.codegraph/default.toml` (base configuration)
332+
2. `~/.codegraph/{environment}.toml` (e.g., development.toml, production.toml)
333+
3. `~/.codegraph/local.toml` (local overrides, machine-specific)
334+
4. `./config/` (fallback for backward compatibility)
335+
5. Environment variables (CODEGRAPH__* prefix)
336+
337+
**See [Configuration Guide](docs/CONFIGURATION_GUIDE.md) for complete documentation.**
330338

331339
**Full configuration example:**
332340
```toml

config/README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# CodeGraph Configuration Files
2+
3+
## Important: Configuration Directory Migration
4+
5+
**As of the latest version, CodeGraph uses `~/.codegraph` as the primary configuration directory.**
6+
7+
### New Location: `~/.codegraph`
8+
9+
All user-level configuration files should now be placed in:
10+
11+
```
12+
~/.codegraph/
13+
```
14+
15+
This provides a centralized, uniform location for all CodeGraph configuration across your system.
16+
17+
### Why the Change?
18+
19+
- **Centralized**: All CodeGraph configs in one place, regardless of project
20+
- **User-level**: Configurations follow you across different projects
21+
- **Standard practice**: Follows Unix/Linux convention for user configuration
22+
- **Cleaner projects**: Keeps project directories focused on code
23+
24+
### Migration
25+
26+
To migrate your existing configurations:
27+
28+
```bash
29+
# Create the directory
30+
mkdir -p ~/.codegraph
31+
32+
# Copy existing configs
33+
cp config/*.toml ~/.codegraph/
34+
35+
# Or symlink for development (keeps backward compatibility)
36+
ln -s $(pwd)/config ~/.codegraph
37+
```
38+
39+
### Backward Compatibility
40+
41+
CodeGraph maintains backward compatibility by checking directories in this order:
42+
43+
1. **`~/.codegraph/`** (Primary)
44+
2. **`./config/`** (This directory - fallback)
45+
3. **Current directory** (last resort)
46+
47+
If `~/.codegraph` exists, it will be used. Otherwise, CodeGraph falls back to `./config/`.
48+
49+
## Configuration Files in This Directory
50+
51+
This directory contains **example configuration files** that can be copied to `~/.codegraph/`:
52+
53+
- `default.toml` - Base configuration example
54+
- `surrealdb_example.toml` - SurrealDB configuration
55+
- `example_embedding.toml` - Embedding provider configuration
56+
- `example_performance.toml` - Performance tuning
57+
- `production.toml` - Production settings example
58+
59+
## Quick Start
60+
61+
### 1. Initialize User Config
62+
63+
```bash
64+
# Create ~/.codegraph with default configs
65+
mkdir -p ~/.codegraph
66+
cp config/default.toml ~/.codegraph/
67+
```
68+
69+
### 2. Customize Your Configuration
70+
71+
```bash
72+
# Edit your user config
73+
nano ~/.codegraph/default.toml
74+
75+
# Or create environment-specific configs
76+
cp ~/.codegraph/default.toml ~/.codegraph/development.toml
77+
cp ~/.codegraph/default.toml ~/.codegraph/production.toml
78+
```
79+
80+
### 3. Set Environment
81+
82+
```bash
83+
export APP_ENV=development # Loads ~/.codegraph/development.toml
84+
# or
85+
export APP_ENV=production # Loads ~/.codegraph/production.toml
86+
```
87+
88+
## Environment Variables
89+
90+
Override any config value using environment variables:
91+
92+
```bash
93+
# Database backend
94+
export CODEGRAPH__DATABASE__BACKEND=surrealdb
95+
96+
# SurrealDB connection
97+
export CODEGRAPH__DATABASE__SURREALDB__CONNECTION=ws://localhost:8000
98+
99+
# Server port
100+
export CODEGRAPH__SERVER__PORT=8080
101+
```
102+
103+
## Further Documentation
104+
105+
- **[Full Configuration Guide](../docs/CONFIGURATION_GUIDE.md)** - Complete configuration documentation
106+
- **[SurrealDB Guide](../docs/SURREALDB_GUIDE.md)** - SurrealDB-specific configuration
107+
- **[Environment Variables](../docs/CONFIGURATION_GUIDE.md#environment-variables)** - Full list of env vars
108+
109+
## Development
110+
111+
For development, you can continue using this `./config` directory, but we recommend migrating to `~/.codegraph` for consistency:
112+
113+
```bash
114+
# Option 1: Copy to ~/.codegraph
115+
mkdir -p ~/.codegraph && cp config/*.toml ~/.codegraph/
116+
117+
# Option 2: Symlink (for active development)
118+
ln -s $(pwd)/config ~/.codegraph
119+
120+
# Option 3: Keep using ./config (backward compatible)
121+
# CodeGraph will use ./config if ~/.codegraph doesn't exist
122+
```
123+
124+
## Need Help?
125+
126+
- Check the [Configuration Guide](../docs/CONFIGURATION_GUIDE.md)
127+
- See example configs in this directory
128+
- Use `CODEGRAPH__LOGGING__LEVEL=debug` to see which config directory is being used

crates/codegraph-core/src/config.rs

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,97 @@ impl ConfigManager {
324324
Ok(manager)
325325
}
326326

327+
/// Get the default configuration directory.
328+
///
329+
/// Priority order:
330+
/// 1. ~/.codegraph/ (primary, user-level config)
331+
/// 2. ./config/ (backward compatibility, project-level config)
332+
/// 3. Current directory (fallback)
327333
pub fn default_config_dir() -> PathBuf {
328-
// Prefer ./config/, fallback to current dir
334+
// First, try ~/.codegraph
335+
if let Some(home_dir) = dirs::home_dir() {
336+
let codegraph_dir = home_dir.join(".codegraph");
337+
if codegraph_dir.exists() {
338+
info!("Using config directory: {:?}", codegraph_dir);
339+
return codegraph_dir;
340+
}
341+
}
342+
343+
// Fall back to ./config/ for backward compatibility
329344
let cwd = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
330-
let dir = cwd.join("config");
331-
if dir.exists() {
332-
dir
333-
} else {
334-
cwd
345+
let project_config = cwd.join("config");
346+
if project_config.exists() {
347+
info!("Using config directory: {:?}", project_config);
348+
return project_config;
349+
}
350+
351+
// Final fallback to current directory
352+
info!("Using config directory: {:?}", cwd);
353+
cwd
354+
}
355+
356+
/// Initialize the ~/.codegraph configuration directory with default config files.
357+
///
358+
/// This creates the directory if it doesn't exist and optionally copies
359+
/// default configuration files.
360+
pub fn init_user_config_dir(copy_defaults: bool) -> Result<PathBuf> {
361+
let home_dir = dirs::home_dir()
362+
.ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
363+
364+
let codegraph_dir = home_dir.join(".codegraph");
365+
366+
// Create directory if it doesn't exist
367+
if !codegraph_dir.exists() {
368+
fs::create_dir_all(&codegraph_dir)
369+
.context("Failed to create ~/.codegraph directory")?;
370+
info!("Created config directory: {:?}", codegraph_dir);
335371
}
372+
373+
if copy_defaults {
374+
// Create default.toml if it doesn't exist
375+
let default_config = codegraph_dir.join("default.toml");
376+
if !default_config.exists() {
377+
let default_content = include_str!("../../../config/default.toml");
378+
fs::write(&default_config, default_content)
379+
.context("Failed to write default config")?;
380+
info!("Created default config: {:?}", default_config);
381+
}
382+
383+
// Create README
384+
let readme = codegraph_dir.join("README.txt");
385+
if !readme.exists() {
386+
let readme_content = r#"CodeGraph Configuration Directory
387+
==================================
388+
389+
This directory contains configuration files for CodeGraph.
390+
391+
Configuration files are loaded in the following order:
392+
1. default.toml (base configuration)
393+
2. {environment}.toml (e.g., development.toml, production.toml)
394+
3. local.toml (local overrides, not tracked in git)
395+
4. Environment variables (CODEGRAPH__* prefix)
396+
397+
Example configuration files:
398+
- default.toml - Base configuration
399+
- development.toml - Development environment
400+
- production.toml - Production environment
401+
- surrealdb.toml - SurrealDB-specific config
402+
- embedding.toml - Embedding model config
403+
404+
For documentation, see: https://github.com/your-repo/codegraph-rust
405+
"#;
406+
fs::write(&readme, readme_content)
407+
.context("Failed to write README")?;
408+
info!("Created README: {:?}", readme);
409+
}
410+
}
411+
412+
Ok(codegraph_dir)
413+
}
414+
415+
/// Get the config directory path, with an option to specify a custom location
416+
pub fn get_config_dir(custom_path: Option<PathBuf>) -> PathBuf {
417+
custom_path.unwrap_or_else(Self::default_config_dir)
336418
}
337419

338420
pub fn load_from_sources(config_dir: &Path, env_name: &str) -> Result<Settings> {

0 commit comments

Comments
 (0)