mrapids 0.1.7

Your OpenAPI, but executable
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
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#![warn(clippy::unwrap_used)]

mod cli;
mod core;
mod utils;

use anyhow::Result;
use clap::Parser;
use colored::*;
use std::env;
use std::path::PathBuf;
use std::process;

// Exit codes for consistent scripting
const EXIT_SUCCESS: i32 = 0;
const EXIT_UNKNOWN_ERROR: i32 = 1;
const EXIT_USAGE_ERROR: i32 = 2;
const EXIT_AUTH_ERROR: i32 = 3;
const EXIT_NETWORK_ERROR: i32 = 4;
const EXIT_RATE_LIMIT_ERROR: i32 = 5;
const EXIT_SERVER_ERROR: i32 = 6;
const EXIT_VALIDATION_ERROR: i32 = 7;
const EXIT_BREAKING_CHANGE: i32 = 8;

#[tokio::main]
async fn main() {
    let exit_code = match run().await {
        Ok(code) => code,
        Err(e) => {
            eprintln!("{}: {}", "Error".bright_red(), e);

            // Determine exit code based on error type
            if let Some(network_err) = e.downcast_ref::<reqwest::Error>() {
                if network_err.is_timeout() || network_err.is_connect() {
                    EXIT_NETWORK_ERROR
                } else if network_err.status() == Some(reqwest::StatusCode::TOO_MANY_REQUESTS) {
                    EXIT_RATE_LIMIT_ERROR
                } else if network_err.is_status() {
                    EXIT_SERVER_ERROR
                } else {
                    EXIT_UNKNOWN_ERROR
                }
            } else if e.to_string().to_lowercase().contains("auth")
                || e.to_string().to_lowercase().contains("authentication")
            {
                EXIT_AUTH_ERROR
            } else if e.to_string().to_lowercase().contains("validation")
                || e.to_string().to_lowercase().contains("invalid")
            {
                EXIT_VALIDATION_ERROR
            } else if e.to_string().to_lowercase().contains("breaking") {
                EXIT_BREAKING_CHANGE
            } else {
                EXIT_UNKNOWN_ERROR
            }
        }
    };

    process::exit(exit_code);
}

async fn run() -> Result<i32> {
    // Check if no arguments provided
    let args_count = env::args().count();
    if args_count == 1 {
        // Only the program name, no arguments
        core::display_banner();
        return Ok(EXIT_SUCCESS);
    }

    // Check for version flag early
    let args_vec: Vec<String> = env::args().collect();
    if args_vec.len() == 2 && (args_vec[1] == "--version" || args_vec[1] == "-V") {
        println!("{}", core::get_version_info());
        return Ok(EXIT_SUCCESS);
    }

    let args = match cli::Args::try_parse() {
        Ok(args) => args,
        Err(e) => {
            eprintln!("{}", e);
            return Ok(EXIT_USAGE_ERROR);
        }
    };

    // Apply global options
    if args.no_color {
        colored::control::set_override(false);
    }

    if args.trace {
        env::set_var("MRAPIDS_TRACE", "true");
    } else if args.verbose {
        env::set_var("MRAPIDS_VERBOSE", "true");
    }

    // Store global options in environment for commands to use
    if let Some(env_name) = &args.env {
        env::set_var("MRAPIDS_ENV", env_name);
    }
    if let Some(output) = &args.output_format {
        env::set_var("MRAPIDS_OUTPUT", output);
    }
    if args.quiet {
        env::set_var("MRAPIDS_QUIET", "true");
    }

    match args.command {
        cli::Commands::Init(cmd) => {
            core::init_command(cmd)?;
        }
        cli::Commands::Run(cmd) => {
            core::display_short_banner();
            core::run_command(cmd)?;
        }
        cli::Commands::Test(cmd) => {
            println!("{}", "🧪 Running tests...".bright_cyan());
            core::test_command(cmd)?;
        }
        cli::Commands::SetupTests(cmd) => {
            core::setup_tests_command(cmd)?;
        }
        cli::Commands::List(cmd) => {
            core::list_command(cmd)?;
        }
        cli::Commands::Show(cmd) => {
            core::show_command(cmd)?;
        }
        cli::Commands::Cleanup(cmd) => {
            core::cleanup_command(cmd)?;
        }
        cli::Commands::InitConfig(cmd) => {
            core::init_config_command(cmd)?;
        }
        cli::Commands::Explore(cmd) => {
            core::explore_command(cmd)?;
        }
        cli::Commands::Auth(cmd) => {
            handle_auth_command(cmd).await?;
        }
        cli::Commands::Flatten(cmd) => {
            core::flatten_command(cmd).await?;
        }
        cli::Commands::Validate(cmd) => {
            core::validate_command(cmd)?;
        }
        cli::Commands::Diff(cmd) => {
            core::diff_command(cmd)?;
        }
        cli::Commands::Gen(cmd) => {
            handle_gen_command(cmd).await?;
        }
        cli::Commands::Collection(cmd) => {
            handle_collection_command(cmd).await?;
        }
    }

    Ok(EXIT_SUCCESS)
}

