use std::path::PathBuf;
use anyhow::Result;
use clap::Args;
use gen_models::{errors::OperationError, sample::Sample};
use crate::{
commands::{cli_context::CliContext, get_default_collection},
imports::gfa::{GFAImportError, import_gfa},
};
#[derive(Debug, Args, Clone)]
pub struct Command {
#[clap(index = 1)]
pub path: String,
#[arg(short, long)]
name: Option<String>,
#[arg(short, long, default_value_t = Sample::DEFAULT_NAME.to_string())]
sample: String,
}
pub fn execute(cli_context: &CliContext, cmd: Command) -> Result<()> {
println!("GFA import called");
let context = cli_context.context;
let operation_conn = context.operations().conn();
let conn = context.graph().conn();
conn.execute("BEGIN TRANSACTION", []).unwrap();
operation_conn.execute("BEGIN TRANSACTION", []).unwrap();
let name = &cmd
.name
.clone()
.unwrap_or_else(|| get_default_collection(operation_conn));
match import_gfa(
context,
&PathBuf::from(cmd.path.clone()),
name,
cmd.sample.as_str(),
) {
Ok(_) => {
println!("GFA imported.");
conn.execute("END TRANSACTION;", []).unwrap();
operation_conn.execute("END TRANSACTION;", []).unwrap();
Ok(())
}
Err(GFAImportError::OperationError(OperationError::NoChanges)) => {
conn.execute("ROLLBACK TRANSACTION;", []).unwrap();
operation_conn.execute("ROLLBACK TRANSACTION;", []).unwrap();
println!("GFA already exists.");
Ok(())
}
Err(e) => {
conn.execute("ROLLBACK TRANSACTION;", []).unwrap();
operation_conn.execute("ROLLBACK TRANSACTION;", []).unwrap();
println!("Import failed.");
Err(e.into())
}
}
}