From 71efc29fae94e4f8ef889c84e87e8fcb0268f1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Sat, 22 Jan 2022 13:59:24 +1300 Subject: [PATCH] Hook up -C/--config-file option All it does is parsing, but it's a start --- cli/src/args.rs | 7 +++++++ cli/src/config/file.rs | 5 +---- cli/src/main.rs | 19 ++++++++++++++++++- cli/tests/snapshots/help__help_unix.snap | 1 + cli/tests/snapshots/help__help_windows.snap | 1 + 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cli/src/args.rs b/cli/src/args.rs index c4d3e31..7771629 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -23,6 +23,7 @@ impl Clap3Compat for Arg<'_, '_> {} const OPTSET_FILTERING: &str = "Filtering options:"; const OPTSET_COMMAND: &str = "Command options:"; +const OPTSET_CONFIG: &str = "Config file options:"; const OPTSET_DEBUGGING: &str = "Debugging options:"; const OPTSET_OUTPUT: &str = "Output options:"; const OPTSET_BEHAVIOUR: &str = "Behaviour options:"; @@ -32,6 +33,12 @@ pub fn get_args(tagged_filterer: bool) -> Result> { .version(crate_version!()) .about("Execute commands when watched files change") .after_help("Use @argfile as first argument to load arguments from the file `argfile` (one argument per line) which will be inserted in place of the @argfile (further arguments on the CLI will override or add onto those in the file).") + .arg(Arg::with_name("config-file") + .help_heading(Some(OPTSET_CONFIG)) + .help("Config file(s) to use") + .multiple(true) + .short("C") + .long("config")) .arg(Arg::with_name("command") .help_heading(Some(OPTSET_COMMAND)) .help("Command to execute") diff --git a/cli/src/config/file.rs b/cli/src/config/file.rs index 3fcce8d..790a488 100644 --- a/cli/src/config/file.rs +++ b/cli/src/config/file.rs @@ -199,10 +199,7 @@ mod tests { config.commands[0].run.as_ref().unwrap().shell, Shell::default() ); - assert_eq!( - config.commands[0].run.as_ref().unwrap().shell, - Shell::None - ); + assert_eq!(config.commands[0].run.as_ref().unwrap().shell, Shell::None); } #[test] diff --git a/cli/src/main.rs b/cli/src/main.rs index fb8a691..d4610e7 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,6 +1,8 @@ use std::env::var; -use miette::{IntoDiagnostic, Result}; +use miette::{IntoDiagnostic, Result, WrapErr}; +use tokio::fs::read_to_string; +use tracing::debug; use watchexec::{event::Event, Watchexec}; mod args; @@ -47,6 +49,21 @@ async fn main() -> Result<()> { } } + let mut configs = Vec::with_capacity( + args.occurrences_of("config-file") + .try_into() + .into_diagnostic() + .wrap_err("Too many config files")?, + ); + // TODO: look for global/project config file + for path in args.values_of("config-file").unwrap_or_default() { + let content = read_to_string(path).await.into_diagnostic()?; + configs.push(config::File::parse(&content)?); + } + debug!(?configs, "loaded config files"); + // TODO: merge configs + // TODO: use configs + let init = config::init(&args)?; let mut runtime = config::runtime(&args)?; runtime.filterer(if tagged_filterer { diff --git a/cli/tests/snapshots/help__help_unix.snap b/cli/tests/snapshots/help__help_unix.snap index c79dddb..e642d07 100644 --- a/cli/tests/snapshots/help__help_unix.snap +++ b/cli/tests/snapshots/help__help_unix.snap @@ -12,6 +12,7 @@ USAGE: FLAGS: -c, --clear Clear screen before executing command + -C, --config Config file(s) to use -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 diff --git a/cli/tests/snapshots/help__help_windows.snap b/cli/tests/snapshots/help__help_windows.snap index 66f9bde..be3875c 100644 --- a/cli/tests/snapshots/help__help_windows.snap +++ b/cli/tests/snapshots/help__help_windows.snap @@ -11,6 +11,7 @@ USAGE: FLAGS: -c, --clear Clear screen before executing command + -C, --config Config file(s) to use -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