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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use crate::{config::Config, error::Result, store, ui, vars::VarStore};
/// Audit repository health.
#[allow(clippy::too_many_lines)]
pub fn run() -> Result<()> {
ui::header("Doctor");
let mut ok = 0usize;
let mut warnings = 0usize;
let mut errors = 0usize;
// 1. Check repo root
if let Some(repo_root) = store::get_repo_root()? {
if repo_root.exists() {
ui::success(&format!("repo: {}", repo_root.display()));
ok += 1;
} else {
ui::error(&format!("repo directory missing: {}", repo_root.display()));
// errors += 1 — early return so we don't need to track it
ui::summary(0, 0, 1);
return Ok(());
}
// 2. Check config file
let config_path = store::config_path(&repo_root);
if config_path.exists() {
match Config::load(&config_path) {
Ok(config) => {
ui::success(&format!("config: {} entries", config.entries.len()));
ok += 1;
// 3. Check each entry
for entry in &config.entries {
let state =
crate::deploy::check_state(entry, &repo_root, config.settings.method);
let source_path = repo_root.join(&entry.source);
// Check source exists in repo
if !source_path.exists() {
ui::error(&format!("missing source: {}", entry.source));
errors += 1;
continue;
}
match state {
crate::deploy::EntryState::Deployed => {
ok += 1;
}
crate::deploy::EntryState::Modified => {
ui::warning(&format!(
"modified: {} → {}",
entry.source, entry.target
));
warnings += 1;
}
crate::deploy::EntryState::Missing => {
ui::error(&format!(
"not deployed: {} → {}",
entry.source, entry.target
));
errors += 1;
}
crate::deploy::EntryState::Broken => {
ui::error(&format!(
"broken link: {} → {}",
entry.source, entry.target
));
errors += 1;
}
crate::deploy::EntryState::Conflict => {
ui::warning(&format!(
"conflict: {} → {}",
entry.source, entry.target
));
warnings += 1;
}
}
}
// 3b. Template-specific checks
let var_store = VarStore::load().unwrap_or_default();
let repo_root_str = repo_root.to_string_lossy().into_owned();
let ctx = crate::template::RenderContext::new(
&repo_root_str,
&config.vars,
&var_store.as_pairs(),
);
for entry in &config.entries {
if !entry.template {
continue;
}
if entry.encrypted {
ui::info(&format!(
"template (encrypted — cannot validate without vault): {}",
entry.source
));
continue;
}
let source_path = repo_root.join(&entry.source);
if !source_path.exists() {
continue; // Already reported above
}
let Ok(text) = std::fs::read_to_string(&source_path) else {
continue;
};
let vars = crate::template::scan_variables(&text);
let mut missing_count = 0usize;
for var in &vars {
if var.namespace == "var" && ctx.resolve("var", &var.key).is_none() {
ui::warning(&format!(
"template `{}`: unresolved var.{} — run `dotling vars set {} <value>`",
entry.source, var.key, var.key
));
warnings += 1;
missing_count += 1;
}
}
if missing_count == 0 && !vars.is_empty() {
ok += 1;
}
}
// 3c. Check [vars] defaults for suspicious values
for (key, value) in &config.vars {
if let Some(hint) =
crate::vars::looks_like_real_value(key, value, &var_store)
{
ui::warning(&format!("[vars] {hint}"));
warnings += 1;
}
}
// 4. Check for orphan files in repo
check_orphans(&repo_root, &config);
}
Err(e) => {
ui::error(&format!("config error: {e}"));
errors += 1;
}
}
} else {
ui::error("config file (dotling.toml) not found");
errors += 1;
}
// 5. Check git
if repo_root.join(".git").exists() {
ui::success("git: initialized");
ok += 1;
} else {
ui::warning("git: not initialized");
warnings += 1;
}
// 6. Check vault
if crate::crypto::vault::vault_exists() {
ui::success("vault: initialized");
ok += 1;
} else {
ui::info("vault: not initialized (optional)");
}
} else {
ui::error("no repo found — run `dotling init <path>`");
errors += 1;
}
ui::summary(ok, warnings, errors);
Ok(())
}
/// Check for files in the repo that aren't tracked in the config.
fn check_orphans(repo_root: &std::path::Path, config: &Config) {
let Ok(files) = crate::fs::walk_dir(repo_root, false) else {
return;
};
for file in files {
let Ok(rel) = file.strip_prefix(repo_root) else {
continue;
};
let rel_str = rel.to_string_lossy();
// Skip known files
if rel_str == "dotling.toml" || rel_str.starts_with(".git") {
continue;
}
// Check if any entry tracks this file
let is_tracked = config.entries.iter().any(|e| {
let source = &e.source;
rel_str == *source || rel_str.starts_with(&format!("{source}/"))
});
if !is_tracked {
ui::warning(&format!("orphan: {rel_str}"));
}
}
}