Skip to content

Commit 2aa929f

Browse files
committed
add startup commands
1 parent 519802e commit 2aa929f

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ check_interval_ms: 1000
3737
actions:
3838
# ID of the action
3939
myaction:
40+
# Define commands that shall run on the tool
41+
# startup for this action. This overwrites
42+
# run_commands_on_startup to true.
43+
startup_commands:
44+
- "killall -s TERM myapp"
45+
# Set this to true to run the defined action
46+
# commands on startup of the tool.
47+
run_commands_on_startup: true
4048
# List of files to be watched and
4149
# transitions which will trigger the
4250
# command execution.

fw/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub struct Config {
1818

1919
#[derive(Debug, Deserialize)]
2020
pub struct Action {
21+
pub run_commands_on_startup: Option<bool>,
22+
pub startup_commands: Option<Vec<Command>>,
2123
pub targets: Vec<Target>,
2224
pub commands: Vec<Command>,
2325
}

fw/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::config::Config;
66
use clap::Parser;
77
use env_logger::Env;
88
use log::{debug, error};
9-
use watcher::watch;
109

1110
#[derive(Parser)]
1211
#[command(author, version, about, long_about = None)]
@@ -41,5 +40,5 @@ async fn main() {
4140

4241
debug!("Parsed config: {cfg:#?}");
4342

44-
watch(&cfg, cmd.actions).await;
43+
watcher::watch(&cfg, cmd.actions).await;
4544
}

fw/src/watcher.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ pub async fn watch(cfg: &Config, filter: Option<Vec<String>>) {
2828
.flat_map(|(_, action)| &action.targets)
2929
.for_each(|target| watcher.add_target(BasicTarget::new(target.path())));
3030

31+
let startup_actions = actions.iter().filter(|a| {
32+
a.1.run_commands_on_startup.is_some_and(|v| v) || matches!(a.1.startup_commands, Some(_))
33+
});
34+
for (name, action) in startup_actions {
35+
info!("Executing startup commands for {} ...", name);
36+
if let Some(cmds) = action.startup_commands.as_ref() {
37+
execute_commands::<Vec<(&str, &str)>, &str, &str>(cmds, None).await;
38+
} else {
39+
execute_commands::<Vec<(&str, &str)>, &str, &str>(&action.commands, None).await;
40+
}
41+
}
42+
3143
info!("Watching targets ...");
3244
loop {
3345
for (index, transition) in watcher
@@ -61,7 +73,7 @@ pub async fn watch(cfg: &Config, filter: Option<Vec<String>>) {
6173
),
6274
];
6375
tokio::spawn(async move {
64-
execute_commands(&cmds, envmap).await;
76+
execute_commands(&cmds, Some(envmap)).await;
6577
});
6678
}
6779
}
@@ -71,7 +83,7 @@ pub async fn watch(cfg: &Config, filter: Option<Vec<String>>) {
7183
}
7284
}
7385

74-
async fn execute_commands<E, K, V>(cmds: &[Command], env: E)
86+
async fn execute_commands<E, K, V>(cmds: &[Command], env: Option<E>)
7587
where
7688
E: IntoIterator<Item = (K, V)> + Clone,
7789
K: AsRef<OsStr>,
@@ -87,7 +99,10 @@ where
8799
let args = &args[1..];
88100

89101
let mut exec = process::Command::new(ex);
90-
exec.args(args).current_dir(cmd.cwd()).envs(env.clone());
102+
exec.args(args).current_dir(cmd.cwd());
103+
if let Some(env) = env.as_ref() {
104+
exec.envs(env.clone());
105+
}
91106

92107
if cmd.is_async() {
93108
match exec.spawn() {

0 commit comments

Comments
 (0)