async fn handle_gen_command(cmd: cli::GenCommand) -> Result<()> {
    use cli::GenTarget;

    match cmd.target {
        GenTarget::Snippets(snippets_cmd) => {
            // Call analyze_v2 directly
            println!("{}", "📝 Generating snippets...".bright_cyan());
            let analyze_cmd = cli::AnalyzeCommand {
                spec: snippets_cmd.spec,
                operation: snippets_cmd.operation,
                output: snippets_cmd.output,
                all: true,
                skip_data: false,
                skip_validate: false,
                force: true,
                cleanup_backups: true,
            };
            core::analyze_v2::analyze_command(analyze_cmd)?;
        }
        GenTarget::Sdk(sdk_cmd) => {
            // Call SDK generation directly
            println!("{}", "🔧 Generating SDK...".bright_cyan());
            let spec_path = sdk_cmd
                .spec
                .unwrap_or_else(|| PathBuf::from("specs/api.yaml"));
            let content = std::fs::read_to_string(&spec_path)?;
            let spec = core::parser::parse_spec(&content)?;

            let lang_str = match sdk_cmd.language {
                cli::SdkLanguage::Typescript => "typescript",
                cli::SdkLanguage::Python => "python",
                cli::SdkLanguage::Go => "go",
                cli::SdkLanguage::Rust => "rust",
            };

            let output_dir = sdk_cmd
                .output
                .unwrap_or_else(|| PathBuf::from(format!("./sdk-{}", lang_str)));

            // Generate SDK based on language
            match sdk_cmd.language {
                cli::SdkLanguage::Typescript => {
                    core::sdk_gen::typescript::generate_typescript_sdk(
                        &spec,
                        &output_dir,
                        sdk_cmd.package.as_deref(),
                        sdk_cmd.docs,
                        sdk_cmd.examples,
                    )?;
                }
                cli::SdkLanguage::Python => {
                    core::sdk_gen::python::generate_python_sdk(
                        &spec,
                        &output_dir,
                        sdk_cmd.package.as_deref(),
                        sdk_cmd.docs,
                        sdk_cmd.examples,
                    )?;
                }
                cli::SdkLanguage::Go => {
                    core::sdk_gen::go::generate_go_sdk(
                        &spec,
                        &output_dir,
                        sdk_cmd.package.as_deref(),
                        sdk_cmd.docs,
                        sdk_cmd.examples,
                    )?;
                }
                cli::SdkLanguage::Rust => {
                    core::sdk_gen::rust_gen::generate_rust_sdk(
                        &spec,
                        &output_dir,
                        sdk_cmd.package.as_deref(),
                        sdk_cmd.docs,
                        sdk_cmd.examples,
                    )?;
                }
            }

            println!("✅ SDK generated successfully in: {}", output_dir.display());
        }
        GenTarget::Stubs(stubs_cmd) => {
            // Generate server stubs based on framework
            let spec_path = stubs_cmd
                .spec
                .unwrap_or_else(|| PathBuf::from("specs/api.yaml"));
            let content = std::fs::read_to_string(&spec_path)?;
            let spec = core::parser::parse_spec(&content)?;

            let output_dir = stubs_cmd
                .output
                .unwrap_or_else(|| PathBuf::from("./generated"));

            core::stubs::generate_server_stubs(
                &spec,
                &stubs_cmd.framework,
                &output_dir,
                stubs_cmd.with_tests,
                stubs_cmd.with_validation,
            )?;
        }
        GenTarget::Fixtures(fixtures_cmd) => {
            // Generate test fixtures
            let spec_path = fixtures_cmd
                .spec
                .unwrap_or_else(|| PathBuf::from("specs/api.yaml"));
            let content = std::fs::read_to_string(&spec_path)?;
            let spec = core::parser::parse_spec(&content)?;

            // Convert format string to enum
            let format = match fixtures_cmd.format {
                cli::FixtureFormat::Json => core::fixtures::FixtureFormat::Json,
                cli::FixtureFormat::Yaml => core::fixtures::FixtureFormat::Yaml,
                cli::FixtureFormat::Csv => core::fixtures::FixtureFormat::Csv,
            };

            // For now, default to valid variant
            let variant = core::fixtures::FixtureVariant::Valid;

            core::fixtures::generate_fixtures(
                &spec,
                &fixtures_cmd.output,
                fixtures_cmd.count,
                fixtures_cmd.schema,
                fixtures_cmd.seed,
                format,
                variant,
            )?;
        }
    }

    Ok(())
}

