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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! `cargo athena doctor` - preflight every prereq for publishing and
//! submitting workflows. Reports each check as a green / red line with
//! a fix hint when something is missing.
//!
//! Exit code: 0 on all-pass, 1 if any check failed.
use crate::style;
use cargo_athena::AthenaConfig;
use std::process::{Command, Stdio, exit};
#[derive(clap::Args)]
pub struct DoctorArgs {
/// Also try a live HEAD against the configured S3 bucket
///
/// Off by default because it needs working credentials and network.
#[arg(long)]
check_s3: bool,
}
const W: usize = 36;
pub fn doctor(args: DoctorArgs) {
let mut passed = 0usize;
let mut failed = 0usize;
let mut warned = 0usize;
eprintln!();
eprintln!("cargo athena doctor");
eprintln!();
// ---- athena.toml ------------------------------------------------------
let cfg = match try_load_config() {
Some((path, Ok(cfg))) => {
check_pass("athena.toml", &format!("loaded {path}"));
passed += 1;
Some(cfg)
}
// The file exists but doesn't read/parse — the exact thing
// doctor is for, so it must be a red check, not a panic.
Some((_, Err(e))) => {
check_fail("athena.toml", &e, None);
failed += 1;
None
}
None => {
check_fail(
"athena.toml",
"not found",
Some("create one with `cargo athena init`, or copy from docs/configuration"),
);
failed += 1;
None
}
};
// ---- toolchain --------------------------------------------------------
match exec_version("cargo-zigbuild", &["--version"]) {
Some(v) => {
check_pass("cargo-zigbuild", &v);
passed += 1;
}
None => {
check_fail(
"cargo-zigbuild",
"not found",
Some("cargo install cargo-zigbuild"),
);
failed += 1;
}
}
match exec_version("zig", &["version"]) {
Some(v) => {
check_pass("zig", &format!("zig {v}"));
passed += 1;
}
None => {
check_fail(
"zig",
"not found",
Some("pip install ziglang (or: brew install zig, or ziglang.org/download)"),
);
failed += 1;
}
}
// ---- rustup targets ---------------------------------------------------
if let Some(cfg) = cfg.as_ref() {
match rustup_installed_targets() {
Some(installed) => {
for t in &cfg.bootstrap.targets {
if installed.iter().any(|i| i == t) {
check_pass("rustup target", t);
passed += 1;
} else {
check_fail(
"rustup target",
&format!("{t} not installed"),
Some(&format!("rustup target add {t}")),
);
failed += 1;
}
}
if cfg.bootstrap.targets.is_empty() {
check_warn("rustup target", "athena.toml [bootstrap].targets is empty");
warned += 1;
}
}
None => {
check_warn(
"rustup",
"couldn't run `rustup target list` (rustup not installed?)",
);
warned += 1;
}
}
}
// ---- AWS credentials --------------------------------------------------
let aws_env = std::env::var("AWS_ACCESS_KEY_ID").is_ok()
&& std::env::var("AWS_SECRET_ACCESS_KEY").is_ok();
if aws_env {
check_pass("AWS credentials", "AWS_ACCESS_KEY_ID set");
passed += 1;
} else {
check_warn(
"AWS credentials",
"AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY not set (will rely on ambient identity: IMDS / ECS / IRSA)",
);
warned += 1;
}
// ---- live S3 (opt-in) -------------------------------------------------
if args.check_s3 {
if let Some(cfg) = cfg.as_ref() {
match check_s3_reachable(cfg) {
Ok(()) => {
check_pass(
"S3 reachable",
&format!(
"{} ({})",
cfg.artifact_repository.s3.bucket, cfg.artifact_repository.s3.endpoint
),
);
passed += 1;
}
Err(e) => {
check_fail(
"S3 reachable",
&e,
Some(
"verify athena.toml endpoint + AWS_* env (or AWS_ENDPOINT_URL for port-forward)",
),
);
failed += 1;
}
}
} else {
check_warn("S3 reachable", "skipped (no athena.toml)");
warned += 1;
}
}
// ---- summary ----------------------------------------------------------
eprintln!();
let total = passed + failed + warned;
if failed == 0 && warned == 0 {
eprintln!(
"{}",
style::good()
.bold()
.apply_to(format!("All {total} checks passed."))
);
exit(0);
} else if failed == 0 {
eprintln!("{passed} of {total} passed, {warned} warning(s) - okay, but read above.");
exit(0);
} else {
let w = if warned > 0 {
format!(", {warned} warning(s)")
} else {
String::new()
};
eprintln!(
"{}",
style::bad().apply_to(format!(
"{passed} of {total} passed, {failed} failed{w} - fix the above to publish."
))
);
exit(1);
}
}
// ---- check primitives -----------------------------------------------------
fn check_pass(name: &str, detail: &str) {
eprintln!(" {name:.<W$} {ok} {detail}", ok = style::ok());
}
fn check_fail(name: &str, msg: &str, fix: Option<&str>) {
eprintln!(" {name:.<W$} {err} {msg}", err = style::err());
if let Some(fix) = fix {
eprintln!(" {:>W$} fix: {fix}", "");
}
}
fn check_warn(name: &str, msg: &str) {
eprintln!(" {name:.<W$} {warn} {msg}", warn = style::warn());
}
// ---- implementations ------------------------------------------------------
fn try_load_config() -> Option<(String, Result<AthenaConfig, String>)> {
// `main()` has already resolved the effective config (`--config`,
// `$ATHENA_CONFIG`, the repo-local `./athena.toml`, or the global
// `~/.config` fallback) and exported `ATHENA_CONFIG`. Report whatever
// it landed on; the path itself shows which source won (e.g. a
// `~/.config/...` path means the global fallback). `None` = no file
// at all; `Some((path, Err(..)))` = present but unreadable/malformed.
let path = std::env::var_os("ATHENA_CONFIG").map(std::path::PathBuf::from)?;
if !path.is_file() {
return None;
}
Some((path.display().to_string(), AthenaConfig::try_load()))
}
fn exec_version(cmd: &str, args: &[&str]) -> Option<String> {
let out = Command::new(cmd)
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.output()
.ok()?;
if !out.status.success() {
return None;
}
let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
Some(s.lines().next().unwrap_or("").trim().to_string())
}
fn rustup_installed_targets() -> Option<Vec<String>> {
let out = Command::new("rustup")
.args(["target", "list", "--installed"])
.stdout(Stdio::piped())
.stderr(Stdio::null())
.output()
.ok()?;
if !out.status.success() {
return None;
}
Some(
String::from_utf8_lossy(&out.stdout)
.lines()
.map(|l| l.trim().to_string())
.filter(|l| !l.is_empty())
.collect(),
)
}
fn check_s3_reachable(cfg: &AthenaConfig) -> Result<(), String> {
// A sentinel key we don't care about; we only need the request to
// make it to the server. 404 is still "reachable".
let s3 = cargo_athena::S3Ref::from_repo(
&cfg.artifact_repository.s3,
".athena-doctor-probe".to_string(),
);
let store = crate::emulate::s3_store(&s3);
let key = object_store::path::Path::from(s3.key.as_str());
let result = crate::emulate::rt()
.block_on(async { object_store::ObjectStore::head(&store, &key).await });
match result {
Ok(_) => Ok(()),
Err(object_store::Error::NotFound { .. }) => Ok(()), // bucket reachable, key absent
Err(e) => Err(format!("{e}")),
}
}