rusty_ci/buildbot/
step.rs

1use std::fmt::{Display, Error, Formatter};
2
3/// This describes a step executed by a builder in buildbot
4/// This is used by the Builder struct to construct the Builder config file
5#[derive(Clone, Debug)]
6pub enum Step {
7    /// Represents a git clone operation
8    GitClone {
9        url: String, // The repo to clone
10    },
11
12    /// Represents a git clone operation targetting gitlab.
13    /// The reason we need to know specifically about if the clone
14    /// is from gitlab is because this also handles merge requests
15    GitLabClone {
16        url: String, // The repo to clone
17    },
18
19    /// Represents a command line command
20    Command {
21        command: String,         // The command to run
22        workdir: Option<String>, // The optional workdir
23    },
24}
25
26/// Implementation of step struct
27impl Step {
28    /// Construct a command line step
29    pub fn command<S: Display>(command: S, workdir: Option<S>) -> Self {
30        Step::Command {
31            command: command.to_string(),
32            workdir: workdir.and_then(|s| Some(s.to_string())),
33        }
34    }
35
36    /// Construct a git clone step
37    pub fn git_clone<S: Display>(url: S) -> Self {
38        Step::GitClone {
39            url: url
40                .to_string()
41                .trim()
42                .trim_start_matches('\"')
43                .trim_end_matches('\"')
44                .to_string(),
45            // branch: branch.to_string(),
46        }
47    }
48
49    /// Construct a gitlab clone step
50    pub fn gitlab_clone<S: Display>(url: S) -> Self {
51        Step::GitLabClone {
52            url: url
53                .to_string()
54                .trim()
55                .trim_start_matches('\"')
56                .trim_end_matches('\"')
57                .to_string(),
58            // branch: branch.to_string(),
59        }
60    }
61}
62
63/// Convert Step to String / Allow string formatting for Step
64impl Display for Step {
65    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
66        match self {
67            // Used by buildbot to get the updated repository
68            Step::GitClone { url } => write!(f,
69                "steps.Git(repourl=\"{}\", mode=\"full\", branch=\"master\", method=\"clobber\", shallow=False, submodules=True)", url),
70
71            // Used by buildbot to get the updated repository
72            Step::GitLabClone { url } => write!(f,
73                "steps.GitLab(repourl=\"{}\", mode=\"full\", branch=\"master\", method=\"clobber\", shallow=False, submodules=True)", url),
74
75            // Command with provided work directory
76            Step::Command {command, workdir: Some(workdir)} => write!(
77                f, "steps.ShellCommand(command={command:?}, workdir=\"{workdir}\")",
78                command = command
79                    .split_whitespace()
80                    .map(String::from)
81                    .collect::<Vec<String>>(),
82                workdir = workdir
83            ),
84
85            // Command without provided work directory
86            Step::Command {command, workdir: None} => write!(
87                f, "steps.ShellCommand(command={:?})",
88                command.split_whitespace()
89                    .map(String::from)
90                    .collect::<Vec<String>>()
91            ),
92        }
93    }
94}