oag 0.20.6

CLI for the oag OpenAPI code generator
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
mod self_update;

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

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use clap_complete::Shell;

use oag_core::GeneratedFile;
use oag_core::config::{self, CONFIG_FILE_NAME, LEGACY_CONFIG_FILE, OagConfig};
use oag_core::engine;
use oag_core::engine::pack::TemplatePack;
use oag_core::engine::resolve;
use oag_core::ir::IrSpec;
use oag_core::parse;
use oag_core::transform::{self, TransformOptions};

mod github;

/// Resolve a pack, with extends support.
///
/// Resolution order:
/// 1. If no `@ref` and a locally installed pack exists → use it
/// 2. Otherwise → fetch from GitHub (cached by `{pack_id}/{ref}`)
fn resolve_pack(specifier: &str) -> Result<TemplatePack> {
    let (pack_id, git_ref) = github::parse_pack_specifier(specifier);
    let is_pinned = specifier.contains('@');

    let pack = if !is_pinned {
        // Check locally installed packs first
        if let Some(disk_path) = resolve::resolve_pack_path(pack_id, None) {
            TemplatePack::from_dir(&disk_path)
                .map_err(|e| anyhow::anyhow!("failed to load pack '{}': {}", pack_id, e))?
        } else {
            fetch_and_install(pack_id, git_ref)?
        }
    } else {
        fetch_and_install(pack_id, git_ref)?
    };

    // Handle extends
    if let Some(ref base_id) = pack.manifest.pack.extends {
        let mut base = resolve_pack(base_id)?;
        base.merge_from(&pack);
        Ok(base)
    } else {
        Ok(pack)
    }
}

/// Download a pack from GitHub and install it to `.oag/packs/<id>/`.
fn fetch_and_install(pack_id: &str, git_ref: &str) -> Result<TemplatePack> {
    let target = resolve::templates_dir().join(pack_id);

    ui::info(&format!("installing {pack_id}@{git_ref} from GitHub..."));
    github::download_pack(pack_id, git_ref, &target)?;
    ui::phase_ok("installed", Some(&format!("{pack_id}@{git_ref}")));

    TemplatePack::from_dir(&target)
        .map_err(|e| anyhow::anyhow!("failed to load pack '{}': {}", pack_id, e))
}

// ── UI helpers (all output to stderr) ────────────────────────────────

mod ui {
    use crossterm::style::Stylize;
    use std::io::{self, Write};

    pub fn header(cmd: &str) {
        let mut err = io::stderr();
        let _ = writeln!(err);
        let _ = writeln!(err, "  {}", cmd.cyan().bold());
        let _ = writeln!(err, "  {}", "\u{2500}".repeat(40).dim());
        let _ = writeln!(err);
    }

    pub fn phase_ok(msg: &str, detail: Option<&str>) {
        let mut err = io::stderr();
        let suffix = detail
            .map(|d| format!(" \u{00b7} {}", d.dim()))
            .unwrap_or_default();
        let _ = writeln!(err, "  {} {msg}{suffix}", "\u{2713}".green().bold());
    }

    pub fn warn(msg: &str) {
        let mut err = io::stderr();
        let _ = writeln!(err, "  {} {}", "\u{26a0}".yellow().bold(), msg.yellow());
    }

    pub fn info(msg: &str) {
        let mut err = io::stderr();
        let _ = writeln!(err, "  {} {}", "\u{2139}".cyan(), msg.dim());
    }
}

