portaki-cli 8.12.0

Portaki module CLI (portaki) — init, build, test, hosted sandbox, OCI publish and registry announcement
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! `portaki doctor` — ce qui, sur cette machine, empêcherait de développer ou de publier.
//!
//! Une ligne par contrôle, chacune avec sa correction. Rien n'est poussé ni signé : le registre
//! est interrogé pour un jeton de poussée, la toolchain pour sa cible, cosign pour sa version.

use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::Result;
use clap::Parser;
use serde_json::json;

use crate::api::Platform;
use crate::workspace::ModuleArgs;
use crate::{sign, ui};

#[derive(Debug, Parser)]
/// Arguments for `portaki doctor`.
pub struct DoctorArgs {
    #[command(flatten)]
    pub modules: ModuleArgs,
    /// Skip the lookups on crates.io and the registry — for an offline machine.
    #[arg(long)]
    pub offline: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "lowercase")]
enum Status {
    Ok,
    /// Un signal : rien n'est bloqué aujourd'hui.
    Warn,
    Fail,
    /// Pas vérifiable ici — la correction dit comment le vérifier.
    Skip,
}

#[derive(Debug, Clone, serde::Serialize)]
struct Check {
    id: &'static str,
    module: Option<String>,
    status: Status,
    summary: String,
    fix: Option<String>,
}

fn check(id: &'static str, status: Status, summary: impl Into<String>) -> Check {
    Check {
        id,
        module: None,
        status,
        summary: summary.into(),
        fix: None,
    }
}

impl Check {
    fn fix(mut self, fix: impl Into<String>) -> Self {
        self.fix = Some(fix.into());
        self
    }
}

/// Runs `portaki doctor`.
pub async fn run(args: DoctorArgs) -> Result<()> {
    ui::header(
        "portaki doctor",
        &crate::tr!("What on this machine would stop you from developing or publishing — and the fix.", "Ce qui, sur cette machine, empêcherait de développer ou de publier — et la correction."),
    );
    let base = crate::profile::api_url(None);
    let mut checks = Vec::new();

    let (session, mut platform) = session(&base).await;
    checks.push(session);
    checks.push(cli_version(args.offline).await);
    checks.push(cosign());
    checks.push(signing_identity(platform.as_ref()));

    let members = args.modules.resolve()?;
    for member in members.iter().filter(|member| !member.id.is_empty()) {
        let mut module = vec![
            toolchain(&member.root),
            sdk_versions(&member.root, args.offline).await,
            link(platform.as_mut(), &member.id).await,
            cargo_overrides(&member.root),
        ];
        for check in &mut module {
            check.module = Some(member.id.clone());
        }
        checks.extend(module);
    }
    if members.iter().all(|member| member.id.is_empty()) {
        checks.push(toolchain(&std::env::current_dir()?));
    }

    let failed = checks
        .iter()
        .filter(|check| check.status == Status::Fail)
        .count();
    render(&base, &checks);
    if failed > 0 {
        anyhow::bail!(
            "{failed} check{} failed",
            if failed == 1 { "" } else { "s" }
        );
    }
    Ok(())
}

fn render(base: &str, checks: &[Check]) {
    if ui::json() {
        ui::emit(&json!({
            "schemaVersion": 1,
            "api": base,
            "ok": checks.iter().all(|check| check.status != Status::Fail),
            "checks": checks,
        }));
        return;
    }
    ui::field("api", base);
    let mut current = None;
    for check in checks {
        if current != Some(check.module.as_deref()) {
            current = Some(check.module.as_deref());
            ui::section(check.module.as_deref().unwrap_or("this machine"));
        }
        let line = format!("{:<10} {}", check.id, check.summary);
        match check.status {
            Status::Ok => ui::success(line),
            Status::Warn => ui::warn(line),
            Status::Fail => ui::failure(line),
            Status::Skip => ui::skipped(line),
        }
        if let Some(fix) = &check.fix {
            ui::detail(format!("→ {fix}"));
        }
    }
    ui::blank();
}

