libroast/operations/
raw.rs

1// SPDX-License-Identifier: MPL-2.0
2
3// Copyright (C) 2025 Soc Virnyl Estela and contributors
4
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9use crate::{
10    decompress,
11    operations::cli,
12    utils::{
13        is_supported_format,
14        process_globs,
15        start_tracing,
16    },
17};
18use std::io;
19#[allow(unused_imports)]
20use tracing::{
21    Level,
22    debug,
23    error,
24    info,
25    trace,
26    warn,
27};
28
29/// Extracts a tarball as long as it is a supported mime-type. Arguments
30/// passed are based on `crate::cli::RawArgs`.
31pub fn raw_opts(raw_args: cli::RawArgs, start_trace: bool) -> io::Result<()>
32{
33    if start_trace
34    {
35        start_tracing();
36    }
37
38    info!("🥩 Starting Raw.");
39
40    let target_path = process_globs(&raw_args.target)?;
41    let target_path = target_path.canonicalize().unwrap_or(target_path);
42    if target_path.is_file()
43    {
44        match is_supported_format(&raw_args.target)
45        {
46            Ok(target) => match target
47            {
48                crate::common::SupportedFormat::Compressed(mime_type, src) =>
49                {
50                    info!(?mime_type);
51                    let outpath =
52                        raw_args.outdir.unwrap_or(std::env::current_dir().inspect_err(|e| {
53                            error!(?e, "Unable to determine current directory!");
54                        })?);
55                    match mime_type
56                    {
57                        crate::common::Compression::Gz =>
58                        {
59                            decompress::targz(&outpath, &src)?;
60                        }
61                        crate::common::Compression::Xz =>
62                        {
63                            decompress::tarxz(&outpath, &src)?;
64                        }
65                        crate::common::Compression::Zst | crate::common::Compression::Zstd =>
66                        {
67                            decompress::tarzst(&outpath, &src)?;
68                        }
69                        crate::common::Compression::Bz2 =>
70                        {
71                            decompress::tarbz2(&outpath, &src)?;
72                        }
73                        crate::common::Compression::Not =>
74                        {
75                            decompress::vanilla(&outpath, &src)?;
76                        }
77                    }
78                    info!("🥩 You have extracted your source at {}", outpath.display());
79                    Ok(())
80                }
81                crate::common::SupportedFormat::Dir(_) =>
82                {
83                    unreachable!("This should never be a directory since we already checked it!")
84                }
85            },
86            Err(err) =>
87            {
88                eprintln!("{}", err);
89                error!(?err);
90                Err(io::Error::new(io::ErrorKind::Unsupported, err.to_string()))
91            }
92        }
93    }
94    else
95    {
96        let err = io::Error::new(io::ErrorKind::Unsupported, "Directory detected.");
97        error!(?err);
98        Err(err)
99    }
100}