aboutsummaryrefslogtreecommitdiff
path: root/src/command/publish_archive.rs
blob: 96dda861fdc31a9b515a6cfddd969e512629a87a (plain)
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
use crate::configuration::Configuration;
use std::io::{Error, ErrorKind::Other, Result};
use std::process::{Command, Stdio};

const COMMAND: &str = "rsync";

pub struct PublishArchive;

impl PublishArchive {
    pub fn new() -> Self {
        PublishArchive
    }
}

impl super::Command for PublishArchive {
    fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![]
    }

    fn execute(
        &self,
        input: Option<&String>,
        configuration: &Configuration,
        _: &str,
    ) -> Result<()> {
        let input = input.ok_or_else(|| {
            Error::new(Other, "You must provide a location to publish the archive")
        })?;

        Command::new(COMMAND)
            .arg("--version")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map_err(|_| Error::new(Other, "Publishing requires rsync"))?;

        Command::new(COMMAND)
            .arg("-r")
            .arg(format!(
                "{}/",
                &configuration.archive_output_directory.display()
            ))
            .arg(input.as_str())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map_err(|_| Error::new(Other, "Rsync failed to publish."))?;
        Ok(())
    }

    fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![]
    }

    fn command(&self) -> &'static str {
        "publish-archive"
    }

    fn help(&self) -> &'static str {
        "<destination>\tPublishes the archive to a remote host"
    }
}