async fn handle_auth_command(cmd: cli::AuthCommand) -> Result<()> {
    use crate::core::auth::{
        delete_profile, list_profiles, load_tokens, oauth_login, refresh_tokens, test_auth_profile,
    };
    use cli::AuthCommands;

    match cmd.command {
        AuthCommands::Login {
            provider,
            client_id,
            client_secret,
            auth_url,
            token_url,
            scopes,
            profile,
            setup_help,
        } => {
            if setup_help {
                println!(
                    "{}",
                    crate::core::auth::providers::get_provider_help(&provider)
                );
                return Ok(());
            }

            let profile_name = profile.unwrap_or_else(|| provider.clone());

            // Check if profile already exists
            if crate::core::auth::token_store::profile_exists(&profile_name) {
                println!("⚠️  Profile '{}' already exists. Use 'mrapids auth logout {}' first to remove it.", 
                    profile_name.bright_yellow(), profile_name);
                return Ok(());
            }

            let config = if provider.to_lowercase() == "custom" {
                // Custom provider requires all fields
                if client_id.is_none() || auth_url.is_none() || token_url.is_none() {
                    anyhow::bail!(
                        "Custom provider requires --client-id, --auth-url, and --token-url"
                    );
                }

                crate::core::auth::providers::create_custom_config(
                    &profile_name,
                    client_id.unwrap(),
                    client_secret,
                    auth_url.unwrap(),
                    token_url.unwrap(),
                    if scopes.is_empty() {
                        vec!["read".to_string()]
                    } else {
                        scopes
                    },
                )
            } else {
                // Known provider
                let mut config = crate::core::auth::providers::get_provider_config(&provider)?;

                // Override with provided values if any
                if let Some(id) = client_id {
                    config.client_id = id;
                }
                if let Some(secret) = client_secret {
                    config.client_secret = Some(secret);
                }
                if !scopes.is_empty() {
                    config.scopes = scopes;
                }

                // Check for default placeholders
                if config.client_id.starts_with("YOUR_") {
                    println!("⚠️  {} OAuth setup required:", provider.bright_yellow());
                    println!(
                        "{}",
                        crate::core::auth::providers::get_provider_help(&provider)
                    );
                    return Ok(());
                }

                config
            };

            oauth_login(config, profile_name).await?;
        }

        AuthCommands::List { detailed } => {
            let profiles = list_profiles()?;

            if profiles.is_empty() {
                println!(
                    "No auth profiles found. Use 'mrapids auth login <provider>' to create one."
                );
                return Ok(());
            }

            if detailed {
                use prettytable::{row, Table};
                let mut table = Table::new();
                table.add_row(row!["Profile", "Provider", "Created", "Last Used"]);

                for profile in profiles {
                    let last_used = profile
                        .last_used
                        .map(|dt| dt.format("%Y-%m-%d %H:%M").to_string())
                        .unwrap_or_else(|| "Never".to_string());

                    table.add_row(row![
                        profile.name.bright_green(),
                        profile.provider.bright_cyan(),
                        profile.created_at.format("%Y-%m-%d %H:%M"),
                        last_used
                    ]);
                }

                table.printstd();
            } else {
                println!("🔐 Auth Profiles:\n");
                for profile in profiles {
                    println!(
                        "{} ({})",
                        profile.name.bright_green(),
                        profile.provider.bright_cyan()
                    );
                }
                println!("\nUse 'mrapids auth list --detailed' for more information.");
            }
        }

        AuthCommands::Show {
            profile,
            show_tokens,
        } => {
            let auth_profile = crate::core::auth::token_store::load_profile(&profile)?;
            let provider_config = crate::core::auth::providers::load_provider_config(&profile)?;

            println!("🔐 Auth Profile: {}\n", profile.bright_green());
            println!("  Provider: {}", auth_profile.provider.bright_cyan());
            println!(
                "  Created: {}",
                auth_profile.created_at.format("%Y-%m-%d %H:%M")
            );
            if let Some(last_used) = auth_profile.last_used {
                println!("  Last Used: {}", last_used.format("%Y-%m-%d %H:%M"));
            }
            println!("  Scopes: {}", provider_config.scopes.join(", "));

            if show_tokens {
                println!("\n⚠️  {}:", "Token Information (SENSITIVE)".bright_red());
                let tokens = load_tokens(&profile)?;
                println!("  Token Type: {}", tokens.token_type);
                println!(
                    "  Access Token: {}...{}",
                    &tokens.access_token[..10.min(tokens.access_token.len())],
                    &tokens.access_token[tokens.access_token.len().saturating_sub(10)..]
                );
                if let Some(expires_at) = tokens.expires_at {
                    let remaining = expires_at.signed_duration_since(chrono::Utc::now());
                    if remaining.num_seconds() > 0 {
                        println!("  Expires In: {} minutes", remaining.num_minutes());
                    } else {
                        println!("  Status: {} (refresh required)", "EXPIRED".bright_red());
                    }
                }
                println!(
                    "  Has Refresh Token: {}",
                    if tokens.refresh_token.is_some() {
                        "Yes"
                    } else {
                        "No"
                    }
                );
            }
        }

        AuthCommands::Refresh { profile } => {
            refresh_tokens(&profile).await?;
        }

        AuthCommands::Logout { profile, force } => {
            if !force {
                println!(
                    "Are you sure you want to remove auth profile '{}'? [y/N] ",
                    profile.bright_yellow()
                );
                use std::io::{self, BufRead};
                let stdin = io::stdin();
                let mut lines = stdin.lock().lines();
                if let Some(Ok(line)) = lines.next() {
                    if !line.trim().eq_ignore_ascii_case("y") {
                        println!("Cancelled.");
                        return Ok(());
                    }
                }
            }

            delete_profile(&profile)?;
            println!(
                "✅ Auth profile '{}' removed successfully.",
                profile.bright_green()
            );
        }

        AuthCommands::Test { profile } => {
            test_auth_profile(&profile).await?;
        }

        AuthCommands::Setup { provider } => {
            println!(
                "{}",
                crate::core::auth::providers::get_provider_help(&provider)
            );
        }
    }

    Ok(())
}

