ferro-cli 0.2.13

CLI for scaffolding Ferro web applications
Documentation
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
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Phase 122.2 §4: `.do/app.yaml` starter renderer.
//!
//! Pure string substitution over `files/do/app.yaml.tpl`. Caller is
//! responsible for I/O (reading Cargo.toml, .env.production, git remote).

// Renderer is pure and I/O-free. Web-bin resolution uses
// `crate::deploy::bin_detect::detect_web_bin` at the caller
// (`commands/do_init.rs`) so that the Dockerfile ENTRYPOINT and the DO web
// service stay in sync by construction (ferro 127, D-02).
#[cfg(test)]
use crate::deploy::env_production::parse_env_example_structured;
use crate::deploy::env_production::EnvLine;
use crate::deploy::secret_keys::is_secret_key;

const TEMPLATE: &str = include_str!("files/do/app.yaml.tpl");

/// Inputs for [`render_app_yaml`]. All fields are pre-resolved by the caller.
pub struct AppYamlContext {
    /// Sanitized DO app name (output of [`sanitize_do_app_name`]).
    pub name: String,
    /// `owner/repo` for `github.repo`, or fallback placeholder.
    pub repo: String,
    /// The bin matching the package name — kept for symmetry, not currently
    /// emitted (the `web` service is hardcoded in the template).
    pub web_bin: String,
    /// Non-web, non-test-like bins to emit as workers.
    pub workers: Vec<String>,
    /// Structured `.env.example` lines (keys + blank separators). `None` when
    /// `.env.example` is missing — the renderer emits an empty envs block and
    /// the caller logs a warning (D-06 graceful missing-file path).
    pub env_lines: Option<Vec<EnvLine>>,
    /// Identity override: DO App Platform app name from an existing file.
    /// When `Some`, takes precedence over `name` (derived from package name).
    pub preserved_name: Option<String>,
    /// Identity override: DO region slug from an existing file.
    /// When `Some`, takes precedence over the default (`fra1`).
    pub preserved_region: Option<String>,
    /// Identity override: `github.repo` from an existing file.
    /// When `Some`, takes precedence over the git-remote-derived `repo`.
    pub preserved_github_repo: Option<String>,
    /// Identity override: `github.branch` from an existing file.
    /// When `Some`, takes precedence over the default (`main`).
    pub preserved_github_branch: Option<String>,
}

/// Render `.do/app.yaml` from a fully-resolved context.
pub fn render_app_yaml(ctx: &AppYamlContext) -> String {
    let workers_block = render_workers_block(&ctx.workers);
    let envs_block = match &ctx.env_lines {
        Some(lines) => render_envs_block_from_lines(lines),
        None => String::new(),
    };

    // Preserved identity fields take precedence over derived defaults.
    let name = ctx.preserved_name.as_deref().unwrap_or(ctx.name.as_str());
    let region = ctx.preserved_region.as_deref().unwrap_or("fra1");
    let repo = ctx
        .preserved_github_repo
        .as_deref()
        .unwrap_or(ctx.repo.as_str());
    let branch = ctx.preserved_github_branch.as_deref().unwrap_or("main");

    let rendered = TEMPLATE
        .replace("{{NAME}}", name)
        .replace("{{REGION}}", region)
        .replace("{{REPO}}", repo)
        .replace("{{GITHUB_BRANCH}}", branch)
        .replace("{{WORKERS_BLOCK}}", &workers_block)
        .replace("{{ENVS_BLOCK}}", &envs_block);
    debug_assert!(
        !rendered.contains("{{"),
        "unresolved template token in rendered .do/app.yaml"
    );
    rendered
}

/// Test helper: render an envs block from raw `.env.example` contents.
#[cfg(test)]
fn render_envs_block(env_example_contents: &str) -> String {
    let lines = parse_env_example_structured(env_example_contents);
    render_envs_block_from_lines(&lines)
}

fn render_envs_block_from_lines(lines: &[EnvLine]) -> String {
    let mut out = String::new();
    let indent = "  "; // child of `envs:` — matches template indent level
    for line in lines {
        match line {
            EnvLine::Key(key) => {
                out.push_str(indent);
                out.push_str("- key: ");
                out.push_str(key);
                out.push('\n');
                out.push_str(indent);
                out.push_str("  value: \"\"\n");
                if is_secret_key(key) {
                    out.push_str(indent);
                    out.push_str("  type: SECRET\n");
                    out.push_str(indent);
                    out.push_str("  scope: RUN_AND_BUILD_TIME\n");
                } else {
                    out.push_str(indent);
                    out.push_str("  scope: RUN_TIME\n");
                }
            }
            EnvLine::Blank => {
                out.push('\n');
            }
            EnvLine::Comment => {
                // Comments in .env.example are dropped; blank-line separators
                // carry the grouping into the generated YAML.
            }
        }
    }
    while out.ends_with('\n') {
        out.pop();
    }
    out
}