#[derive(Parser)]
#[command(name = "oag", about = "OpenAPI 3.x code generator", version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Generate code from an OpenAPI spec using oag.yaml configuration
    Generate {
        /// Path to the OpenAPI spec file (YAML or JSON). Overrides the `input` field in the config.
        input: Option<PathBuf>,

        /// Overwrite scaffold files (package.json, tsconfig.json, etc.) even if they already exist.
        /// By default, scaffold files are only written on initial creation to preserve customizations.
        #[arg(long)]
        force_scaffold: bool,
    },

    /// Validate an OpenAPI spec and report its contents (paths, schemas, operations)
    Validate {
        /// Path to the OpenAPI spec file (YAML or JSON)
        input: PathBuf,
    },

    /// Dump the parsed intermediate representation (IR) as JSON for debugging
    Inspect {
        /// Path to the OpenAPI spec file (YAML or JSON)
        input: PathBuf,
    },

    /// Create an oag.yaml config file and download packs from GitHub
    Init {
        /// Overwrite an existing oag.yaml file
        #[arg(long)]
        force: bool,

        /// Pack IDs to download (e.g., node-client, fastapi-server)
        #[arg(short, long)]
        pack: Vec<String>,
    },

    /// Run validators (lint, typecheck) on generated output directories
    Check,

    /// Generate shell completion scripts for tab-completion (bash, zsh, fish, powershell, elvish)
    Completions {
        /// Target shell for completions
        shell: Shell,
    },

    /// Manage packs for code generation
    Packs {
        #[command(subcommand)]
        action: PacksAction,
    },

    /// Self-update oag to the latest release
    Update,
    /// Print the current version
    Version,
}

#[derive(Subcommand)]
enum PacksAction {
    /// List installed packs
    List,
    /// Install a pack from a local directory or download from GitHub
    Install {
        /// Path to a local pack directory
        source: Option<PathBuf>,
        /// Download a pack by ID from GitHub (e.g., node-client)
        #[arg(long)]
        id: Option<String>,
    },
    /// Remove an installed pack
    Remove {
        /// Pack ID to remove
        id: String,
    },
}

fn main() -> Result<()> {
    env_logger::init();

    let cli = Cli::parse();

    match cli.command {
        Commands::Generate {
            input,
            force_scaffold,
        } => cmd_generate(input, force_scaffold),
        Commands::Check => cmd_check(),
        Commands::Validate { input } => cmd_validate(input),
        Commands::Inspect { input } => cmd_inspect(input),
        Commands::Init { force, pack } => cmd_init(force, pack),
        Commands::Completions { shell } => {
            let mut cmd = <Cli as clap::CommandFactory>::command();
            clap_complete::generate(shell, &mut cmd, "oag", &mut std::io::stdout());
            Ok(())
        }
        Commands::Packs { action } => cmd_packs(action),
        Commands::Update => cmd_update(),
        Commands::Version => {
            println!("oag v{}", env!("CARGO_PKG_VERSION"));
            Ok(())
        }
    }
}

/// Try to load the project config file from the current directory.
fn try_load_config() -> Result<Option<OagConfig>> {
    match config::find_config(Path::new(".")) {
        Some((path, is_legacy)) => {
            if is_legacy {
                ui::warn(&format!(
                    "{} is deprecated, rename to {} (legacy support will be removed in a future release)",
                    LEGACY_CONFIG_FILE, CONFIG_FILE_NAME,
                ));
            }
            config::load_config(&path).map_err(|e| anyhow::anyhow!(e))
        }
        None => Ok(None),
    }
}

fn load_spec(path: &PathBuf, cfg: &OagConfig) -> Result<IrSpec> {
    let content =
        fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;

    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("yaml");

    let parsed = match ext {
        "json" => parse::from_json(&content)?,
        _ => parse::from_yaml(&content)?,
    };

    let options = TransformOptions {
        naming_strategy: cfg.naming.strategy,
        aliases: cfg.naming.aliases.clone(),
    };

    let ir = transform::transform_with_options(&parsed, &options)?;
    Ok(ir)
}

/// Write generated files to disk under the given base directory.
fn write_files(base: &Path, files: &[GeneratedFile]) -> Result<()> {
    for file in files {
        let path = base.join(&file.path);
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("failed to create directory {}", parent.display()))?;
        }
        fs::write(&path, &file.content)
            .with_context(|| format!("failed to write {}", path.display()))?;
        ui::phase_ok("wrote", Some(&path.display().to_string()));
    }
    Ok(())
}

/// Write scaffold files, skipping any that already exist on disk (unless `force` is true).
/// Returns the number of files that were skipped.
fn write_scaffold_files(base: &Path, files: &[GeneratedFile], force: bool) -> Result<usize> {
    let mut skipped = 0;
    for file in files {
        let path = base.join(&file.path);
        if !force && path.exists() {
            ui::info(&format!("skipped (exists): {}", path.display()));
            skipped += 1;
            continue;
        }
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("failed to create directory {}", parent.display()))?;
        }
        fs::write(&path, &file.content)
            .with_context(|| format!("failed to write {}", path.display()))?;
        ui::phase_ok("wrote", Some(&path.display().to_string()));
    }
    Ok(skipped)
}

