use crate::config::{Config, LogLevel};
use crate::transformer::TransformConfig;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "tinyetl")]
#[command(about = "A tiny ETL tool for moving data between sources")]
#[command(version)]
#[command(args_conflicts_with_subcommands = true)]
#[command(subcommand_precedence_over_arg = true)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
pub source: Option<String>,
pub target: Option<String>,
#[arg(long, default_value = "true")]
pub infer_schema: bool,
#[arg(long, value_name = "FILE")]
pub schema_file: Option<String>,
#[arg(long, default_value = "10000")]
pub batch_size: usize,
#[arg(long, value_name = "N")]
pub preview: Option<usize>,
#[arg(long)]
pub dry_run: bool,
#[arg(long, default_value = "info")]
pub log_level: LogLevel,
#[arg(long)]
pub skip_existing: bool,
#[arg(long)]
pub truncate: bool,
#[arg(long, value_name = "FILE")]
pub transform_file: Option<String>,
#[arg(long, value_name = "EXPRESSIONS")]
pub transform: Option<String>,
#[arg(long, value_name = "TYPE")]
pub source_type: Option<String>,
#[arg(long, value_name = "ID")]
pub source_secret_id: Option<String>,
#[arg(long, value_name = "ID")]
pub dest_secret_id: Option<String>,
}
#[derive(Subcommand)]
pub enum Commands {
Run {
config_file: String,
},
GenerateDefaultConfig,
#[command(override_usage = "tinyetl generate-config [OPTIONS] <SOURCE> <TARGET>")]
GenerateConfig {
source: String,
target: String,
#[arg(long, default_value = "true")]
infer_schema: bool,
#[arg(long, value_name = "FILE")]
schema_file: Option<String>,
#[arg(long, default_value = "10000")]
batch_size: usize,
#[arg(long, value_name = "N")]
preview: Option<usize>,
#[arg(long)]
dry_run: bool,
#[arg(long, default_value = "info")]
log_level: LogLevel,
#[arg(long)]
skip_existing: bool,
#[arg(long)]
truncate: bool,
#[arg(long, value_name = "FILE")]
transform_file: Option<String>,
#[arg(long, value_name = "EXPRESSIONS")]
transform: Option<String>,
#[arg(long, value_name = "TYPE")]
source_type: Option<String>,
#[arg(long, value_name = "ID")]
source_secret_id: Option<String>,
#[arg(long, value_name = "ID")]
dest_secret_id: Option<String>,
},
}
impl Cli {
pub fn is_config_mode(&self) -> bool {
matches!(self.command, Some(Commands::Run { .. }))
}
pub fn is_generate_config_mode(&self) -> bool {
matches!(self.command, Some(Commands::GenerateConfig { .. }))
}
pub fn is_generate_default_config_mode(&self) -> bool {
matches!(self.command, Some(Commands::GenerateDefaultConfig))
}
pub fn get_config_file(&self) -> Option<&str> {
match &self.command {
Some(Commands::Run { config_file }) => Some(config_file),
_ => None,
}
}
pub fn has_direct_params(&self) -> bool {
self.source.is_some() && self.target.is_some()
}
}
impl From<Cli> for Config {
fn from(cli: Cli) -> Self {
let source = cli.source.expect("Source is required for direct CLI usage");
let target = cli.target.expect("Target is required for direct CLI usage");
let transform_config = match (&cli.transform_file, &cli.transform) {
(Some(file), None) => TransformConfig::File(file.clone()),
(None, Some(expressions)) => TransformConfig::Inline(expressions.clone()),
(Some(file), Some(_)) => {
eprintln!("Warning: Both --transform-file and --transform specified. Using --transform-file.");
TransformConfig::File(file.clone())
}
(None, None) => TransformConfig::None,
};
Config {
source,
target,
infer_schema: cli.infer_schema,
schema_file: cli.schema_file,
batch_size: cli.batch_size,
preview: cli.preview,
dry_run: cli.dry_run,
log_level: cli.log_level,
skip_existing: cli.skip_existing,
truncate: cli.truncate,
transform: transform_config,
source_type: cli.source_type,
source_secret_id: cli.source_secret_id,
dest_secret_id: cli.dest_secret_id,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
#[test]
fn test_basic_cli_parsing() {
let cli = Cli::try_parse_from(&["tinyetl", "source.csv", "target.db#table"]).unwrap();
assert_eq!(cli.source, Some("source.csv".to_string()));
assert_eq!(cli.target, Some("target.db#table".to_string()));
assert_eq!(cli.batch_size, 10000);
assert!(cli.infer_schema);
assert!(!cli.dry_run);
assert!(!cli.skip_existing);
assert!(cli.has_direct_params());
assert!(!cli.is_config_mode());
}
#[test]
fn test_config_file_parsing() {
let cli = Cli::try_parse_from(&["tinyetl", "run", "my_job.yaml"]).unwrap();
assert!(cli.is_config_mode());
assert_eq!(cli.get_config_file(), Some("my_job.yaml"));
assert!(!cli.has_direct_params());
}
#[test]
fn test_cli_with_options() {
let cli = Cli::try_parse_from([
"tinyetl",
"source.json",
"target.csv",
"--batch-size",
"5000",
"--preview",
"10",
"--dry-run",
"--log-level",
"warn",
"--skip-existing",
"--source-type",
"json",
])
.unwrap();
assert_eq!(cli.source, Some("source.json".to_string()));
assert_eq!(cli.target, Some("target.csv".to_string()));
assert_eq!(cli.batch_size, 5000);
assert_eq!(cli.preview, Some(10));
assert!(cli.dry_run);
assert!(cli.skip_existing);
assert!(matches!(cli.log_level, LogLevel::Warn));
assert_eq!(cli.source_type, Some("json".to_string()));
}
#[test]
fn test_cli_to_config_conversion() {
let cli = Cli::try_parse_from([
"tinyetl",
"input.csv",
"output.db#data",
"--batch-size",
"2000",
"--preview",
"5",
])
.unwrap();
let config: Config = cli.into();
assert_eq!(config.source, "input.csv");
assert_eq!(config.target, "output.db#data");
assert_eq!(config.batch_size, 2000);
assert_eq!(config.preview, Some(5));
}
#[test]
fn test_missing_arguments() {
let result = Cli::try_parse_from(["tinyetl"]);
assert!(result.is_ok());
}
#[test]
fn test_invalid_log_level() {
let result = Cli::try_parse_from([
"tinyetl",
"source.csv",
"target.db",
"--log-level",
"invalid",
]);
assert!(result.is_err());
}
#[test]
fn test_http_source_with_type() {
let cli = Cli::try_parse_from([
"tinyetl",
"https://example.com/api/data",
"output.csv",
"--source-type",
"json",
])
.unwrap();
assert_eq!(cli.source, Some("https://example.com/api/data".to_string()));
assert_eq!(cli.target, Some("output.csv".to_string()));
assert_eq!(cli.source_type, Some("json".to_string()));
let config: Config = cli.into();
assert_eq!(config.source_type, Some("json".to_string()));
}
#[test]
fn test_generate_default_config_parsing() {
let cli = Cli::try_parse_from(&["tinyetl", "generate-default-config"]).unwrap();
assert!(cli.is_generate_default_config_mode());
assert!(!cli.is_config_mode());
assert!(!cli.is_generate_config_mode());
assert!(!cli.has_direct_params());
}
}