feat: implement unpacking

This commit is contained in:
2026-02-06 23:49:14 -08:00
parent a562005f6c
commit 4cc2efbf96
5 changed files with 45 additions and 21 deletions

View File

@@ -9,6 +9,7 @@ use crate::{
#[derive(Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub sync: Sync,
}
@@ -17,6 +18,14 @@ pub struct Sync {
pub fs_dir: Option<String>,
}
impl Default for Sync {
fn default() -> Self {
Self {
fs_dir: Some(base_path_local().to_string_lossy().into_owned()),
}
}
}
impl Config {
pub fn new() -> Result<Self> {
let path = base_path_local().join("config.toml");

View File

@@ -31,6 +31,8 @@ struct Cli {
enum Commands {
/// push current state to storage
Push,
/// update state with latest revision
Pull,
}
struct AppState {
@@ -68,6 +70,9 @@ fn run() -> crate::error::Result<()> {
Some(Commands::Push) => {
app_state.storage.store_revision(app_state.clone())?;
}
Some(Commands::Pull) => {
app_state.storage.unpack_revision(app_state.clone())?;
}
_ => Cli::command().print_long_help()?,
}

View File

@@ -1,4 +1,4 @@
use std::borrow::Cow;
use std::{borrow::Cow, time::Duration};
use indicatif::{HumanBytes, ProgressBar, ProgressDrawTarget, ProgressStyle};
@@ -19,7 +19,7 @@ impl Reporter {
.unwrap()
.tick_chars(TICK_CHARS),
);
spinner.enable_steady_tick(std::time::Duration::from_millis(100));
spinner.enable_steady_tick(Duration::from_millis(100));
spinner.set_draw_target(ProgressDrawTarget::stderr_with_hz(20));
Self { app_state, spinner }
}
@@ -57,6 +57,25 @@ impl Reporter {
self.spinner.finish_with_message(msg);
}
pub fn start_extract_progress(&self, operation: impl Into<Cow<'static, str>>) -> ProgressBar {
let bar = self.app_state.multi.add(ProgressBar::new_spinner());
bar.set_style(
ProgressStyle::with_template("{msg:8.214/yellow} {spinner} [{elapsed_precise}]")
.unwrap()
.tick_chars(TICK_CHARS),
);
bar.enable_steady_tick(Duration::from_millis(100));
bar.set_message(operation);
bar
}
pub fn finish_extract_progress(bar: &ProgressBar, msg: impl Into<Cow<'static, str>>) {
bar.set_style(
ProgressStyle::with_template("{msg:>12.green} ✓ [{elapsed_precise}]").unwrap(),
);
bar.finish_with_message(msg);
}
pub fn start_bytes_progress(
&self,
total_bytes: u64,

View File

@@ -9,7 +9,7 @@ use crate::Data;
use crate::archive::create_archive;
use crate::error::{Error, Result};
use crate::reporter::Reporter;
use crate::storage::StorageImpl;
use crate::storage::{StorageImpl, base_path_prism};
pub struct FSStorage;
@@ -111,17 +111,13 @@ impl StorageImpl for FSStorage {
Ok(archive)
}
fn unpack_revision(
&self,
app_state: Data,
destination: PathBuf,
revision: usize,
) -> Result<()> {
fn unpack_revision(&self, app_state: Data) -> Result<()> {
let revision = self.get_revision_metadata(app_state.clone())?;
let mut archive = self.retrieve_revision(app_state.clone(), revision)?;
let entries = archive.entries()?;
let reporter = Reporter::new(app_state);
let total_files = entries.progress_with_style
reporter.start_archive_progress(total);
let reporter =
Reporter::new(app_state.clone()).start_extract_progress("extracting revision");
let destination = base_path_prism();
for entry in entries {
let mut entry = entry?;
@@ -129,6 +125,7 @@ impl StorageImpl for FSStorage {
.unpack_in(&destination)
.map_err(|e| Error::unpack_error(e))?;
}
Reporter::finish_extract_progress(&reporter, "extracted revision");
Ok(())
}

View File

@@ -14,8 +14,7 @@ pub trait StorageImpl {
app_state: Data,
revision: usize,
) -> Result<tar::Archive<GzDecoder<BufReader<std::fs::File>>>>;
fn unpack_revision(&self, app_state: Data, destination: PathBuf, revision: usize)
-> Result<()>;
fn unpack_revision(&self, app_state: Data) -> Result<()>;
}
pub enum Storage {
@@ -47,14 +46,9 @@ impl StorageImpl for Storage {
}
}
fn unpack_revision(
&self,
app_state: Data,
destination: PathBuf,
revision: usize,
) -> Result<()> {
fn unpack_revision(&self, app_state: Data) -> Result<()> {
match self {
Self::FS(storage) => storage.unpack_revision(app_state, destination, revision),
Self::FS(storage) => storage.unpack_revision(app_state),
}
}
}