/// Une session pour l'origine visée, et qu'elle ouvre bien quelque chose.
async fn session(base: &str) -> (Check, Option<Platform>) {
    let login = crate::profile::login_command(base);
    let mut platform = match Platform::open(base) {
        Ok(platform) => platform,
        Err(failure) => {
            return (
                check("session", Status::Fail, format!("{failure:#}")).fix(login),
                None,
            )
        }
    };
    match platform.get("/dev/v1/onboarding").await {
        Ok(_) => (
            check("session", Status::Ok, format!("signed in to {base}")),
            Some(platform),
        ),
        Err(failure) => (
            check(
                "session",
                Status::Fail,
                format!("the session for {base} does not open: {failure:#}"),
            )
            .fix(login),
            None,
        ),
    }
}

async fn cli_version(offline: bool) -> Check {
    let running = env!("CARGO_PKG_VERSION");
    if offline {
        return check("cli", Status::Skip, format!("portaki {running} (offline)"));
    }
    match crate::update::latest_published("portaki-cli").await {
        Ok(latest) if crate::update::outdated(running, &latest) => check(
            "cli",
            Status::Warn,
            format!("portaki {running} — {latest} is published"),
        )
        .fix("cargo install portaki-cli --locked --force"),
        Ok(_) => check("cli", Status::Ok, format!("portaki {running}, the latest")),
        Err(failure) => check(
            "cli",
            Status::Skip,
            format!("portaki {running} — could not reach crates.io: {failure}"),
        ),
    }
}

/// La toolchain que `cargo` prendra dans ce dossier, et sa cible wasm32.
///
/// Par le sysroot et non par `rustup` : une toolchain installée sans rustup a aussi sa cible,
/// et c'est le `rustc` de ce dossier — `rust-toolchain.toml` compris — qui compte.
fn toolchain(dir: &Path) -> Check {
    let run = |args: &[&str]| {
        Command::new("rustc")
            .args(args)
            .current_dir(dir)
            .output()
            .ok()
            .filter(|output| output.status.success())
            .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string())
    };
    let Some(version) = run(&["--version"]) else {
        return check("toolchain", Status::Fail, "no rustc found")
            .fix("install Rust from https://rustup.rs");
    };
    let has_target = run(&["--print", "sysroot"])
        .map(|sysroot| {
            Path::new(&sysroot)
                .join("lib/rustlib/wasm32-unknown-unknown")
                .is_dir()
        })
        .unwrap_or(false);
    if has_target {
        check(
            "toolchain",
            Status::Ok,
            format!("{version} with wasm32-unknown-unknown"),
        )
    } else {
        check(
            "toolchain",
            Status::Fail,
            format!("{version} without the wasm32-unknown-unknown target"),
        )
        .fix("rustup target add wasm32-unknown-unknown")
    }
}

fn cosign() -> Check {
    let bin = sign::cosign_binary();
    let version = Command::new(&bin)
        .args(["version", "--json"])
        .output()
        .ok()
        .and_then(|output| serde_json::from_slice::<serde_json::Value>(&output.stdout).ok())
        .and_then(|version| Some(version.get("gitVersion")?.as_str()?.to_string()));
    match sign::check_cosign(&bin) {
        Ok(()) => check(
            "cosign",
            Status::Ok,
            format!("cosign {}", version.unwrap_or_default()),
        ),
        // `release` signe par défaut : sans cosign, il faut `--no-sign`, et la version ne
        // tournera qu'en sandbox. Un signal, pas un blocage.
        Err(failure) => check("cosign", Status::Warn, format!("{failure:#}"))
            .fix("brew install cosign — portaki release signs with it"),
    }
}

