Skip to main content

lux_cli/
unpack.rs

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