| 38 | } |
| 39 | |
| 40 | fn read_batch_steps( |
| 41 | steps: Vec<String>, |
| 42 | file: Option<PathBuf>, |
| 43 | use_stdin: bool, |
| 44 | ) -> anyhow::Result<Vec<String>> { |
| 45 | let source_count = |
| 46 | usize::from(!steps.is_empty()) + usize::from(file.is_some()) + usize::from(use_stdin); |
| 47 | if source_count != 1 { |
| 48 | return Err(crate::error::AppError::bad_request( |
| 49 | "Specify exactly one batch source: --step, --file, or --stdin.", |
| 50 | ) |
| 51 | .into()); |
| 52 | } |
| 53 | let raw = if use_stdin { |
| 54 | let mut buffer = String::new(); |
| 55 | io::stdin().read_to_string(&mut buffer)?; |
| 56 | buffer |
| 57 | } else if let Some(file) = file { |
| 58 | fs::read_to_string(file)? |
| 59 | } else { |
| 60 | return Ok(steps); |
| 61 | }; |
| 62 | Ok(raw |
| 63 | .lines() |
| 64 | .map(str::trim) |
| 65 | .filter(|line| !line.is_empty() && !line.starts_with('#')) |
| 66 | .map(str::to_owned) |
| 67 | .collect()) |
| 68 | } |
| 69 | |
| 70 | fn batch_lines_to_json_steps(step_lines: &[String]) -> anyhow::Result<Vec<Value>> { |
| 71 | step_lines |