Skip to main content

upstream_rs/application/features/
export.rs

1use anyhow::Result;
2use console::style;
3use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
4use std::path::PathBuf;
5use std::time::Duration;
6
7use crate::{
8    application::operations::export_operation::ExportOperation,
9    services::storage::package_storage::PackageStorage, utils::static_paths::UpstreamPaths,
10};
11
12pub async fn run_export(path: PathBuf, full: bool) -> Result<()> {
13    let paths = UpstreamPaths::new();
14    let package_storage = PackageStorage::new(&paths.config.packages_file)?;
15    let export_op = ExportOperation::new(&package_storage, &paths);
16
17    let pb = ProgressBar::new(0);
18    pb.set_draw_target(ProgressDrawTarget::stderr_with_hz(10));
19    pb.set_style(ProgressStyle::with_template(
20        "{msg}\n{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})",
21    )?);
22    pb.enable_steady_tick(Duration::from_millis(120));
23
24    let pb_ref = &pb;
25
26    let mut progress_callback = Some(move |done: u64, total: u64| {
27        pb_ref.set_length(total);
28        pb_ref.set_position(done);
29    });
30
31    let mut message_callback = Some(move |msg: &str| {
32        pb_ref.set_message(msg.to_string());
33    });
34
35    if full {
36        println!("{}", style("Creating full snapshot ...").cyan());
37
38        export_op.export_snapshot(&path, &mut progress_callback, &mut message_callback)?;
39
40        pb.set_position(pb.length().unwrap_or(0));
41        pb.finish_with_message("Snapshot complete");
42
43        println!(
44            "{}",
45            style(format!("Snapshot complete: saved to '{}'.", path.display())).green()
46        );
47    } else {
48        println!("{}", style("Exporting package manifest ...").cyan());
49
50        export_op.export_manifest(&path, &mut message_callback)?;
51
52        pb.finish_with_message("Manifest complete");
53
54        println!(
55            "{}",
56            style(format!("Manifest complete: saved to '{}'.", path.display())).green()
57        );
58    }
59
60    Ok(())
61}