/// Try to run formatters based on pack's formatter config or file presence.
fn try_run_formatter(output_dir: &Path, pack: &TemplatePack) {
    for fmt_config in pack.manifest.formatters.values() {
        if output_dir.join(&fmt_config.detect).exists() {
            let parts: Vec<&str> = fmt_config.command.split_whitespace().collect();
            if parts.is_empty() {
                continue;
            }
            match Command::new(parts[0])
                .args(&parts[1..])
                .current_dir(output_dir)
                .output()
            {
                Ok(result) if result.status.success() => {
                    ui::phase_ok(&format!("ran: {}", fmt_config.command), None);
                }
                Ok(_) => {
                    ui::warn(&format!(
                        "{} had issues (non-zero exit)",
                        fmt_config.command
                    ));
                }
                Err(_) => {
                    ui::info(&format!(
                        "{} not found — run `{}` in {} to format",
                        parts[0],
                        fmt_config.command,
                        output_dir.display()
                    ));
                }
            }
        }
    }
}

/// Generate the "do not edit" README.
fn readme_content() -> &'static str {
    r#"# Generated Code — Do Not Edit

This directory is **auto-generated** by [oag](https://github.com/urmzd/oag).
Any manual changes will be overwritten the next time `oag generate` is run.

To regenerate, run:
```
oag generate
```

To customize the generated output, edit your `oag.yaml` configuration file.
"#
}

fn cmd_generate(input: Option<PathBuf>, force_scaffold: bool) -> Result<()> {
    let cfg = try_load_config()?.unwrap_or_default();
    let input = input.unwrap_or_else(|| PathBuf::from(&cfg.input));
    let ir = load_spec(&input, &cfg)?;

    if cfg.generators.is_empty() {
        ui::warn("No generators configured. Add a `generators` section to your config.");
        return Ok(());
    }

    for (gen_id, gen_config) in &cfg.generators {
        ui::header(&format!(
            "Generating {} \u{2192} {}",
            gen_id, gen_config.output
        ));

        let pack = resolve_pack(gen_id.as_str())?;
        let output = engine::generate(&ir, gen_config, &pack).map_err(|e| anyhow::anyhow!(e))?;

        let output_dir = PathBuf::from(&gen_config.output);
        fs::create_dir_all(&output_dir).with_context(|| {
            format!("failed to create output directory {}", output_dir.display())
        })?;

        // Source files are always overwritten
        write_files(&output_dir, &output.source_files)?;

        // Scaffold files are write-once by default (skip if they exist)
        let skipped = write_scaffold_files(&output_dir, &output.scaffold_files, force_scaffold)?;

        // Add README.md
        let readme_path = output_dir.join("README.md");
        fs::write(&readme_path, readme_content())
            .with_context(|| format!("failed to write {}", readme_path.display()))?;
        ui::phase_ok("wrote", Some(&readme_path.display().to_string()));

        // Auto-run formatters
        try_run_formatter(&output_dir, &pack);

        let total = output.source_files.len() + output.scaffold_files.len() + 1;
        let skip_note = if skipped > 0 {
            format!(", {skipped} scaffold skipped")
        } else {
            String::new()
        };
        ui::phase_ok(
            &format!("generated {total} files{skip_note}"),
            Some(&output_dir.display().to_string()),
        );
    }

    ui::info(
        "source files are always regenerated \u{2014} scaffold files are preserved if they exist",
    );
    Ok(())
}

