8000 add unarchive atom by martintc · Pull Request #355 · comtrya/comtrya · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add unarchive atom #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ trust-dns-resolver = "0.23.2"
walkdir = "2.3"
which = "5.0"
whoami = "1.4"
tar = "0.4.40"
flate2 = "1.0.28"

[target.'cfg(unix)'.dependencies]
uzers = "0.11"
Expand Down
2 changes: 2 additions & 0 deletions lib/src/atoms/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod create;
mod decrypt;
mod link;
mod remove;
mod unarchive;

use super::Atom;
pub use chmod::Chmod;
Expand All @@ -16,6 +17,7 @@ pub use create::Create;
pub use decrypt::Decrypt;
pub use link::Link;
pub use remove::Remove;
pub use unarchive::Unarchive;

pub trait FileAtom: Atom {
// Don't think this is needed? Validate soon
Expand Down
65 changes: 65 additions & 0 deletions lib/src/atoms/file/unarchive.rs
55C7
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::{fs::File, path::PathBuf};

use flate2::read::GzDecoder;
use tar::Archive;

use crate::atoms::{Atom, Outcome};

use super::FileAtom;

pub struct Unarchive {
pub origin: PathBuf,
pub dest: PathBuf,
pub force: bool,
}

impl FileAtom for Unarchive {
fn get_path(&self) -> &PathBuf {
&self.origin
}
}

impl Atom for Unarchive {
// Determine if this atom needs to run
fn plan(&self) -> anyhow::Result<Outcome> {
if self.dest.exists() {
if self.force {
return Ok(Outcome {
side_effects: vec![],
should_run: self.origin.exists(),
});
}
return Ok(Outcome {
side_effects: vec![],
should_run: false,
});
}

return Ok(Outcome {
side_effects: vec![],
should_run: self.origin.exists(),
});
}

// Apply new to old
fn execute(&mut self) -> anyhow::Result<()> {
let tar_gz = File::open(&self.origin)?;
let tar = GzDecoder::new(tar_gz);
let mut archive = Archive::new(tar);
archive.unpack(&self.dest)?;
Ok(())
}
}

impl std::fmt::Display for Unarchive {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let origin_path = self.origin.display().to_string();
let dest_path = self.dest.display().to_string();

write!(
f,
"The archive {} to be decompressed to {}",
origin_path, dest_path
)
}
}
0