|
8 | 8 | //!
|
9 | 9 | //! Note that currently we only load from `/usr/share/moss/triggers/{tx,sys.d}/*.yaml`
|
10 | 10 | //! and do not yet support local triggers
|
11 |
| -use std::{ |
12 |
| - path::{Path, PathBuf}, |
13 |
| - process, |
14 |
| -}; |
| 11 | +use std::path::{Path, PathBuf}; |
15 | 12 |
|
16 | 13 | use crate::Installation;
|
17 | 14 | use container::Container;
|
18 | 15 | use itertools::Itertools;
|
19 | 16 | use serde::Deserialize;
|
20 | 17 | use thiserror::Error;
|
21 |
| -use triggers::format::{CompiledHandler, Handler, Trigger}; |
| 18 | +use triggers::{CompiledHandler, Trigger}; |
22 | 19 |
|
23 | 20 | use super::PendingFile;
|
24 | 21 |
|
@@ -132,13 +129,9 @@ pub(super) fn triggers<'a>(
|
132 | 129 | };
|
133 | 130 |
|
134 | 131 | // Load trigger collection, process all the paths, convert to scoped TriggerRunner vec
|
135 |
| - let mut collection = triggers::Collection::new(triggers.iter())?; |
136 |
| - collection.process_paths(fstree.iter().map(|m| m.to_string())); |
137 |
| - let computed_commands = collection |
138 |
| - .bake()? |
139 |
| - .into_iter() |
140 |
| - .map(|trigger| TriggerRunner { scope, trigger }) |
141 |
| - .collect_vec(); |
| 132 | + let graph = triggers::DepGraph::from_iter(triggers.iter()); |
| 133 | + let computed_commands = process_paths(fstree.iter().map(|m| m.to_string()), &graph, scope); |
| 134 | + |
142 | 135 | Ok(computed_commands)
|
143 | 136 | }
|
144 | 137 |
|
@@ -184,26 +177,38 @@ impl<'a> TriggerRunner<'a> {
|
184 | 177 |
|
185 | 178 | /// Internal executor for triggers.
|
186 | 179 | fn execute_trigger_directly(trigger: &CompiledHandler) -> Result<(), Error> {
|
187 |
| - match trigger.handler() { |
188 |
| - Handler::Run { run, args } => { |
189 |
| - let cmd = process::Command::new(run).args(args).current_dir("/").output()?; |
190 |
| - |
191 |
| - if let Some(code) = cmd.status.code() { |
192 |
| - if code != 0 { |
193 |
| - eprintln!("Trigger exited with non-zero status code: {run} {args:?}"); |
194 |
| - eprintln!(" Stdout: {}", String::from_utf8(cmd.stdout).unwrap()); |
195 |
| - eprintln!(" Stderr: {}", String::from_utf8(cmd.stderr).unwrap()); |
196 |
| - } |
197 |
| - } else { |
198 |
| - eprintln!("Failed to execute trigger: {run} {args:?}"); |
199 |
| - } |
| 180 | + let out = trigger.run(Path::new("/"))?; |
| 181 | + if let Some(code) = out.status.code() { |
| 182 | + if code != 0 { |
| 183 | + eprintln!("Trigger exited with non-zero status code: {}", trigger); |
| 184 | + eprintln!(" Stdout: {}", String::from_utf8(out.stdout).unwrap()); |
| 185 | + eprintln!(" Stderr: {}", String::from_utf8(out.stderr).unwrap()); |
200 | 186 | }
|
201 |
| - Handler::Delete { .. } => todo!(), |
| 187 | + } else { |
| 188 | + eprintln!("Failed to execute trigger: {}", trigger); |
202 | 189 | }
|
203 | 190 |
|
204 | 191 | Ok(())
|
205 | 192 | }
|
206 | 193 |
|
| 194 | +fn process_paths<'a>( |
| 195 | + paths: impl Iterator<Item = String>, |
| 196 | + triggers: &triggers::DepGraph, |
| 197 | + scope: TriggerScope<'a>, |
| 198 | +) -> Vec<TriggerRunner<'a>> { |
| 199 | + paths |
| 200 | + .flat_map(move |fspath| { |
| 201 | + triggers |
| 202 | + .iter() |
| 203 | + .flat_map(move |trig| trig.compiled_handlers(fspath.clone())) // FIXME: can I avoid this clone? |
| 204 | + }) |
| 205 | + .map(|comp_hnd| TriggerRunner { |
| 206 | + scope, |
| 207 | + trigger: comp_hnd, |
| 208 | + }) |
| 209 | + .collect() |
| 210 | +} |
| 211 | + |
207 | 212 | #[derive(Debug, Error)]
|
208 | 213 | pub enum Error {
|
209 | 214 | #[error("container")]
|
|
0 commit comments