fn cmd_check() -> Result<()> {
    let cfg = try_load_config()?.unwrap_or_default();

    if cfg.generators.is_empty() {
        ui::warn("No generators configured.");
        return Ok(());
    }

    let mut failures = Vec::new();

    for (gen_id, gen_config) in &cfg.generators {
        let pack = resolve_pack(gen_id.as_str())?;
        let output_dir = PathBuf::from(&gen_config.output);

        if !output_dir.exists() {
            ui::warn(&format!(
                "{} does not exist — skipping",
                output_dir.display()
            ));
            continue;
        }

        for (name, val_config) in &pack.manifest.validators {
            if !output_dir.join(&val_config.detect).exists() {
                continue;
            }

            ui::header(&format!("{gen_id}: {name}"));

            if let Some(setup) = &val_config.setup {
                let setup_parts: Vec<&str> = setup.split_whitespace().collect();
                if !setup_parts.is_empty() {
                    match Command::new(setup_parts[0])
                        .args(&setup_parts[1..])
                        .current_dir(&output_dir)
                        .status()
                    {
                        Ok(s) if s.success() => {}
                        Ok(_) => {
                            ui::warn(&format!("setup failed: {setup}"));
                            failures.push(format!("{gen_id}: {name} (setup)"));
                            continue;
                        }
                        Err(e) => {
                            ui::warn(&format!("{} not found: {e}", setup_parts[0]));
                            failures.push(format!("{gen_id}: {name} (setup)"));
                            continue;
                        }
                    }
                }
            }

            let parts: Vec<&str> = val_config.command.split_whitespace().collect();
            if parts.is_empty() {
                continue;
            }

            match Command::new(parts[0])
                .args(&parts[1..])
                .current_dir(&output_dir)
                .status()
            {
                Ok(status) if status.success() => {
                    ui::phase_ok(&format!("passed: {}", val_config.command), None);
                }
                Ok(_) => {
                    ui::warn(&format!("failed: {}", val_config.command));
                    failures.push(format!("{gen_id}: {name}"));
                }
                Err(e) => {
                    ui::warn(&format!("{} not found: {e}", parts[0]));
                    failures.push(format!("{gen_id}: {name}"));
                }
            }
        }
    }

    if !failures.is_empty() {
        anyhow::bail!("validation failed for: {}", failures.join(", "));
    }

    ui::phase_ok("all validators passed", None);
    Ok(())
}

fn cmd_validate(input: PathBuf) -> Result<()> {
    let content = fs::read_to_string(&input)
        .with_context(|| format!("failed to read {}", input.display()))?;

    let ext = input.extension().and_then(|e| e.to_str()).unwrap_or("yaml");

    let parsed = match ext {
        "json" => parse::from_json(&content)?,
        _ => parse::from_yaml(&content)?,
    };

    ui::header(&format!("Validate \u{00b7} {}", parsed.info.title));

    ui::info(&format!("OpenAPI {}", parsed.openapi));
    ui::info(&format!("Version: {}", parsed.info.version));
    ui::info(&format!("Paths: {}", parsed.paths.len()));

    if let Some(ref components) = parsed.components {
        ui::info(&format!("Schemas: {}", components.schemas.len()));
    }

    let ir = transform::transform(&parsed)?;
    ui::info(&format!("Operations: {}", ir.operations.len()));
    ui::info(&format!("IR Schemas: {}", ir.schemas.len()));

    ui::phase_ok("validation successful", None);
    Ok(())
}

fn cmd_inspect(input: PathBuf) -> Result<()> {
    let cfg = OagConfig::default();
    let ir = load_spec(&input, &cfg)?;

    let summary = build_inspect_summary(&ir);
    let json = serde_json::to_string_pretty(&summary)?;
    println!("{}", json);

    Ok(())
}

fn build_inspect_summary(ir: &IrSpec) -> serde_json::Value {
    let schemas: Vec<serde_json::Value> = ir
        .schemas
        .iter()
        .map(|s| {
            serde_json::json!({
                "name": s.name().pascal_case,
                "kind": match s {
                    oag_core::ir::IrSchema::Object(_) => "object",
                    oag_core::ir::IrSchema::Enum(_) => "enum",
                    oag_core::ir::IrSchema::Alias(_) => "alias",
                    oag_core::ir::IrSchema::Union(_) => "union",
                },
            })
        })
        .collect();

    let operations: Vec<serde_json::Value> = ir
        .operations
        .iter()
        .map(|op| {
            let return_kind = match &op.return_type {
                oag_core::ir::IrReturnType::Standard(_) => "standard",
                oag_core::ir::IrReturnType::Sse(_) => "sse",
                oag_core::ir::IrReturnType::Void => "void",
            };
            serde_json::json!({
                "name": op.name.camel_case,
                "method": op.method.as_str(),
                "path": op.path,
                "return_kind": return_kind,
                "tags": op.tags,
            })
        })
        .collect();

    serde_json::json!({
        "info": {
            "title": ir.info.title,
            "version": ir.info.version,
        },
        "schemas": schemas,
        "operations": operations,
        "modules": ir.modules.iter().map(|m| &m.name.original).collect::<Vec<_>>(),
    })
}

