| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use serde::{Deserialize, Serialize}; |
| 9 |
use std::path::PathBuf; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 15 |
#[serde(rename_all = "snake_case")] |
| 16 |
pub enum Action { |
| 17 |
|
| 18 |
Build, |
| 19 |
Sign, |
| 20 |
Notarize, |
| 21 |
Staple, |
| 22 |
Package, |
| 23 |
|
| 24 |
Deploy, |
| 25 |
Restart, |
| 26 |
Rollback, |
| 27 |
|
| 28 |
Observe(ObserveKind), |
| 29 |
|
| 30 |
|
| 31 |
Custom(String), |
| 32 |
} |
| 33 |
|
| 34 |
impl Action { |
| 35 |
|
| 36 |
pub fn is_actuate(&self) -> bool { |
| 37 |
!matches!(self, Action::Observe(_)) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
pub fn token(&self) -> Option<String> { |
| 43 |
Some(match self { |
| 44 |
Action::Build => "build".into(), |
| 45 |
Action::Sign => "sign".into(), |
| 46 |
Action::Notarize => "notarize".into(), |
| 47 |
Action::Staple => "staple".into(), |
| 48 |
Action::Package => "package".into(), |
| 49 |
Action::Deploy => "deploy".into(), |
| 50 |
Action::Restart => "restart".into(), |
| 51 |
Action::Rollback => "rollback".into(), |
| 52 |
Action::Custom(s) => s.clone(), |
| 53 |
Action::Observe(_) => return None, |
| 54 |
}) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
pub fn actuate_from_token(token: &str) -> Action { |
| 61 |
match token { |
| 62 |
"build" => Action::Build, |
| 63 |
"sign" => Action::Sign, |
| 64 |
"notarize" => Action::Notarize, |
| 65 |
"staple" => Action::Staple, |
| 66 |
"package" => Action::Package, |
| 67 |
"deploy" => Action::Deploy, |
| 68 |
"restart" => Action::Restart, |
| 69 |
"rollback" => Action::Rollback, |
| 70 |
other => Action::Custom(other.to_string()), |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] |
| 77 |
#[serde(rename_all = "snake_case")] |
| 78 |
pub enum ObserveKind { |
| 79 |
Journal, |
| 80 |
Metrics, |
| 81 |
Health, |
| 82 |
BuildLog, |
| 83 |
Custom(String), |
| 84 |
} |
| 85 |
|
| 86 |
impl ObserveKind { |
| 87 |
pub fn token(&self) -> String { |
| 88 |
match self { |
| 89 |
ObserveKind::Journal => "journal".into(), |
| 90 |
ObserveKind::Metrics => "metrics".into(), |
| 91 |
ObserveKind::Health => "health".into(), |
| 92 |
ObserveKind::BuildLog => "build-log".into(), |
| 93 |
ObserveKind::Custom(s) => s.clone(), |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
pub fn from_token(token: &str) -> ObserveKind { |
| 98 |
match token { |
| 99 |
"journal" => ObserveKind::Journal, |
| 100 |
"metrics" => ObserveKind::Metrics, |
| 101 |
"health" => ObserveKind::Health, |
| 102 |
"build-log" | "build_log" => ObserveKind::BuildLog, |
| 103 |
other => ObserveKind::Custom(other.to_string()), |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#[derive(Clone, Debug, Serialize, Deserialize)] |
| 114 |
pub struct Step { |
| 115 |
pub action: Action, |
| 116 |
pub argv: Vec<String>, |
| 117 |
#[serde(default)] |
| 118 |
pub env: Vec<(String, String)>, |
| 119 |
#[serde(default)] |
| 120 |
pub cwd: Option<PathBuf>, |
| 121 |
} |
| 122 |
|
| 123 |
impl Step { |
| 124 |
|
| 125 |
pub fn new(action: Action, argv: impl IntoIterator<Item = impl Into<String>>) -> Self { |
| 126 |
Self { |
| 127 |
action, |
| 128 |
argv: argv.into_iter().map(Into::into).collect(), |
| 129 |
env: Vec::new(), |
| 130 |
cwd: None, |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
pub fn shell(action: Action, script: impl Into<String>) -> Self { |
| 137 |
Self { |
| 138 |
action, |
| 139 |
argv: vec!["/bin/sh".into(), "-c".into(), script.into()], |
| 140 |
env: Vec::new(), |
| 141 |
cwd: None, |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
pub fn with_env(mut self, key: impl Into<String>, val: impl Into<String>) -> Self { |
| 146 |
self.env.push((key.into(), val.into())); |
| 147 |
self |
| 148 |
} |
| 149 |
|
| 150 |
pub fn with_cwd(mut self, cwd: impl Into<PathBuf>) -> Self { |
| 151 |
self.cwd = Some(cwd.into()); |
| 152 |
self |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
pub(crate) fn shell_script(&self) -> Option<&str> { |
| 157 |
match self.argv.as_slice() { |
| 158 |
[sh, dash_c, script] if (sh == "/bin/sh" || sh == "sh") && dash_c == "-c" => { |
| 159 |
Some(script.as_str()) |
| 160 |
} |
| 161 |
_ => None, |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
#[cfg(test)] |
| 167 |
mod tests { |
| 168 |
use super::*; |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn token_roundtrip() { |
| 172 |
for tok in ["build", "sign", "deploy", "restart", "rollback", "package"] { |
| 173 |
assert_eq!(Action::actuate_from_token(tok).token().as_deref(), Some(tok)); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn unknown_actuate_token_is_custom_not_build() { |
| 179 |
let a = Action::actuate_from_token("frobnicate"); |
| 180 |
assert_eq!(a, Action::Custom("frobnicate".into())); |
| 181 |
assert!(a.is_actuate()); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn observe_is_not_actuate() { |
| 186 |
assert!(!Action::Observe(ObserveKind::Health).is_actuate()); |
| 187 |
assert_eq!(Action::Observe(ObserveKind::Health).token(), None); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn shell_step_detected() { |
| 192 |
let s = Step::shell(Action::Deploy, "set -e; echo hi"); |
| 193 |
assert_eq!(s.shell_script(), Some("set -e; echo hi")); |
| 194 |
let p = Step::new(Action::Build, ["cargo", "build"]); |
| 195 |
assert_eq!(p.shell_script(), None); |
| 196 |
} |
| 197 |
} |
| 198 |
|