diff --git a/CHANGELOG.md b/CHANGELOG.md index 2819a52..076b4c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased + +* Added: `--path-prepend` option. + ## v1.5.1 (2024-10-14) * Fixed: Old log files were not deleted when stored on a Windows network share. diff --git a/src/cli.rs b/src/cli.rs index 278e9d5..6c659ab 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -263,10 +263,14 @@ pub struct CommonOpts { #[clap(long, number_of_values = 1, value_parser = parse_env_var)] pub env: Vec<(String, String)>, - /// Additional directory to add to the PATH environment variable (repeatable) + /// Additional directory to append to the PATH environment variable (repeatable) #[clap(long, number_of_values = 1, value_parser = parse_canonical_path)] pub path: Vec, + /// Additional directory to prepend to the PATH environment variable (repeatable) + #[clap(long, number_of_values = 1, value_parser = parse_canonical_path)] + pub path_prepend: Vec, + /// Process priority of the command to run as a service #[clap(long, value_parser = possible_values!(Priority, ALL))] pub priority: Option, @@ -1001,5 +1005,44 @@ speculate::speculate! { }, ); } + + it "accepts --path-prepend" { + let path = env!("CARGO_MANIFEST_DIR"); + check_args( + &["shawl", "add", "--path-prepend", path, "--name", "foo", "--", "foo"], + Cli { + sub: Subcommand::Add { + name: s("foo"), + cwd: None, + dependencies: vec![], + common: CommonOpts { + path_prepend: vec![p(path)], + command: vec![s("foo")], + ..Default::default() + } + } + }, + ); + } + + it "accepts --path-prepend multiple times" { + let path1 = format!("{}/target", env!("CARGO_MANIFEST_DIR")); + let path2 = format!("{}/src", env!("CARGO_MANIFEST_DIR")); + check_args( + &["shawl", "add", "--path-prepend", &path1, "--path-prepend", &path2, "--name", "foo", "--", "foo"], + Cli { + sub: Subcommand::Add { + name: s("foo"), + cwd: None, + dependencies: vec![], + common: CommonOpts { + path_prepend: vec![p(&path1), p(&path2)], + command: vec![s("foo")], + ..Default::default() + } + } + }, + ); + } } } diff --git a/src/control.rs b/src/control.rs index 113c023..9dbfeee 100644 --- a/src/control.rs +++ b/src/control.rs @@ -127,6 +127,12 @@ fn construct_shawl_run_args(name: &str, cwd: &Option, opts: &CommonOpts) shawl_args.push(quote(path)); } } + if !opts.path_prepend.is_empty() { + for path in &opts.path_prepend { + shawl_args.push("--path-prepend".to_string()); + shawl_args.push(quote(path)); + } + } if let Some(priority) = opts.priority { shawl_args.push("--priority".to_string()); shawl_args.push(priority.to_cli()); diff --git a/src/service.rs b/src/service.rs index b510bd9..9bc70e5 100644 --- a/src/service.rs +++ b/src/service.rs @@ -170,6 +170,13 @@ pub fn run_service(start_arguments: Vec) -> windows_service: None => Some(simplified.join(";").to_string()), }; } + if !opts.path_prepend.is_empty() { + let simplified: Vec<_> = opts.path_prepend.iter().map(|x| crate::simplify_path(x)).collect(); + path_env = match path_env { + Some(path) => Some(format!("{};{}", simplified.join(";"), path)), + None => Some(simplified.join(";").to_string()), + }; + } if let Some(active_cwd) = &cwd { let active_cwd = crate::simplify_path(active_cwd); child_cmd.current_dir(&active_cwd);