silver_platter/
checks.rs

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
66
67
68
69
70
71
72
73
74
75
76
use breezyshim::tree::WorkingTree;
use breezyshim::RevisionId;
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::process::Command;

#[derive(Debug)]
pub struct PreCheckFailed;

impl fmt::Display for PreCheckFailed {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Pre-check failed")
    }
}

impl Error for PreCheckFailed {}

pub fn run_pre_check(tree: WorkingTree, script: &str) -> Result<(), PreCheckFailed> {
    let path = tree.abspath(std::path::Path::new("")).unwrap();
    let status = Command::new("sh")
        .arg("-c")
        .arg(script)
        .current_dir(path)
        .status();

    match status {
        Ok(status) => {
            if status.code().unwrap() != 0 {
                Err(PreCheckFailed)
            } else {
                Ok(())
            }
        }
        Err(_) => Err(PreCheckFailed),
    }
}

#[derive(Debug)]
pub struct PostCheckFailed;

impl fmt::Display for PostCheckFailed {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Post-check failed")
    }
}

impl Error for PostCheckFailed {}

pub fn run_post_check(
    tree: WorkingTree,
    script: &str,
    since_revid: &RevisionId,
) -> Result<(), PostCheckFailed> {
    let mut env_vars = HashMap::new();
    env_vars.insert("SINCE_REVID", since_revid.to_string());
    let path = tree.abspath(std::path::Path::new("")).unwrap();

    let status = Command::new("sh")
        .arg("-c")
        .arg(script)
        .current_dir(path)
        .envs(&env_vars)
        .status();

    match status {
        Ok(status) => {
            if status.code().unwrap() != 0 {
                Err(PostCheckFailed)
            } else {
                Ok(())
            }
        }
        Err(_) => Err(PostCheckFailed),
    }
}