Add environment vars support

This commit is contained in:
Félix Saparelli 2021-12-29 20:55:09 +13:00
parent 53d9a65e5f
commit 9ed3645088
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
4 changed files with 37 additions and 8 deletions

View File

@ -135,9 +135,9 @@ pub fn get_args(tagged_filterer: bool) -> Result<ArgMatches<'static>> {
.help("Do not wrap command in a shell. Deprecated: use --shell=none instead.")
.short("n")
.long("no-shell"))
.arg(Arg::with_name("no-environment") // TODO
.arg(Arg::with_name("no-environment")
.help_heading(Some(OPTSET_OUTPUT))
.help("Do not set WATCHEXEC_*_PATH environment variables for the command")
.help("Do not set WATCHEXEC_* environment variables for the command")
.long("no-environment"))
.arg(Arg::with_name("no-process-group")
.help_heading(Some(OPTSET_COMMAND))
@ -201,7 +201,7 @@ pub fn get_args(tagged_filterer: bool) -> Result<ArgMatches<'static>> {
.takes_value(true),
)
.arg(
Arg::with_name("filter") // TODO
Arg::with_name("filter")
.help_heading(Some(OPTSET_FILTERING))
.help("Ignore all modifications except those matching the pattern")
.short("f")
@ -212,7 +212,7 @@ pub fn get_args(tagged_filterer: bool) -> Result<ArgMatches<'static>> {
.value_name("pattern"),
)
.arg(
Arg::with_name("ignore") // TODO
Arg::with_name("ignore")
.help_heading(Some(OPTSET_FILTERING))
.help("Ignore modifications to paths matching the pattern")
.short("i")

View File

@ -4,11 +4,13 @@ use clap::ArgMatches;
use miette::{IntoDiagnostic, Result};
use notify_rust::Notification;
use watchexec::{
action::{Action, Outcome},
action::{Action, Outcome, PostSpawn, PreSpawn},
command::Shell,
config::RuntimeConfig,
event::ProcessEnd,
fs::Watcher,
handler::SyncFnHandler,
paths::summarise_events_to_env,
signal::{process::SubSignal, source::MainSignal},
};
@ -194,7 +196,34 @@ pub fn runtime(args: &ArgMatches<'static>) -> Result<RuntimeConfig> {
fut
});
// TODO: pre-command (environment vars)
let no_env = args.is_present("no-environment");
config.on_pre_spawn(move |prespawn: PreSpawn| async move {
if !no_env {
let envs = summarise_events_to_env(prespawn.events.iter());
if let Some(mut command) = prespawn.command().await {
for (k, v) in envs {
command.env(format!("WATCHEXEC_{}", k), v);
}
}
}
Ok::<(), Infallible>(())
});
config.on_post_spawn(SyncFnHandler::from(move |postspawn: PostSpawn| {
if notif {
Notification::new()
.summary("Watchexec: change detected")
.body(&format!("Running `{}`", postspawn.command.join(" ")))
.show()
.map(drop)
.unwrap_or_else(|err| {
eprintln!("Failed to send desktop notification: {}", err);
});
}
Ok::<(), Infallible>(())
}));
Ok(config)
}

View File

@ -13,7 +13,7 @@ FLAGS:
-c, --clear Clear screen before executing command
-h, --help Prints help information
--no-default-ignore Skip auto-ignoring of commonly ignored globs
--no-environment Do not set WATCHEXEC_*_PATH environment variables for the command
--no-environment Do not set WATCHEXEC_* environment variables for the command
--no-global-ignore Skip auto-loading of global or environment-wide ignore files
--no-meta Ignore metadata changes
--no-process-group Do not use a process group when running the command

View File

@ -13,7 +13,7 @@ FLAGS:
-c, --clear Clear screen before executing command
-h, --help Prints help information
--no-default-ignore Skip auto-ignoring of commonly ignored globs
--no-environment Do not set WATCHEXEC_*_PATH environment variables for the command
--no-environment Do not set WATCHEXEC_* environment variables for the command
--no-global-ignore Skip auto-loading of global or environment-wide ignore files
--no-meta Ignore metadata changes
--no-process-group Do not use a process group when running the command