Skip to content

Commit

Permalink
#36: Add --dependencies option
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Oct 1, 2023
1 parent 3805806 commit 3e7a08d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Fixed: The path to the Shawl executable was not quoted when it contained spaces.
* Added: `--priority` option to set the process priority.
* Added: `--dependencies` option for `add` command to specify services as dependencies.

## v1.2.1 (2023-08-10)

Expand Down
15 changes: 15 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ pub enum Subcommand {
#[clap(long, value_name = "path", parse(try_from_str = parse_canonical_path))]
cwd: Option<String>,

/// Other services that must be started first (comma-separated)
#[clap(long, use_delimiter(true))]
dependencies: Vec<String>,

/// Name of the service to create
#[clap(long)]
name: String,
Expand Down Expand Up @@ -547,6 +551,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("custom-name"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
command: vec![s("foo")],
..Default::default()
Expand Down Expand Up @@ -577,6 +582,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
pass: Some(vec![1, 2]),
command: vec![s("foo")],
Expand All @@ -594,6 +600,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
restart: true,
command: vec![s("foo")],
Expand All @@ -611,6 +618,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
no_restart: true,
command: vec![s("foo")],
Expand All @@ -628,6 +636,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
restart_if: vec![1, 2],
command: vec![s("foo")],
Expand All @@ -645,6 +654,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
restart_if_not: vec![1, 2],
command: vec![s("foo")],
Expand All @@ -662,6 +672,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
stop_timeout: Some(500),
command: vec![s("foo")],
Expand Down Expand Up @@ -748,6 +759,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
env: vec![(s("FOO"), s("bar"))],
command: vec![s("foo")],
Expand All @@ -765,6 +777,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
env: vec![(s("FOO"), s("1")), (s("BAR"), s("2"))],
command: vec![s("foo")],
Expand All @@ -783,6 +796,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
path: vec![p(path)],
command: vec![s("foo")],
Expand All @@ -802,6 +816,7 @@ speculate::speculate! {
sub: Subcommand::Add {
name: s("foo"),
cwd: None,
dependencies: vec![],
common: CommonOpts {
path: vec![p(&path1), p(&path2)],
command: vec![s("foo")],
Expand Down
19 changes: 15 additions & 4 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ use crate::cli::CommonOpts;
use log::error;
use std::io::Write;

pub fn add_service(name: String, cwd: Option<String>, opts: CommonOpts) -> Result<(), ()> {
pub fn add_service(
name: String,
cwd: Option<String>,
dependencies: &[String],
opts: CommonOpts,
) -> Result<(), ()> {
let shawl_path = quote(
&std::env::current_exe()
.expect("Unable to determine Shawl location")
Expand All @@ -11,9 +16,15 @@ pub fn add_service(name: String, cwd: Option<String>, opts: CommonOpts) -> Resul
let shawl_args = construct_shawl_run_args(&name, &cwd, &opts);
let prepared_command = prepare_command(&opts.command);

let output = std::process::Command::new("sc")
.arg("create")
.arg(&name)
let mut cmd = std::process::Command::new("sc");
cmd.arg("create").arg(&name);

if !dependencies.is_empty() {
cmd.arg("depend=");
cmd.arg(quote(&dependencies.join("/")));
}

let output = cmd
.arg("binPath=")
.arg(format!(
"{} {} -- {}",
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Subcommand::Add {
name,
cwd,
dependencies,
common: opts,
} => match control::add_service(name, cwd, opts) {
} => match control::add_service(name, cwd, &dependencies, opts) {
Ok(_) => (),
Err(_) => std::process::exit(1),
},
Expand Down

0 comments on commit 3e7a08d

Please sign in to comment.