fn render_workers_block(workers: &[String]) -> String {
    if workers.is_empty() {
        // SCOPE §4: emit a commented example so users see the shape.
        return "\
# workers: (one entry per non-test/dev/debug [[bin]] other than the service)
# workers:
#   - name: example-worker
#     dockerfile_path: Dockerfile
#     source_dir: /
#     run_command: /usr/local/bin/example-worker
#     instance_size_slug: apps-s-1vcpu-0.5gb
#     instance_count: 1
"
        .to_string();
    }

    let mut out = String::from(
        "# workers: (one entry per non-test/dev/debug [[bin]] other than the service)\nworkers:\n",
    );
    for name in workers {
        out.push_str(&format!(
            "  - name: {name}\n    dockerfile_path: Dockerfile\n    source_dir: /\n    run_command: /usr/local/bin/{name}\n    instance_size_slug: apps-s-1vcpu-0.5gb\n    instance_count: 1\n"
        ));
    }
    out
}

/// Sanitize a package name to a DigitalOcean-compatible app name.
///
/// Kept because DO rejects otherwise-valid names with a cryptic remote
/// error; local sanitization gives a fast-fail with a clear reason. This is
/// the only sanitizer surviving Phase 122.2.
pub fn sanitize_do_app_name(package_name: &str) -> String {
    let lowered = package_name.to_lowercase();
    let mut out = String::with_capacity(lowered.len());
    for c in lowered.chars() {
        if c.is_ascii_lowercase() || c.is_ascii_digit() {
            out.push(c);
        } else if c == '-' || c == '_' || c == ' ' {
            out.push('-');
        }
        // Other chars stripped.
    }
    // Collapse runs of dashes.
    let mut collapsed = String::with_capacity(out.len());
    let mut prev_dash = false;
    for c in out.chars() {
        if c == '-' {
            if !prev_dash {
                collapsed.push(c);
            }
            prev_dash = true;
        } else {
            collapsed.push(c);
            prev_dash = false;
        }
    }
    collapsed.trim_matches('-').to_string()
}

/// Parse a git remote URL (HTTPS or SSH form) to `"owner/repo"`. GitHub only.
pub fn parse_git_remote(remote_url: &str) -> Option<String> {
    let url = remote_url.trim();
    let tail = if let Some(rest) = url.strip_prefix("https://github.com/") {
        rest
    } else if let Some(rest) = url.strip_prefix("git@github.com:") {
        rest
    } else {
        return None;
    };
    let tail = tail.strip_suffix(".git").unwrap_or(tail);
    let mut parts = tail.splitn(3, '/');
    let owner = parts.next()?;
    let repo = parts.next()?;
    if owner.is_empty() || repo.is_empty() {
        return None;
    }
    Some(format!("{owner}/{repo}"))
}

