Skip to content

Commit f510a50

Browse files
moss: client: Adapt code to the new triggers API
1 parent 44f30ba commit f510a50

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

moss/src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ impl Client {
353353
postblit::TriggerScope::Transaction(&self.installation, &self.scope),
354354
&fstree,
355355
)?;
356-
for trigger in triggers {
356+
for trigger in triggers.iter() {
357357
trigger.execute()?;
358358
}
359359
// ephemeral system triggers

moss/src/client/postblit.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88
//!
99
//! Note that currently we only load from `/usr/share/moss/triggers/{tx,sys.d}/*.yaml`
1010
//! and do not yet support local triggers
11-
use std::{
12-
path::{Path, PathBuf},
13-
process,
14-
};
11+
use std::path::{Path, PathBuf};
1512

1613
use crate::Installation;
1714
use container::Container;
1815
use itertools::Itertools;
1916
use serde::Deserialize;
2017
use thiserror::Error;
21-
use triggers::format::{CompiledHandler, Handler, Trigger};
18+
use triggers::{CompiledHandler, Trigger};
2219

2320
use super::PendingFile;
2421

@@ -132,13 +129,9 @@ pub(super) fn triggers<'a>(
132129
};
133130

134131
// 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+
142135
Ok(computed_commands)
143136
}
144137

@@ -184,26 +177,38 @@ impl<'a> TriggerRunner<'a> {
184177

185178
/// Internal executor for triggers.
186179
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());
200186
}
201-
Handler::Delete { .. } => todo!(),
187+
} else {
188+
eprintln!("Failed to execute trigger: {}", trigger);
202189
}
203190

204191
Ok(())
205192
}
206193

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+
207212
#[derive(Debug, Error)]
208213
pub enum Error {
209214
#[error("container")]

0 commit comments

Comments
 (0)