async fn handle_collection_command(cmd: cli::CollectionCommand) -> Result<()> {
    use cli::CollectionSubcommand;
    use mrapids::collections::{
        find_collection, list_collections, parse_collection, validate_collection,
        CollectionExecutor, ConsoleReporter, ExecutionOptions,
    };
    use mrapids::core::parser::parse_spec;
    use serde_json::json;
    use std::collections::HashMap;

    match cmd.command {
        CollectionSubcommand::List { dir } => {
            let collections = list_collections(&dir)?;

            if collections.is_empty() {
                println!("No collections found in {:?}", dir);
                println!("\n💡 Create a collection YAML file in this directory to get started.");
            } else {
                println!("📚 Available collections:\n");
                for path in collections {
                    if let Some(name) = path.file_stem() {
                        println!("{}", name.to_string_lossy().bright_cyan());
                    }
                }
                println!("\nRun 'mrapids collection show <name>' for details");
            }
        }

        CollectionSubcommand::Show { name, dir } => {
            let path = find_collection(&dir, &name)?;
            let collection = parse_collection(&path)?;

            println!("📋 Collection: {}", collection.name.bright_cyan().bold());
            if let Some(desc) = &collection.description {
                println!("   {}", desc.dimmed());
            }
            println!("\n🔗 Requests ({}):", collection.requests.len());

            for (i, request) in collection.requests.iter().enumerate() {
                println!(
                    "   {}. {}{}",
                    i + 1,
                    request.name.bright_green(),
                    request.operation.dimmed()
                );
            }

            if !collection.variables.is_empty() {
                println!("\n📝 Variables:");
                for (key, value) in &collection.variables {
                    println!(
                        "   {} = {}",
                        key.bright_yellow(),
                        serde_json::to_string(value).unwrap_or_else(|_| "?".to_string())
                    );
                }
            }

            if let Some(auth) = &collection.auth_profile {
                println!("\n🔐 Auth Profile: {}", auth.bright_magenta());
            }
        }

        CollectionSubcommand::Validate {
            name,
            dir,
            spec: spec_path,
        } => {
            let path = find_collection(&dir, &name)?;
            let collection = parse_collection(&path)?;

            // Load spec if provided
            let spec = if let Some(spec_path) = spec_path {
                let content = std::fs::read_to_string(&spec_path)?;
                Some(parse_spec(&content)?)
            } else {
                None
            };

            let result = validate_collection(&collection, spec.as_ref());

            if result.is_valid() {
                println!("✅ Collection '{}' is valid!", name.bright_green());
            } else {
                println!("❌ Collection '{}' has errors:", name.bright_red());
                for error in &result.errors {
                    println!("{}", error.red());
                }
            }

            if !result.warnings.is_empty() {
                println!("\n⚠️  Warnings:");
                for warning in &result.warnings {
                    println!("{}", warning.yellow());
                }
            }
        }

        CollectionSubcommand::Run {
            name,
            dir,
            output,
            save_all,
            save_summary,
            variables,
            auth_profile,
            continue_on_error,
            requests,
            skip_requests,
            use_env,
            env_file,
            spec: spec_path,
            env: _,
        } => {
            let path = find_collection(&dir, &name)?;
            let collection = parse_collection(&path)?;

            // Load spec
            let spec_path = spec_path.unwrap_or_else(|| PathBuf::from("specs/api.yaml"));
            let spec_content = std::fs::read_to_string(&spec_path)?;
            let spec = parse_spec(&spec_content)?;

            // Build execution options
            let mut variable_map = HashMap::new();
            for (key, value) in variables {
                variable_map.insert(key, json!(value));
            }

            let options = ExecutionOptions {
                continue_on_error,
                skip_requests,
                only_requests: if requests.is_empty() {
                    None
                } else {
                    Some(requests)
                },
                auth_profile,
                variable_overrides: variable_map,
                use_env,
                env_file,
                save_all,
                save_summary,
            };

            // TODO: Load auth profile
            let auth = None;

            // Create executor and reporter
            let executor = CollectionExecutor::new(spec, auth);
            let mut reporter = ConsoleReporter::new(output != "json");

            // Execute collection
            let summary = executor
                .execute(&collection, options, &mut reporter)
                .await?;

            // Output JSON if requested
            if output == "json" {
                println!("{}", serde_json::to_string_pretty(&summary)?);
            }

            // Exit with error if any requests failed
            if summary.failed > 0 && !continue_on_error {
                std::process::exit(1);
            }
        }

        CollectionSubcommand::Test {
            name,
            dir,
            spec: spec_path,
            auth_profile,
            output,
            continue_on_error,
        } => {
            let path = find_collection(&dir, &name)?;
            let collection = parse_collection(&path)?;

            // Load spec
            let spec_path = spec_path.unwrap_or_else(|| PathBuf::from("specs/api.yaml"));
            let spec_content = std::fs::read_to_string(&spec_path)?;
            let spec = parse_spec(&spec_content)?;

            // Build execution options
            let options = ExecutionOptions {
                continue_on_error,
                skip_requests: vec![],
                only_requests: None,
                auth_profile,
                variable_overrides: HashMap::new(),
                use_env: false,
                env_file: None,
                save_all: None,
                save_summary: None,
            };

            // TODO: Load auth profile
            let auth = None;

            // Create executor
            let executor = CollectionExecutor::new(spec, auth);

            // Run as tests
            let test_results = executor.execute_as_tests(&collection, options).await?;

            // Output results based on format
            match output.as_str() {
                "json" => {
                    println!("{}", serde_json::to_string_pretty(&test_results)?);
                }
                "junit" => {
                    let junit_xml = mrapids::collections::testing::to_junit_xml(&test_results);
                    println!("{}", junit_xml);
                }
                _ => {
                    // Pretty print test results
                    mrapids::collections::testing::print_test_results(&test_results);
                }
            }

            // Exit with error if tests failed
            if !test_results.all_passed {
                std::process::exit(1);
            }
        }
    }

    Ok(())
}