Skip to main content

ferro_cli/commands/
db_seed.rs

1use console::style;
2use std::path::Path;
3use std::process::Command;
4
5pub fn run(class: Option<String>) {
6    // Check we're in a Ferro project
7    if !Path::new("src/seeders").exists() {
8        eprintln!(
9            "{} No seeders directory found at src/seeders",
10            style("Error:").red().bold()
11        );
12        eprintln!(
13            "{}",
14            style("Run 'ferro make:seeder <name>' to create your first seeder.").dim()
15        );
16        std::process::exit(1);
17    }
18
19    match &class {
20        Some(name) => {
21            println!(
22                "{} Running seeder: {}...",
23                style("->").cyan(),
24                style(name).bold()
25            );
26        }
27        None => {
28            println!("{} Running all seeders...", style("->").cyan());
29        }
30    }
31
32    // Build command arguments
33    let mut args = vec!["run", "--quiet", "--", "db:seed"];
34
35    let class_arg;
36    if let Some(name) = &class {
37        class_arg = format!("--class={name}");
38        args.push(&class_arg);
39    }
40
41    // Run cargo run -- db:seed [--class=Name] (unified binary)
42    let status = Command::new("cargo")
43        .args(&args)
44        .status()
45        .expect("Failed to execute cargo command");
46
47    if !status.success() {
48        eprintln!("{} Seeding failed", style("Error:").red().bold());
49        std::process::exit(1);
50    }
51}