From 3e7a08d27fdbc8ec7e73045502d2275a9dfb1edf Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Sun, 1 Oct 2023 14:25:28 +0800 Subject: [PATCH] #36: Add --dependencies option --- CHANGELOG.md | 1 + src/cli.rs | 15 +++++++++++++++ src/control.rs | 19 +++++++++++++++---- src/main.rs | 3 ++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1c287a..c31c85f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/cli.rs b/src/cli.rs index 2e14ca3..8a15935 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -216,6 +216,10 @@ pub enum Subcommand { #[clap(long, value_name = "path", parse(try_from_str = parse_canonical_path))] cwd: Option, + /// Other services that must be started first (comma-separated) + #[clap(long, use_delimiter(true))] + dependencies: Vec, + /// Name of the service to create #[clap(long)] name: String, @@ -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() @@ -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")], @@ -594,6 +600,7 @@ speculate::speculate! { sub: Subcommand::Add { name: s("foo"), cwd: None, + dependencies: vec![], common: CommonOpts { restart: true, command: vec![s("foo")], @@ -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")], @@ -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")], @@ -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")], @@ -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")], @@ -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")], @@ -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")], @@ -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")], @@ -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")], diff --git a/src/control.rs b/src/control.rs index a2f6382..580fbc0 100644 --- a/src/control.rs +++ b/src/control.rs @@ -2,7 +2,12 @@ use crate::cli::CommonOpts; use log::error; use std::io::Write; -pub fn add_service(name: String, cwd: Option, opts: CommonOpts) -> Result<(), ()> { +pub fn add_service( + name: String, + cwd: Option, + dependencies: &[String], + opts: CommonOpts, +) -> Result<(), ()> { let shawl_path = quote( &std::env::current_exe() .expect("Unable to determine Shawl location") @@ -11,9 +16,15 @@ pub fn add_service(name: String, cwd: Option, 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!( "{} {} -- {}", diff --git a/src/main.rs b/src/main.rs index 58723e4..3094c80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,8 +76,9 @@ fn main() -> Result<(), Box> { 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), },