fn cmd_init(force: bool, packs: Vec<String>) -> Result<()> {
    let config_path = PathBuf::from(CONFIG_FILE_NAME);

    if config_path.exists() && !force {
        anyhow::bail!(
            "{} already exists. Use --force to overwrite.",
            config_path.display()
        );
    }

    fs::write(&config_path, config::default_config_content())?;
    ui::phase_ok("created", Some(&config_path.display().to_string()));

    // Download requested packs
    for pack_id in &packs {
        install_pack_from_github(pack_id)?;
    }

    Ok(())
}

/// Download a pack from GitHub and install it locally, resolving `extends` dependencies.
fn install_pack_from_github(specifier: &str) -> Result<()> {
    let (pack_id, git_ref) = github::parse_pack_specifier(specifier);
    let templates_dir = resolve::templates_dir();
    let target = templates_dir.join(pack_id);

    github::download_pack(pack_id, git_ref, &target)?;
    ui::phase_ok("installed", Some(&format!("{pack_id}@{git_ref}")));

    // If pack extends another, download the base too
    let pack = TemplatePack::from_dir(&target).map_err(|e| anyhow::anyhow!(e))?;
    if let Some(ref base_id) = pack.manifest.pack.extends {
        let base_target = templates_dir.join(base_id.as_str());
        if !base_target.join("oag.pack.toml").exists() {
            install_pack_from_github(base_id)?;
        }
    }

    Ok(())
}

fn cmd_update() -> Result<()> {
    eprintln!("current version: {}", env!("CARGO_PKG_VERSION"));
    match self_update::self_update("urmzd/oag", env!("CARGO_PKG_VERSION"), "oag")? {
        self_update::UpdateResult::AlreadyUpToDate => eprintln!("already up to date"),
        self_update::UpdateResult::Updated { from, to } => eprintln!("updated: {from}{to}"),
    }
    Ok(())
}

fn cmd_packs(action: PacksAction) -> Result<()> {
    match action {
        PacksAction::List => {
            let installed = resolve::list_installed_packs();
            if installed.is_empty() {
                ui::info("No packs installed.");
            } else {
                ui::header("Installed packs");
                for (id, path) in &installed {
                    ui::phase_ok(id, Some(&path.display().to_string()));
                }
            }

            let installed_ids: Vec<&str> = installed.iter().map(|(id, _)| id.as_str()).collect();
            ui::header("Available packs (download with `oag packs install --id <pack>`)");
            for &pack_id in github::KNOWN_PACKS {
                let marker = if installed_ids.contains(&pack_id) {
                    " (installed)"
                } else {
                    ""
                };
                ui::phase_ok(pack_id, Some(marker));
            }
            Ok(())
        }
        PacksAction::Install { source, id } => {
            if let Some(pack_id) = id {
                install_pack_from_github(&pack_id)?;
                Ok(())
            } else if let Some(source_path) = source {
                let pack_toml = source_path.join("oag.pack.toml");
                if !pack_toml.exists() {
                    anyhow::bail!("no oag.pack.toml found in {}", source_path.display());
                }
                let pack = TemplatePack::from_dir(&source_path).map_err(|e| anyhow::anyhow!(e))?;
                let id = pack.manifest.pack.id.clone();
                let target =
                    resolve::install_pack(&source_path, &id).map_err(|e| anyhow::anyhow!(e))?;
                ui::phase_ok("installed", Some(&format!("{id}{}", target.display())));
                Ok(())
            } else {
                anyhow::bail!("provide a source path or use --id <pack_id>");
            }
        }
        PacksAction::Remove { id } => {
            resolve::remove_pack(&id).map_err(|e| anyhow::anyhow!(e))?;
            ui::phase_ok("removed", Some(&id));
            Ok(())
        }
    }
}