/// L'adresse que Sigstore certifiera doit être l'adresse Portaki vérifiée : le registre refuse
/// sinon la signature d'auteur, après la poussée.
fn signing_identity(platform: Option<&Platform>) -> Check {
    if sign::in_ci() {
        return check(
            "signing",
            Status::Skip,
            "in CI, the release action signs with the workflow identity",
        );
    }
    let Some(portaki) = platform.and_then(|platform| verified_email(platform.token())) else {
        return check(
            "signing",
            Status::Skip,
            "no verified Portaki email in the session",
        )
        .fix("sign in, and verify your email in the developer space");
    };
    let how = format!(
        "Sigstore certifies the public email of your GitHub profile, or else its primary verified \
         email — make it {portaki} at https://github.com/settings/emails"
    );
    let Some(github) = github_email() else {
        return check(
            "signing",
            Status::Skip,
            format!("cannot read your GitHub email here (gh missing or signed out) — Portaki has {portaki}"),
        )
        .fix(how);
    };
    if github.eq_ignore_ascii_case(&portaki) {
        check(
            "signing",
            Status::Ok,
            format!("GitHub and Portaki both say {portaki}"),
        )
    } else {
        check(
            "signing",
            Status::Fail,
            format!("Sigstore would certify {github}, Portaki verified {portaki} — the registry would refuse the signature"),
        )
        .fix(how)
    }
}

/// `verified_email` du jeton d'accès — la claim même que le registre compare. Lue sans
/// vérifier la signature : c'est un diagnostic, le registre revérifie tout.
fn verified_email(token: &str) -> Option<String> {
    use base64::Engine as _;
    let payload = token.split('.').nth(1)?;
    let bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(payload.trim_end_matches('='))
        .ok()?;
    let claims: serde_json::Value = serde_json::from_slice(&bytes).ok()?;
    claims["verified_email"]
        .as_str()
        .filter(|email| !email.is_empty())
        .map(str::to_string)
}

/// L'adresse que Sigstore verra, lue par `gh` : l'adresse publique du profil, sinon la
/// principale vérifiée — la règle du connecteur GitHub de Sigstore.
fn github_email() -> Option<String> {
    let gh = std::env::var_os("PORTAKI_GH")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("gh"));
    let ask = |args: &[&str]| {
        Command::new(&gh)
            .args(args)
            .output()
            .ok()
            .filter(|output| output.status.success())
            .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string())
            .filter(|answer| !answer.is_empty() && answer != "null")
    };
    ask(&["api", "user", "--jq", ".email"]).or_else(|| {
        ask(&[
            "api",
            "user/emails",
            "--jq",
            ".[] | select(.primary and .verified) | .email",
        ])
    })
}

/// La CLI, le SDK que le lock résout, et le dernier SDK publié.
async fn sdk_versions(root: &Path, offline: bool) -> Check {
    use crate::commands::ci::{find_lockfile, read_locked_sdk};
    let cli = env!("CARGO_PKG_VERSION");
    let Some(locked) = find_lockfile(root)
        .and_then(|lock| std::fs::read_to_string(lock).ok())
        .and_then(|text| read_locked_sdk(&text))
    else {
        return check(
            "sdk",
            Status::Skip,
            "no Cargo.lock resolving portaki-sdk yet",
        )
        .fix("portaki build — cargo writes the lock");
    };
    let sdk = locked.version;
    if crate::update::outdated(cli, &sdk) {
        return check(
            "sdk",
            Status::Warn,
            format!("portaki {cli} is older than the SDK the module resolves ({sdk})"),
        )
        .fix(format!("cargo install portaki-cli@{sdk} --locked --force"));
    }
    if offline {
        return check(
            "sdk",
            Status::Ok,
            format!("portaki-sdk {sdk}, portaki {cli}"),
        );
    }
    match crate::update::latest_published("portaki-sdk").await {
        Ok(latest) if crate::update::outdated(&sdk, &latest) => check(
            "sdk",
            Status::Warn,
            format!("portaki-sdk {sdk} — {latest} is published"),
        )
        .fix("portaki upgrade"),
        Ok(_) => check(
            "sdk",
            Status::Ok,
            format!("portaki-sdk {sdk}, the latest; portaki {cli}"),
        ),
        Err(_) => check(
            "sdk",
            Status::Ok,
            format!("portaki-sdk {sdk}, portaki {cli} (crates.io unreachable)"),
        ),
    }
}

