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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! [`ShellConfig`] — serializable configuration for a shell step.
use ironflow_core::retry::RetryPolicy;
use serde::{Deserialize, Serialize};
use super::artifact::{ArtifactInput, ArtifactOutput, ArtifactRef};
/// Serializable configuration for a shell step.
///
/// # Examples
///
/// ```
/// use ironflow_engine::config::ShellConfig;
///
/// let config = ShellConfig::new("cargo build --release")
/// .timeout_secs(300)
/// .dir("/app")
/// .output("target/report.html");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellConfig {
/// The shell command to execute.
pub command: String,
/// Timeout in seconds (default: 300).
pub timeout_secs: Option<u64>,
/// Working directory.
pub dir: Option<String>,
/// Environment variables to set.
pub env: Vec<(String, String)>,
/// If true, start with a clean environment.
pub clean_env: bool,
/// Files the step promises to produce, collected once it finishes.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub outputs: Vec<ArtifactOutput>,
/// Artifacts of earlier steps to place in the working directory first.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub inputs: Vec<ArtifactInput>,
/// When `true`, a failure of this step does not fail the run. The step is
/// still marked `Failed` but execution continues and the run finishes with
/// [`RunStatus::Warning`] instead of `Failed`.
#[serde(default)]
pub allow_failure: bool,
/// Optional step-level retry policy. When set, a transient failure retries
/// the step locally with exponential backoff before propagating the error.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retry: Option<RetryPolicy>,
}
impl ShellConfig {
/// Create a new shell config with the given command.
///
/// # Examples
///
/// ```
/// use ironflow_engine::config::ShellConfig;
///
/// let config = ShellConfig::new("echo hello");
/// assert_eq!(config.command, "echo hello");
/// ```
pub fn new(command: &str) -> Self {
Self {
command: command.to_string(),
timeout_secs: None,
dir: None,
env: Vec::new(),
clean_env: false,
outputs: Vec::new(),
inputs: Vec::new(),
allow_failure: false,
retry: None,
}
}
/// Set the timeout in seconds.
pub fn timeout_secs(mut self, secs: u64) -> Self {
self.timeout_secs = Some(secs);
self
}
/// Set the working directory.
pub fn dir(mut self, dir: &str) -> Self {
self.dir = Some(dir.to_string());
self
}
/// Add an environment variable.
pub fn env(mut self, key: &str, value: &str) -> Self {
self.env.push((key.to_string(), value.to_string()));
self
}
/// Start with a clean environment (no inherited vars).
pub fn clean_env(mut self) -> Self {
self.clean_env = true;
self
}
/// Declare a file the step produces, typed from its name.
///
/// `pattern` is a glob resolved against [`dir`](Self::dir). Every match is
/// stored as an artifact named after the file. When the step succeeds and
/// the pattern matches nothing, the step fails.
///
/// # Examples
///
/// ```
/// use ironflow_engine::config::ShellConfig;
///
/// let config = ShellConfig::new("cargo build").output("target/*.log");
/// assert_eq!(config.outputs.len(), 1);
/// ```
pub fn output(mut self, pattern: &str) -> Self {
self.outputs.push(ArtifactOutput::new(pattern));
self
}
/// Declare a produced file with an explicit MIME type.
///
/// # Examples
///
/// ```
/// use ironflow_engine::config::ShellConfig;
///
/// let config = ShellConfig::new("./gen").output_typed("data", "application/json");
/// assert_eq!(config.outputs[0].content_type.as_deref(), Some("application/json"));
/// ```
pub fn output_typed(mut self, pattern: &str, content_type: &str) -> Self {
self.outputs
.push(ArtifactOutput::typed(pattern, content_type));
self
}
/// Consume an artifact produced by an earlier step of the same run.
///
/// The handle comes from the producing step, see [`ArtifactRef`]. The
/// artifact is written into the working directory under its own name
/// before the command runs. Use [`input_at`](Self::input_at) to choose
/// another path.
///
/// # Examples
///
/// ```no_run
/// use ironflow_engine::config::ShellConfig;
/// use ironflow_engine::context::WorkflowContext;
/// use ironflow_engine::error::EngineError;
///
/// # async fn example(ctx: &mut WorkflowContext) -> Result<(), EngineError> {
/// let build = ctx.shell("build", ShellConfig::new("./gen").output("report.html")).await?;
/// let report = build.artifact("report.html")?;
/// let config = ShellConfig::new("./publish").input(&report);
/// assert_eq!(config.inputs[0].destination(), "report.html");
/// # Ok(())
/// # }
/// ```
pub fn input(mut self, artifact: &ArtifactRef) -> Self {
self.inputs.push(ArtifactInput::from(artifact));
self
}
/// Mark this step as allowed to fail without stopping the run.
///
/// # Examples
///
/// ```
/// use ironflow_engine::config::ShellConfig;
///
/// let config = ShellConfig::new("cargo clippy").allow_failure();
/// assert!(config.allow_failure);
/// ```
pub fn allow_failure(mut self) -> Self {
self.allow_failure = true;
self
}
/// Set a step-level retry policy.
///
/// # Examples
///
/// ```
/// use ironflow_core::retry::RetryPolicy;
/// use ironflow_engine::config::ShellConfig;
///
/// let config = ShellConfig::new("curl http://api")
/// .retry_policy(RetryPolicy::new(3));
/// assert!(config.retry.is_some());
/// ```
pub fn retry_policy(mut self, policy: RetryPolicy) -> Self {
self.retry = Some(policy);
self
}
/// Consume an artifact and write it to an explicit path.
///
/// # Examples
///
/// ```no_run
/// use ironflow_engine::config::ShellConfig;
/// use ironflow_engine::context::WorkflowContext;
/// use ironflow_engine::error::EngineError;
///
/// # async fn example(ctx: &mut WorkflowContext) -> Result<(), EngineError> {
/// let build = ctx.shell("build", ShellConfig::new("./gen").output("report.html")).await?;
/// let report = build.artifact("report.html")?;
/// let config = ShellConfig::new("./publish").input_at(&report, "in/r.html");
/// assert_eq!(config.inputs[0].destination(), "in/r.html");
/// # Ok(())
/// # }
/// ```
pub fn input_at(mut self, artifact: &ArtifactRef, dest: &str) -> Self {
self.inputs.push(ArtifactInput::from(artifact).at(dest));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder() {
let config = ShellConfig::new("cargo test")
.timeout_secs(60)
.dir("/app")
.env("RUST_LOG", "debug")
.clean_env();
assert_eq!(config.command, "cargo test");
assert_eq!(config.timeout_secs, Some(60));
assert_eq!(config.dir, Some("/app".to_string()));
assert_eq!(
config.env,
vec![("RUST_LOG".to_string(), "debug".to_string())]
);
assert!(config.clean_env);
}
#[test]
fn a_fresh_config_declares_no_artifact() {
let config = ShellConfig::new("echo hi");
assert!(config.outputs.is_empty());
assert!(config.inputs.is_empty());
}
#[test]
fn outputs_and_inputs_accumulate_in_declaration_order() {
let config = ShellConfig::new("build")
.output("a.txt")
.output_typed("b", "text/csv")
.input(&ArtifactRef::new("prev", "c.txt"))
.input_at(&ArtifactRef::new("prev", "d.txt"), "in/d.txt");
assert_eq!(config.outputs[0].pattern, "a.txt");
assert_eq!(config.outputs[1].content_type.as_deref(), Some("text/csv"));
assert_eq!(config.inputs[0], ArtifactInput::new("prev", "c.txt"));
assert_eq!(config.inputs[0].destination(), "c.txt");
assert_eq!(config.inputs[1].step, "prev");
assert_eq!(config.inputs[1].destination(), "in/d.txt");
}
#[test]
fn serde_omits_empty_artifact_declarations() {
let json = serde_json::to_string(&ShellConfig::new("echo hi")).expect("serialize");
assert!(!json.contains("outputs"));
assert!(!json.contains("inputs"));
}
#[test]
fn a_config_predating_artifacts_still_deserializes() {
let config: ShellConfig = serde_json::from_str(
r#"{"command":"echo hi","timeout_secs":null,"dir":null,"env":[],"clean_env":false}"#,
)
.expect("deserialize");
assert!(config.outputs.is_empty());
assert!(config.inputs.is_empty());
}
#[test]
fn a_config_predating_retry_still_deserializes() {
let config: ShellConfig = serde_json::from_str(
r#"{"command":"echo hi","timeout_secs":null,"dir":null,"env":[],"clean_env":false,"allow_failure":false}"#,
)
.expect("deserialize");
assert!(config.retry.is_none());
}
#[test]
fn retry_policy_roundtrip() {
use ironflow_core::retry::RetryPolicy;
let config = ShellConfig::new("curl http://api").retry_policy(RetryPolicy::new(3));
let json = serde_json::to_string(&config).expect("serialize");
let back: ShellConfig = serde_json::from_str(&json).expect("deserialize");
assert_eq!(back.retry.as_ref().unwrap().max_retries(), 3);
}
}