use std::io::Write;
use crate::command::_helpers::loader_commands::run_loader;
use crate::command::errors::CommandError;
use crate::command::models::{Command, LoaderArgs, LoaderRequest};
use crate::copy::constants::PROJECT_HEADER;
use crate::copy::main::copy_project::copy_project;
use crate::copy::models::CopyRequest;
pub(crate) async fn run_copy(
request: LoaderRequest<'_>,
) -> Result<(Vec<String>, u8), CommandError> {
let Command::Copy(args) = request.command else {
return Err("copy runs only for sqlload copy".to_owned().into());
};
let copy_request = CopyRequest {
source: args.source,
destination: args.destination,
write_strategy: args.write_strategy,
unique_key: args.unique_key,
cursor_column: args.cursor_column,
format: args.format,
};
let copy = copy_project(©_request, request.text)?;
for warning in ©.warnings {
let _ = writeln!(std::io::stderr(), "warning: {warning}");
}
if args.print_toml {
return Ok((vec![format!("{PROJECT_HEADER}{}", copy.printed)], 0));
}
run_loader(LoaderRequest {
text: ©.text,
config: request.config,
command: Command::Load(LoaderArgs::default()),
host: request.host,
no_color: request.no_color,
streams: request.streams,
log_json: request.log_json,
variables: copy.variables,
generated: true,
})
.await
}