/// Heuristic: filter test/dev/debug bins from the workers block.
///
/// This is the ONLY surviving heuristic after the Phase 122.2 simplification.
/// Kept because the alternative (explicit workers list in
/// `[package.metadata.ferro.deploy]`) would duplicate the `[[bin]]` entries
/// the user already wrote in Cargo.toml. See Phase 122.2 SCOPE §4.
pub fn is_test_like_bin(name: &str) -> bool {
    const PREFIXES: &[&str] = &["test_", "test-", "dev_", "dev-", "debug_", "debug-"];
    PREFIXES.iter().any(|p| name.starts_with(p))
}

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

    fn ctx(name: &str, repo: &str, workers: Vec<&str>, envs: Vec<&str>) -> AppYamlContext {
        AppYamlContext {
            name: name.to_string(),
            repo: repo.to_string(),
            web_bin: name.to_string(),
            workers: workers.into_iter().map(String::from).collect(),
            env_lines: Some(
                envs.into_iter()
                    .map(|k| EnvLine::Key(k.to_string()))
                    .collect(),
            ),
            preserved_name: None,
            preserved_region: None,
            preserved_github_repo: None,
            preserved_github_branch: None,
        }
    }

    pub(super) fn ctx_without_env(name: &str, repo: &str) -> AppYamlContext {
        AppYamlContext {
            name: name.to_string(),
            repo: repo.to_string(),
            web_bin: name.to_string(),
            workers: Vec::new(),
            env_lines: None,
            preserved_name: None,
            preserved_region: None,
            preserved_github_repo: None,
            preserved_github_branch: None,
        }
    }

    #[test]
    fn sanitize_simple_passthrough() {
        assert_eq!(sanitize_do_app_name("gestiscilo"), "gestiscilo");
    }

    #[test]
    fn sanitize_lowercases_and_replaces_underscores_and_spaces() {
        assert_eq!(sanitize_do_app_name("My_Cool App"), "my-cool-app");
    }

    #[test]
    fn sanitize_collapses_dashes() {
        assert_eq!(sanitize_do_app_name("foo__bar"), "foo-bar");
        assert_eq!(sanitize_do_app_name("foo---bar"), "foo-bar");
    }

    #[test]
    fn sanitize_strips_non_alphanum() {
        assert_eq!(sanitize_do_app_name("X!@#"), "x");
    }

    #[test]
    fn parse_git_remote_https_with_dot_git() {
        assert_eq!(
            parse_git_remote("https://github.com/owner/repo.git"),
            Some("owner/repo".to_string())
        );
    }

    #[test]
    fn parse_git_remote_https_no_dot_git() {
        assert_eq!(
            parse_git_remote("https://github.com/owner/repo"),
            Some("owner/repo".to_string())
        );
    }

    #[test]
    fn parse_git_remote_ssh_with_dot_git() {
        assert_eq!(
            parse_git_remote("git@github.com:owner/repo.git"),
            Some("owner/repo".to_string())
        );
    }

    #[test]
    fn parse_git_remote_ssh_no_dot_git() {
        assert_eq!(
            parse_git_remote("git@github.com:owner/repo"),
            Some("owner/repo".to_string())
        );
    }

    #[test]
    fn parse_git_remote_rejects_non_github() {
        assert_eq!(parse_git_remote("https://gitlab.com/x/y"), None);
    }

    #[test]
    fn is_test_like_bin_matches_prefixes() {
        for n in [
            "test_foo",
            "test-foo",
            "dev_foo",
            "dev-foo",
            "debug_foo",
            "debug-foo",
        ] {
            assert!(is_test_like_bin(n), "expected {n} to be test-like");
        }
    }

    #[test]
    fn is_test_like_bin_rejects_normal_names() {
        for n in ["web", "worker", "screenshot-worker", "api"] {
            assert!(!is_test_like_bin(n));
        }
    }

    #[test]
    fn render_app_yaml_contains_static_fields() {
        let c = ctx("myapp", "owner/repo", vec![], vec![]);
        let out = render_app_yaml(&c);
        assert!(out.starts_with("# Generated by ferro do:init — edit to your needs"));
        assert!(out.contains("name: myapp"));
        assert!(out.contains("region: fra1"));
        assert!(out.contains("repo: owner/repo"));
        assert!(out.contains("branch: main"));
        assert!(out.contains("services:"));
        assert!(out.contains("name: web"));
        assert!(out.contains("envs:"));
        assert!(!out.contains("databases:"));
    }

    #[test]
    fn render_app_yaml_with_empty_workers_emits_commented_example() {
        let c = ctx("a", "o/r", vec![], vec![]);
        let out = render_app_yaml(&c);
        assert!(out.contains("# workers:"));
        assert!(out.contains("# workers: (one entry per"));
    }

    #[test]
    fn render_app_yaml_emits_each_worker() {
        let c = ctx(
            "a",
            "o/r",
            vec!["screenshot-worker", "queue-worker"],
            vec![],
        );
        let out = render_app_yaml(&c);
        assert!(out.contains("workers:\n"));
        assert!(out.contains("- name: screenshot-worker"));
        assert!(out.contains("run_command: /usr/local/bin/screenshot-worker"));
        assert!(out.contains("- name: queue-worker"));
        assert!(out.contains("run_command: /usr/local/bin/queue-worker"));
    }

    #[test]
    fn render_app_yaml_emits_real_envs_entries() {
        let c = ctx(
            "a",
            "o/r",
            vec![],
            vec!["APP_ENV", "APP_URL", "DATABASE_URL"],
        );
        let out = render_app_yaml(&c);
        assert!(out.contains("- key: APP_ENV"));
        assert!(out.contains("- key: APP_URL"));
        assert!(out.contains("- key: DATABASE_URL"));
        assert!(!out.contains("# - APP_ENV"));
    }
}

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

    #[test]
    fn envs_block_from_env_example() {
        let src = "DATABASE_URL=\nSTRIPE_SECRET_KEY=\nAPP_NAME=\n";
        let out = render_envs_block(src);
        assert!(out.contains("- key: DATABASE_URL"));
        assert!(out.contains("- key: STRIPE_SECRET_KEY"));
        assert!(out.contains("- key: APP_NAME"));
    }

    #[test]
    fn secret_scope_and_type() {
        let src = "DATABASE_URL=\nSTRIPE_SECRET_KEY=\nAPP_NAME=\n";
        let out = render_envs_block(src);

        let stripe_idx = out.find("- key: STRIPE_SECRET_KEY").unwrap();
        let stripe_rest = &out[stripe_idx..];
        // Slice up to the next `- key: ` (or end of string).
        let stripe_end = stripe_rest[1..]
            .find("- key: ")
            .map(|i| i + 1)
            .unwrap_or(stripe_rest.len());
        let stripe_slice = &stripe_rest[..stripe_end];
        assert!(
            stripe_slice.contains("type: SECRET"),
            "STRIPE_SECRET_KEY must have type: SECRET, got: {stripe_slice}"
        );
        assert!(
            stripe_slice.contains("scope: RUN_AND_BUILD_TIME"),
            "STRIPE_SECRET_KEY must have scope: RUN_AND_BUILD_TIME"
        );

        let db_idx = out.find("- key: DATABASE_URL").unwrap();
        let db_rest = &out[db_idx..];
        let db_end = db_rest[1..]
            .find("- key: ")
            .map(|i| i + 1)
            .unwrap_or(db_rest.len());
        let db_slice = &db_rest[..db_end];
        assert!(
            !db_slice.contains("type: SECRET"),
            "DATABASE_URL must NOT have type: SECRET"
        );
        assert!(
            db_slice.contains("scope: RUN_TIME"),
            "DATABASE_URL must have scope: RUN_TIME"
        );
    }

    #[test]
    fn envs_preserve_source_order() {
        let src = "Z_NAME=\nA_NAME=\nM_NAME=\n";
        let out = render_envs_block(src);
        let z = out.find("Z_NAME").unwrap();
        let a = out.find("A_NAME").unwrap();
        let m = out.find("M_NAME").unwrap();
        assert!(z < a && a < m);
    }

    #[test]
    fn envs_preserve_blank_separators() {
        let src = "A_NAME=\n\nB_NAME=\n";
        let out = render_envs_block(src);
        let a = out.find("- key: A_NAME").unwrap();
        let b = out.find("- key: B_NAME").unwrap();
        assert!(
            out[a..b].contains("\n\n"),
            "expected blank line separator between A_NAME and B_NAME"
        );
    }
}

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

    #[test]
    fn web_service_has_no_run_command() {
        let c = AppYamlContext {
            name: "x".into(),
            repo: "o/r".into(),
            web_bin: "x".into(),
            workers: Vec::new(),
            env_lines: Some(vec![EnvLine::Key("APP_NAME".into())]),
            preserved_name: None,
            preserved_region: None,
            preserved_github_repo: None,
            preserved_github_branch: None,
        };
        let out = render_app_yaml(&c);
        // Locate the web service block (between `services:` and the workers block).
        let services_idx = out.find("services:").expect("services: block");
        let workers_idx = out[services_idx..]
            .find("# workers:")
            .map(|i| services_idx + i)
            .unwrap_or(out.len());
        let web_block = &out[services_idx..workers_idx];
        assert!(
            !web_block.contains("run_command:"),
            "web service must not set run_command (D-05), got: {web_block}"
        );
    }

    #[test]
    fn web_service_has_entrypoint_comment() {
        let c = AppYamlContext {
            name: "x".into(),
            repo: "o/r".into(),
            web_bin: "x".into(),
            workers: Vec::new(),
            env_lines: Some(vec![EnvLine::Key("APP_NAME".into())]),
            preserved_name: None,
            preserved_region: None,
            preserved_github_repo: None,
            preserved_github_branch: None,
        };
        let out = render_app_yaml(&c);
        assert!(
            out.contains("Dockerfile ENTRYPOINT"),
            "expected inline comment pointing at Dockerfile ENTRYPOINT"
        );
    }

    #[test]
    fn envs_missing_env_example_emits_empty_block() {
        let c = ctx_without_env("x", "o/r");
        let out = render_app_yaml(&c);
        assert!(
            !out.contains("- key: "),
            "expected empty envs block when .env.example missing"
        );
        // The `envs:` header itself still renders.
        assert!(out.contains("envs:"));
    }
}