use anyhow::Result;
use clap::Args;
use gen_models::sample::Sample;
use crate::{
commands::{cli_context::CliContext, get_default_collection},
graphs::combinatorial_library::parse_library,
updates::library::update_with_library,
};
#[derive(Debug, Args, Clone)]
pub struct Command {
#[arg(short, long)]
name: Option<String>,
#[arg(short, long, default_value_t = Sample::DEFAULT_NAME.to_string())]
sample: String,
#[arg(long)]
new_sample: String,
#[arg(short, long)]
path_name: String,
#[arg(long)]
start: i64,
#[arg(short, long)]
end: i64,
#[arg(short, long)]
library: String,
#[arg(long)]
parts: String,
}
pub fn execute(cli_context: &CliContext, cmd: Command) -> Result<()> {
println!("Update with library called");
let context = cli_context.context;
let operation_conn = context.operations().conn();
let conn = context.graph().conn();
conn.execute("BEGIN TRANSACTION", [])?;
operation_conn.execute("BEGIN TRANSACTION", [])?;
let name = &cmd
.name
.clone()
.unwrap_or_else(|| get_default_collection(operation_conn));
let parts_list = parse_library(&cmd.parts, &cmd.library)?;
if let Err(err) = update_with_library(
context,
name,
cmd.sample.as_str(),
&cmd.new_sample,
&cmd.path_name,
cmd.start,
cmd.end,
parts_list,
Some(&cmd.parts),
Some(&cmd.library),
) {
conn.execute("ROLLBACK TRANSACTION;", [])?;
operation_conn.execute("ROLLBACK TRANSACTION;", [])?;
return Err(err.into());
}
conn.execute("END TRANSACTION;", [])?;
operation_conn.execute("END TRANSACTION;", [])?;
println!("Updated with library file: {0}", cmd.library);
Ok(())
}