Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ureq = "2"
hostname = "0.4"

[dev-dependencies]
temp-env = "0.3"

[profile.release]
opt-level = 3
Expand Down
2 changes: 2 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ rtk gain # MUST show token savings, not "command not found"

### Recommended: Global Hook-First Setup

> **Custom config directory**: If you've set `CLAUDE_CONFIG_DIR`, RTK respects it — all paths below use that directory instead of `~/.claude`.

**Best for: All projects, automatic RTK usage**

```bash
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ The hook runs as a Claude Code [PreToolUse hook](https://docs.anthropic.com/en/d

### Quick Install (Automated)

> **Custom config directory**: If you've set `CLAUDE_CONFIG_DIR`, RTK respects it — all paths below use that directory instead of `~/.claude`.

```bash
rtk init -g
# → Installs hook to ~/.claude/hooks/rtk-rewrite.sh (with executable permissions)
Expand Down
33 changes: 32 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
use anyhow::Result;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Resolve the Claude Code config directory.
///
/// Mirrors Claude Code's own logic:
/// 1. `CLAUDE_CONFIG_DIR` env var (if set)
/// 2. Fallback: `~/.claude`
pub fn claude_config_dir() -> Result<PathBuf> {
if let Ok(dir) = std::env::var("CLAUDE_CONFIG_DIR") {
return Ok(PathBuf::from(dir));
}
dirs::home_dir()
.map(|h| h.join(".claude"))
.context("Cannot determine home directory. Is $HOME set?")
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Config {
#[serde(default)]
Expand Down Expand Up @@ -184,4 +198,21 @@ history_days = 90
let config: Config = toml::from_str(toml).expect("valid toml");
assert!(config.hooks.exclude_commands.is_empty());
}

#[test]
fn test_claude_config_dir_env_override() {
temp_env::with_var("CLAUDE_CONFIG_DIR", Some("/tmp/custom-claude"), || {
let dir = claude_config_dir().unwrap();
assert_eq!(dir, PathBuf::from("/tmp/custom-claude"));
});
}

#[test]
fn test_claude_config_dir_default() {
temp_env::with_var_unset("CLAUDE_CONFIG_DIR", || {
let dir = claude_config_dir().unwrap();
let home = dirs::home_dir().unwrap();
assert_eq!(dir, home.join(".claude"));
});
}
}
3 changes: 1 addition & 2 deletions src/discover/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ pub struct ClaudeProvider;
impl ClaudeProvider {
/// Get the base directory for Claude Code projects.
fn projects_dir() -> Result<PathBuf> {
let home = dirs::home_dir().context("could not determine home directory")?;
let dir = home.join(".claude").join("projects");
let dir = crate::config::claude_config_dir()?.join("projects");
if !dir.exists() {
anyhow::bail!(
"Claude Code projects directory not found: {}\nMake sure Claude Code has been used at least once.",
Expand Down
6 changes: 4 additions & 2 deletions src/hook_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ pub fn parse_hook_version(content: &str) -> u8 {
}

fn hook_installed_path() -> Option<PathBuf> {
let home = dirs::home_dir()?;
let path = home.join(".claude").join("hooks").join("rtk-rewrite.sh");
let path = crate::config::claude_config_dir()
.ok()?
.join("hooks")
.join("rtk-rewrite.sh");
if path.exists() {
Some(path)
} else {
Expand Down
Loading