]> git.r.bdr.sh - rbdr/blog/blame - src/command/publish_archive.rs
Improve the error handling
[rbdr/blog] / src / command / publish_archive.rs
CommitLineData
50f53dc4 1use std::io::{Error, ErrorKind::Other, Result};
a9c6be41 2use crate::configuration::Configuration;
6352ebb0
RBR
3use std::process::{Command, Stdio};
4
5const COMMAND: &str = "rsync";
d620665f
RBR
6
7pub struct PublishArchive;
8
9impl PublishArchive {
10 pub fn new() -> Self {
11 PublishArchive
12 }
13}
14
15impl super::Command for PublishArchive {
16 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
17 vec![]
18 }
19
6352ebb0
RBR
20 fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
21
50f53dc4
RBR
22 let input = input
23 .ok_or_else(|| Error::new(Other, "You must provide a location to publish the archive"))?;
6352ebb0
RBR
24
25 Command::new(COMMAND)
26 .arg("--version")
27 .stdout(Stdio::null())
28 .stderr(Stdio::null())
29 .status()
50f53dc4 30 .map_err(|_| Error::new(Other, "Publishing requires rsync"))?;
6352ebb0
RBR
31
32
33 Command::new(COMMAND)
34 .arg("-r")
35 .arg(format!("{}/", &configuration.archive_output_directory.display()))
36 .arg(input.as_str())
37 .stdout(Stdio::null())
38 .stderr(Stdio::null())
39 .status()
50f53dc4 40 .map_err(|_| Error::new(Other, "Rsync failed to publish."))?;
d620665f
RBR
41 return Ok(())
42 }
43
44 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
45 vec![]
46 }
47
48 fn command(&self) -> &'static str {
49 "publish-archive"
50 }
51
52 fn help(&self) -> &'static str {
2f579cf4 53 "<destination>\tPublishes the archive to a remote host"
d620665f
RBR
54 }
55}