Skip to content

Commit

Permalink
Added handling of additional start parameters to ServiceUtil
Browse files Browse the repository at this point in the history
 Signed-off-by: Marvin Hansen <[email protected]>
  • Loading branch information
marvin-hansen committed Jan 22, 2025
1 parent ee3e772 commit 91991aa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
3 changes: 2 additions & 1 deletion crates/service_utils/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ impl ServiceUtil {
pub async fn start_service(
&self,
program: &str,
program_args: Option<Vec<&str>>,
wait_strategy: &WaitStrategy,
env_vars: Option<Vec<(String, String)>>,
) -> Result<(), ServiceUtilError> {
self.start(program, env_vars, wait_strategy.to_owned())
self.start(program, program_args, env_vars, wait_strategy.to_owned())
.await
}

Expand Down
12 changes: 11 additions & 1 deletion crates/service_utils/src/service/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ impl ServiceUtil {
) -> Result<(), ServiceUtilError> {
// Extract parameters
let program = service_start_config.program();
let program_args = service_start_config.program_args().to_owned();
let wait_strategy = service_start_config.wait_strategy().to_owned();
let env_vars = service_start_config.env_vars().to_owned();

// Start the service
self.start(program, env_vars, wait_strategy).await
self.start(program,program_args, env_vars, wait_strategy).await
}

pub(crate) async fn start(
&self,
program: &str,
program_args: Option<Vec< & str,>>,
env_vars: Option<Vec<(String, String)>>,
wait_strategy: WaitStrategy,
) -> Result<(), ServiceUtilError> {
Expand Down Expand Up @@ -60,6 +62,14 @@ impl ServiceUtil {
cmd.envs(env_vars);
}

if program_args.is_some() {
self.dbg_print("Setting program arguments");
let program_args = program_args.unwrap();

// Add program arguments
cmd.args(program_args);
}

self.dbg_print(&format!("Run start command: {:?}", &cmd));
cmd.spawn().expect("Failed to run command");

Expand Down
21 changes: 12 additions & 9 deletions crates/service_utils/src/service_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ use wait_utils::WaitStrategy;
///
/// let config = ServiceStartConfig::builder()
/// .program("program")
/// .program_args(vec!["arg1", "arg2"])
/// .wait_strategy(WaitStrategy::NoWait)
/// .env_vars(vec![("key".into(), "value".into())])
/// .env_vars(vec![("KEY".into(), "VALUE".into())])
/// .build();
/// ```
///
Expand All @@ -47,6 +48,7 @@ use wait_utils::WaitStrategy;
pub struct ServiceStartConfig {
program: &'static str,
wait_strategy: WaitStrategy,
program_args: Option<Vec< &'static str,>>,
env_vars: Option<Vec<(String, String)>>,
}

Expand All @@ -65,14 +67,7 @@ impl ServiceStartConfig {
/// ```rust
/// use service_utils::*;
///
/// let config = ServiceStartConfig::new("program", WaitStrategy::NoWait, None);
/// ```
/// Configuration with optional environment variables using the constructor:
///
/// ```rust
/// use service_utils::*;
///
/// let config = ServiceStartConfig::new("program", WaitStrategy::NoWait, Some(vec![("key".into(), "value".into())]));
/// let config = ServiceStartConfig::new("program", WaitStrategy::NoWait, None, None);
/// ```
///
/// # Returns
Expand All @@ -82,11 +77,13 @@ impl ServiceStartConfig {
pub fn new(
program: &'static str,
wait_strategy: WaitStrategy,
program_args: Option<Vec< &'static str,>>,
env_vars: Option<Vec<(String, String)>>,
) -> Self {
Self {
program,
wait_strategy,
program_args,
env_vars,
}
}
Expand All @@ -98,6 +95,8 @@ impl ServiceStartConfig {
self.program
}



#[inline]
pub const fn wait_strategy(&self) -> &WaitStrategy {
&self.wait_strategy
Expand All @@ -108,6 +107,10 @@ impl ServiceStartConfig {
{
&self.env_vars
}
#[inline]
pub fn program_args(&self) -> &Option<Vec<&'static str>> {
&self.program_args
}
}

impl Display for ServiceStartConfig {
Expand Down

0 comments on commit 91991aa

Please sign in to comment.