1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
mod backup;
mod claude_json;
mod doctor;
mod git;
mod orphans;
mod output;
mod paths;
mod process;
mod prune;
mod secrets;
mod show;
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use colored::control;
use std::path::PathBuf;
use std::process::ExitCode;
#[derive(Clone, Copy, PartialEq, clap::ValueEnum)]
enum ColorMode {
Always,
Auto,
Never,
}
#[derive(Parser)]
#[command(
name = "midden",
about = "Resolve, audit, and garbage-collect Claude Code's accumulated state",
version,
propagate_version = true
)]
struct Cli {
/// When to use colors: auto, always, never
#[arg(long, default_value = "auto", global = true)]
color: ColorMode,
/// Output as JSON
#[arg(long, global = true)]
json: bool,
/// Path to `~/.claude.json` (default: $HOME/.claude.json)
#[arg(long, global = true, value_name = "PATH")]
config: Option<PathBuf>,
/// Path to the Claude user-scope directory (default: $HOME/.claude)
#[arg(long, global = true, value_name = "PATH")]
claude_home: Option<PathBuf>,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
/// Garbage-collect dead `projects` entries from ~/.claude.json
Prune {
/// Actually remove entries (default is a dry run)
#[arg(long)]
apply: bool,
/// Only consider entries under a .claude/worktrees/ path
#[arg(long = "worktrees-only")]
worktrees_only: bool,
/// Write even if a `claude` process appears to be running
#[arg(long)]
force: bool,
},
/// Hygiene and audit lint for Claude Code configuration
Doctor {
/// Target directory (resolves project-scope state for this path)
#[arg(default_value = ".")]
path: PathBuf,
/// Auto-resolve findings marked auto-fixable
#[arg(long)]
fix: bool,
/// Write even if a `claude` process appears to be running
#[arg(long)]
force: bool,
/// Unmask secret values in the output (dangerous)
#[arg(long = "show-secrets")]
show_secrets: bool,
},
/// Resolve every config surface for a target directory with provenance
Show {
/// Target directory
#[arg(default_value = ".")]
path: PathBuf,
/// Unmask secret values in the output (dangerous)
#[arg(long = "show-secrets")]
show_secrets: bool,
},
/// Generate shell completions
Completions {
/// Shell to generate completions for
shell: Shell,
},
}
fn main() -> ExitCode {
let cli = Cli::parse();
match cli.color {
ColorMode::Always => control::set_override(true),
ColorMode::Never => control::set_override(false),
ColorMode::Auto => {
if cli.json {
control::set_override(false);
}
}
}
let env = paths::Env::new(cli.config.clone(), cli.claude_home.clone());
let result = match cli.command {
Command::Prune {
apply,
worktrees_only,
force,
} => prune::run(
&env,
prune::Options {
apply,
worktrees_only,
force,
json: cli.json,
},
),
Command::Doctor {
ref path,
fix,
force,
show_secrets,
} => doctor::run(
&env,
doctor::Options {
path: path.clone(),
fix,
force,
show_secrets,
json: cli.json,
},
),
Command::Show {
ref path,
show_secrets,
} => show::run(
&env,
show::Options {
path: path.clone(),
show_secrets,
json: cli.json,
},
),
Command::Completions { shell } => {
clap_complete::generate(shell, &mut Cli::command(), "midden", &mut std::io::stdout());
return ExitCode::SUCCESS;
}
};
match result {
Ok(code) => code,
Err(e) => {
if cli.json {
let err = serde_json::json!({ "error": format!("{e:#}") });
eprintln!("{err}");
} else {
eprintln!("error: {e:#}");
}
ExitCode::from(2)
}
}
}