unarchiver 0.0.2

CLI Tool for un/archive multiple formats of compressed files
Documentation
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 {
    /// Path to input file or directory
    pub path: PathBuf,
    /// Path to output files
    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(())
    }
}