1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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,
            });
        }

        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
        )
    }
}