use clap::{Parser, Subcommand, ValueEnum};
#[derive(Parser, Debug)]
#[command(
name = "seedkit",
about = "Generate realistic, constraint-safe seed data for any database",
version,
after_help = "Examples:\n seedkit generate --db postgres://localhost/myapp --rows 1000 --output seed.sql\n seedkit generate --rows 100 # auto-detect DB from .env\n seedkit introspect --db postgres://localhost/myapp\n seedkit preview --db postgres://localhost/myapp\n seedkit check --db postgres://localhost/myapp\n seedkit graph --db postgres://localhost/myapp --format mermaid"
)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
#[arg(short, long, global = true)]
pub verbose: bool,
}
#[derive(Subcommand, Debug)]
pub enum Command {
Generate(GenerateArgs),
Introspect(IntrospectArgs),
Preview(PreviewArgs),
Check(CheckArgs),
Graph(GraphArgs),
Sample(SampleArgs),
Reset(ResetArgs),
}
#[derive(Parser, Debug)]
pub struct GenerateArgs {
#[command(flatten)]
pub options: GenerationOptions,
#[arg(short, long)]
pub output: Option<String>,
#[arg(long)]
pub format: Option<OutputFormat>,
#[arg(long)]
pub copy: bool,
}
#[derive(clap::Args, Debug, Clone, Default)]
pub struct SchemaFile {
#[arg(long, value_name = "PATH", requires = "dialect")]
pub schema_file: Option<std::path::PathBuf>,
#[arg(long, value_enum, requires = "schema_file")]
pub dialect: Option<Dialect>,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Dialect {
#[value(alias = "postgresql")]
Postgres,
Mysql,
Sqlite,
}
#[derive(Parser, Debug)]
pub struct GenerationOptions {
#[arg(long, env = "DATABASE_URL")]
pub db: Option<String>,
#[arg(long, conflicts_with = "from_lock")]
pub rows: Option<usize>,
#[arg(long, conflicts_with = "from_lock")]
pub seed: Option<u64>,
#[arg(long, conflicts_with = "from_lock")]
pub scenario: Option<String>,
#[arg(
long,
value_delimiter = ',',
value_name = "TABLE=COUNT",
value_parser = parse_table_rows_entry,
conflicts_with = "from_lock"
)]
pub table_rows: Vec<(String, usize)>,
#[arg(long, value_delimiter = ',', conflicts_with = "from_lock")]
pub include: Vec<String>,
#[arg(long, value_delimiter = ',', conflicts_with = "from_lock")]
pub exclude: Vec<String>,
#[arg(long)]
pub schema: Option<String>,
#[command(flatten)]
pub schema_file: SchemaFile,
#[arg(long)]
pub ai: bool,
#[arg(long)]
pub model: Option<String>,
#[arg(long)]
pub from_lock: bool,
#[arg(long)]
pub force: bool,
#[arg(long)]
pub subset: Option<String>,
#[arg(long, value_name = "LOCALE", conflicts_with = "from_lock")]
pub locale: Option<seedkit_core::generate::locale::Locale>,
}
#[derive(Parser, Debug)]
pub struct ResetArgs {
#[command(flatten)]
pub options: GenerationOptions,
#[arg(long, short = 'y')]
pub yes: bool,
}
#[derive(Parser, Debug)]
pub struct IntrospectArgs {
#[arg(long, env = "DATABASE_URL")]
pub db: Option<String>,
#[arg(long)]
pub schema: Option<String>,
#[command(flatten)]
pub schema_file: SchemaFile,
#[arg(long, default_value = "table")]
pub format: IntrospectFormat,
}
#[derive(Parser, Debug)]
pub struct PreviewArgs {
#[command(flatten)]
pub options: GenerationOptions,
#[arg(long)]
pub limit: Option<usize>,
}
#[derive(Parser, Debug)]
pub struct CheckArgs {
#[arg(long, env = "DATABASE_URL")]
pub db: Option<String>,
#[arg(long)]
pub schema: Option<String>,
#[command(flatten)]
pub schema_file: SchemaFile,
#[arg(long, default_value = "text")]
pub format: CheckFormat,
}
#[derive(Parser, Debug)]
pub struct GraphArgs {
#[arg(long, env = "DATABASE_URL")]
pub db: Option<String>,
#[arg(long)]
pub schema: Option<String>,
#[command(flatten)]
pub schema_file: SchemaFile,
#[arg(long, default_value = "mermaid")]
pub format: GraphFormat,
}
#[derive(Parser, Debug)]
pub struct SampleArgs {
#[arg(long, env = "DATABASE_URL")]
pub db: Option<String>,
#[arg(long)]
pub schema: Option<String>,
#[arg(short, long)]
pub output: Option<String>,
#[arg(long, value_delimiter = ',')]
pub tables: Vec<String>,
#[arg(long, value_name = "TABLE.COLUMN", value_parser = parse_sample_column)]
pub allow_text: Vec<(String, String)>,
#[arg(long, default_value = "50")]
pub categorical_limit: usize,
#[arg(long, default_value = "10")]
pub min_rows: u64,
}
#[derive(Debug, Clone, ValueEnum)]
pub enum OutputFormat {
Sql,
Json,
Csv,
}
#[derive(Debug, Clone, ValueEnum)]
pub enum IntrospectFormat {
Table,
Json,
}
#[derive(Debug, Clone, ValueEnum)]
pub enum CheckFormat {
Text,
Json,
}
#[derive(Debug, Clone, ValueEnum)]
pub enum GraphFormat {
Mermaid,
Dot,
}
fn parse_table_rows_entry(value: &str) -> Result<(String, usize), String> {
value
.split_once('=')
.filter(|(table, _)| !table.is_empty())
.and_then(|(table, count)| Some((table.to_owned(), count.parse().ok()?)))
.ok_or_else(|| "expected TABLE=COUNT, e.g. users=500".to_owned())
}
fn parse_sample_column(value: &str) -> Result<(String, String), String> {
let (table, column) = value
.split_once('.')
.filter(|(t, c)| !t.is_empty() && !c.is_empty())
.ok_or("expected table.column")?;
Ok((table.to_owned(), column.to_owned()))
}
impl GenerateArgs {
pub fn csv_directory(&self) -> Option<&std::path::Path> {
let path = self.output.as_deref().filter(|p| *p != "direct")?;
let is_dir = path.ends_with('/')
|| path.ends_with(std::path::MAIN_SEPARATOR)
|| std::path::Path::new(path).is_dir();
is_dir.then(|| std::path::Path::new(path))
}
pub fn output_format(&self) -> OutputFormat {
if let Some(ref fmt) = self.format {
return fmt.clone();
}
if let Some(ref path) = self.output {
if path.ends_with(".json") {
return OutputFormat::Json;
} else if path.ends_with(".csv") {
return OutputFormat::Csv;
}
}
OutputFormat::Sql
}
}
impl GenerationOptions {
pub fn parse_table_rows(&self) -> std::collections::BTreeMap<String, usize> {
self.table_rows.iter().cloned().collect()
}
}