/// L'environnement GitHub qui porte les relecteurs de la stable — décision du 28 sept.
const STABLE_ENVIRONMENT: &str = "release";

async fn link(platform: Option<&mut Platform>, id: &str) -> Check {
    let Some(platform) = platform else {
        return check("link", Status::Skip, "needs a session");
    };
    match platform.get(&format!("/dev/v1/modules/{id}/link")).await {
        Ok(Some(link)) => {
            let repository = link["repository"].as_str().unwrap_or("a repository");
            match link["requiredEnvironment"].as_str() {
                Some(STABLE_ENVIRONMENT) => check(
                    "link",
                    Status::Ok,
                    format!("linked to {repository}, stable from the `release` environment"),
                ),
                other => check(
                    "link",
                    Status::Warn,
                    format!(
                        "linked to {repository}, but the stable channel's GitHub environment is {} — Portaki expects `release`",
                        other.map(|name| format!("`{name}`")).unwrap_or_else(|| "not set".to_string())
                    ),
                )
                .fix(format!(
                    "set the environment to `release` in the link (portaki link --module {id}), and `environment: release` on the release job"
                )),
            }
        }
        Ok(None) => check(
            "link",
            Status::Warn,
            "not linked to a repository — CI cannot publish it",
        )
        .fix(format!("portaki link --module {id}")),
        Err(failure) => check("link", Status::Skip, format!("{failure:#}")),
    }
}

/// Ce qui ferait prendre à `cargo` une autre toolchain ou d'autres options que celles du CLI.
///
/// Un signal, pas un blocage : ces fichiers sont souvent voulus. Mais un module qui en porte
/// un décide de ce que `cargo` exécute — le job de publication doit le savoir.
fn cargo_overrides(root: &Path) -> Check {
    let found = overrides(root);
    if found.is_empty() {
        return check(
            "overrides",
            Status::Ok,
            "no .cargo/config or rust-toolchain redirects cargo",
        );
    }
    let names: Vec<String> = found
        .iter()
        .map(|path| path.display().to_string())
        .collect();
    check(
        "overrides",
        Status::Warn,
        format!("cargo reads {}", names.join(", ")),
    )
    .fix("make sure they are intended — they choose the toolchain and flags cargo runs with")
}

/// Du module jusqu'à la racine du dépôt (le dossier qui porte `.git`), inclus.
fn overrides(root: &Path) -> Vec<PathBuf> {
    const FILES: [&str; 4] = [
        ".cargo/config.toml",
        ".cargo/config",
        "rust-toolchain.toml",
        "rust-toolchain",
    ];
    let mut found = Vec::new();
    for dir in root.ancestors() {
        found.extend(
            FILES
                .iter()
                .map(|file| dir.join(file))
                .filter(|path| path.is_file()),
        );
        if dir.join(".git").exists() {
            break;
        }
    }
    found
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_verified_email_is_read_from_the_token() {
        use base64::Engine as _;
        let payload = base64::engine::general_purpose::URL_SAFE_NO_PAD
            .encode(r#"{"sub":"x","verified_email":"dev@example.com"}"#);
        assert_eq!(
            verified_email(&format!("h.{payload}.s")).as_deref(),
            Some("dev@example.com")
        );
        assert!(verified_email("not-a-jwt").is_none());
    }

    #[test]
    fn overrides_are_found_up_to_the_repository_root() {
        let repo = tempfile::tempdir().unwrap();
        let module = repo.path().join("modules/nuki");
        std::fs::create_dir_all(module.join(".cargo")).unwrap();
        std::fs::create_dir_all(repo.path().join(".git")).unwrap();
        std::fs::write(module.join(".cargo/config.toml"), "").unwrap();
        std::fs::write(repo.path().join("rust-toolchain.toml"), "").unwrap();

        let found = overrides(&module);
        assert_eq!(found.len(), 2, "{found:?}");
        assert_eq!(cargo_overrides(&module).status, Status::Warn);
        assert!(overrides(&repo.path().join("modules")).len() == 1);
    }
}