Skip to content

Commit

Permalink
#57: Add --path-prepend option
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Nov 17, 2024
1 parent b7ccf53 commit d1ae19e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
45 changes: 44 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// 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<String>,

/// Process priority of the command to run as a service
#[clap(long, value_parser = possible_values!(Priority, ALL))]
pub priority: Option<Priority>,
Expand Down Expand Up @@ -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()
}
}
},
);
}
}
}
6 changes: 6 additions & 0 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ fn construct_shawl_run_args(name: &str, cwd: &Option<String>, 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());
Expand Down
7 changes: 7 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ pub fn run_service(start_arguments: Vec<std::ffi::OsString>) -> 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);
Expand Down

0 comments on commit d1ae19e

Please sign in to comment.