use std::path::PathBuf;
use anyhow::{anyhow, Result};
use clap::Args;
use tokio::fs::canonicalize;
use unarchiver::{
archive::{Archive, ArchiveKind},
utils::file::output_pathname,
};
#[derive(Args, Debug)]
pub struct ArchiveOpt {
pub path: PathBuf,
pub out: Option<PathBuf>,
}
impl ArchiveOpt {
pub async fn exec(&self) -> Result<()> {
let kind = ArchiveKind::Tar;
let input_path = canonicalize(&self.path).await?;
let archive = Archive::new(&input_path, kind.clone()).unwrap();
let outpath = if let Some(out) = self.out.clone() {
canonicalize(out)
.await
.map_err(|err| anyhow!("Failed to canonicalize. {}", err))?
} else {
output_pathname(input_path, kind.default_extension()).await?
};
archive.archive(outpath).await.unwrap();
Ok(())
}
}