libroast/operations/
raw.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::{
    decompress,
    operations::cli,
    utils::{
        is_supported_format,
        process_globs,
        start_tracing,
    },
};
use std::io;
#[allow(unused_imports)]
use tracing::{
    debug,
    error,
    info,
    trace,
    warn,
    Level,
};

pub fn raw_opts(raw_args: cli::RawArgs, start_trace: bool) -> io::Result<()>
{
    if start_trace
    {
        start_tracing();
    }

    info!("🥩 Starting Raw.");

    let target_path = process_globs(&raw_args.target)?;
    let target_path = target_path.canonicalize().unwrap_or(target_path);
    if target_path.is_file()
    {
        match is_supported_format(&raw_args.target)
        {
            Ok(target) => match target
            {
                crate::common::SupportedFormat::Compressed(mime_type, src) =>
                {
                    info!(?mime_type);
                    let outpath =
                        raw_args.outdir.unwrap_or(std::env::current_dir().inspect_err(|e| {
                            error!(?e, "Unable to determine current directory!");
                        })?);
                    match mime_type
                    {
                        crate::common::Compression::Gz =>
                        {
                            decompress::targz(&outpath, &src)?;
                        }
                        crate::common::Compression::Xz =>
                        {
                            decompress::tarxz(&outpath, &src)?;
                        }
                        crate::common::Compression::Zst =>
                        {
                            decompress::tarzst(&outpath, &src)?;
                        }
                        crate::common::Compression::Bz2 =>
                        {
                            decompress::tarbz2(&outpath, &src)?;
                        }
                        crate::common::Compression::Not =>
                        {
                            decompress::vanilla(&outpath, &src)?;
                        }
                    }
                    info!("🥩 You have extracted your source at {}", outpath.display());
                    Ok(())
                }
                crate::common::SupportedFormat::Dir(_) =>
                {
                    unreachable!("This should never be a directory since we already checked it!")
                }
            },
            Err(err) =>
            {
                eprintln!("{}", err);
                error!(?err);
                Err(io::Error::new(io::ErrorKind::Unsupported, err.to_string()))
            }
        }
    }
    else
    {
        let err = io::Error::new(io::ErrorKind::Unsupported, "Directory detected.");
        error!(?err);
        Err(err)
    }
}