Skip to main content

lux_cli/
unpack.rs

1use lux_lib::package;
2use std::{fs::File, io::Cursor, path::PathBuf};
3
4use clap::Args;
5use lux_lib::{config::Config, operations};
6
7use miette::{IntoDiagnostic, Result};
8
9#[derive(Args)]
10pub struct Unpack {
11    /// A path to a .src.rock file. Usually obtained via `lux download`.
12    path: PathBuf,
13    /// Where to unpack the rock.
14    destination: Option<PathBuf>,
15}
16
17#[derive(Args)]
18pub struct UnpackRemote {
19    pub package_req: package::Req,
20    /// The directory to unpack to
21    pub destination: Option<PathBuf>,
22}
23
24pub async fn unpack(data: Unpack, _config: Config) -> Result<()> {
25    let destination = data.destination.unwrap_or_else(|| {
26        PathBuf::from(data.path.to_string_lossy().trim_end_matches(".src.rock"))
27    });
28    let src_file = File::open(data.path).into_diagnostic()?;
29
30    let unpack_path = lux_lib::operations::unpack_src_rock(src_file, destination).await?;
31    tracing::info!("unpacked rock to: {}", unpack_path.display());
32
33    Ok(())
34}
35
36pub async fn unpack_remote(data: UnpackRemote, config: Config) -> Result<()> {
37    let package_req = data.package_req;
38    let rock = operations::Download::new(&package_req, &config)
39        .search_and_download_src_rock()
40        .await?;
41    let cursor = Cursor::new(rock.bytes);
42
43    let destination = data
44        .destination
45        .unwrap_or_else(|| PathBuf::from(format!("{}-{}", rock.name, rock.version)));
46
47    let unpack_path = lux_lib::operations::unpack_src_rock(cursor, destination).await?;
48    tracing::info!("unpacked rock to: {}", unpack_path.display());
49
50    Ok(())
51}