use crate::cli::parser::GenerateArgs;
use crate::commands::base::{Command, CommandContext};
use anyhow::{Result, anyhow};
use rudof_lib::{Rudof, RudofConfig};
pub struct GenerateCommand {
args: GenerateArgs,
}
impl GenerateCommand {
pub fn new(args: GenerateArgs) -> Self {
Self { args }
}
}
impl Command for GenerateCommand {
fn name(&self) -> &'static str {
"generate"
}
fn execute(&self, _ctx: &mut CommandContext) -> Result<()> {
let runtime = tokio::runtime::Runtime::new().map_err(|e| anyhow!("Failed to create tokio runtime: {e}"))?;
runtime.block_on(async {
let rudof = Rudof::new(RudofConfig::default());
let schema_format = self.args.schema_format.into();
let result_format = self.args.result_format.into();
let entity_count = match (self.args.entity_count, &self.args.generator_config) {
(Some(n), _) => Some(n),
(None, Some(_)) => None,
(None, None) => Some(10),
};
let mut generation = rudof
.generate_data(&self.args.schema, &schema_format, entity_count)
.with_result_generation_format(&result_format);
if let Some(seed) = self.args.seed {
generation = generation.with_seed(seed);
}
if let Some(parallel) = self.args.parallel {
generation = generation.with_parallel(parallel);
}
if let Some(output) = &self.args.common.output {
generation = generation.with_output(output);
}
if let Some(generator_config) = &self.args.generator_config {
generation = generation.with_config_file(generator_config);
}
generation.execute().await
})?;
Ok(())
}
}