use log::{debug, info};
use std::env;
use crate::asynchronous::execute_borg;
use crate::common::{extract_fmt_args, extract_parse_output, CommonOptions, ExtractOptions};
use crate::errors::ExtractError;
pub async fn extract(
options: &ExtractOptions,
common_options: &CommonOptions,
) -> Result<(), ExtractError> {
let local_path = common_options.local_path.as_ref().map_or("borg", |x| x);
let original_dir = env::current_dir().map_err(|e| {
ExtractError::CommandFailed(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to get current directory: {}", e),
))
})?;
let result = extract_in_destination(local_path, options, common_options).await;
env::set_current_dir(original_dir).map_err(|e| {
ExtractError::CommandFailed(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to restore original directory: {}", e),
))
})?;
result?;
info!("Finished extracting archive");
Ok(())
}
async fn extract_in_destination(
local_path: &str,
options: &ExtractOptions,
common_options: &CommonOptions,
) -> Result<(), ExtractError> {
env::set_current_dir(&options.destination).map_err(|e| {
ExtractError::CommandFailed(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to change to destination directory: {}", e),
))
})?;
let args = extract_fmt_args(options, common_options);
debug!("Calling borg: {local_path} {args}");
let args = shlex::split(&args).ok_or(ExtractError::ShlexError)?;
let res = execute_borg(local_path, args, &options.passphrase).await?;
extract_parse_output(res)?;
Ok(())
}