Skip to content

Commit

Permalink
Increase line length
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Oct 15, 2024
1 parent f21ab79 commit a4ace10
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 69 deletions.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 120
5 changes: 1 addition & 4 deletions src/bin/shawl-child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use log::info;
use clap::Parser;

#[derive(clap::Parser, Debug)]
#[clap(
name = "shawl-child",
about = "Dummy program to test wrapping with Shawl"
)]
#[clap(name = "shawl-child", about = "Dummy program to test wrapping with Shawl")]
struct Cli {
/// Run forever unless forcibly killed
#[clap(long)]
Expand Down
13 changes: 2 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,7 @@ pub enum Priority {
}

impl Priority {
pub const ALL: &'static [&'static str] = &[
"realtime",
"high",
"above-normal",
"normal",
"below-normal",
"idle",
];
pub const ALL: &'static [&'static str] = &["realtime", "high", "above-normal", "normal", "below-normal", "idle"];
}

impl Priority {
Expand Down Expand Up @@ -303,9 +296,7 @@ pub enum Subcommand {
#[clap(long)]
name: String,
},
#[clap(
about = "Run a command as a service; only works when launched by the Windows service manager"
)]
#[clap(about = "Run a command as a service; only works when launched by the Windows service manager")]
Run {
#[clap(flatten)]
common: CommonOpts,
Expand Down
14 changes: 2 additions & 12 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ use crate::cli::CommonOpts;
use log::error;
use std::io::Write;

pub fn add_service(
name: String,
cwd: Option<String>,
dependencies: &[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 Down Expand Up @@ -85,12 +80,7 @@ fn construct_shawl_run_args(name: &str, cwd: &Option<String>, opts: &CommonOpts)
};
if let Some(pass) = &opts.pass {
shawl_args.push("--pass".to_string());
shawl_args.push(
pass.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(","),
);
shawl_args.push(pass.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(","));
}
if let Some(cwd) = &cwd {
shawl_args.push("--cwd".to_string());
Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};
if should_log {
let (name, common) = match &cli.sub {
Subcommand::Add { name, common, .. } | Subcommand::Run { name, common, .. } => {
(name, common)
}
Subcommand::Add { name, common, .. } | Subcommand::Run { name, common, .. } => (name, common),
};
prepare_logging(
name,
Expand Down
63 changes: 24 additions & 39 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use log::{debug, error, info};
use std::{io::BufRead, os::windows::process::CommandExt};
use windows_service::{
define_windows_service,
service::{
ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus,
ServiceType,
},
service::{ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus, ServiceType},
service_control_handler::{self, ServiceControlHandlerResult},
service_dispatcher,
};
Expand All @@ -21,9 +18,7 @@ enum ProcessStatus {
Terminated,
}

fn check_process(
child: &mut std::process::Child,
) -> Result<ProcessStatus, Box<dyn std::error::Error>> {
fn check_process(child: &mut std::process::Child) -> Result<ProcessStatus, Box<dyn std::error::Error>> {
match child.try_wait() {
Ok(None) => Ok(ProcessStatus::Running),
Ok(Some(status)) => match status.code() {
Expand Down Expand Up @@ -193,9 +188,7 @@ pub fn run_service(start_arguments: Vec<std::ffi::OsString>) -> windows_service:
error!("Unable to launch command: {}", e);
service_exit_code = match e.raw_os_error() {
Some(win_code) => ServiceExitCode::Win32(win_code as u32),
None => {
ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0)
}
None => ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0),
};
break;
}
Expand All @@ -209,18 +202,16 @@ pub fn run_service(start_arguments: Vec<std::ffi::OsString>) -> windows_service:
return;
}
if let Some(stdout) = stdout_option {
std::io::BufReader::new(stdout)
.lines()
.for_each(|line| match line {
Ok(ref x) if !x.is_empty() => {
if output_logs_need_target {
debug!(target: "{shawl-cmd}", "{}", x);
} else {
debug!("stdout: {:?}", x);
}
std::io::BufReader::new(stdout).lines().for_each(|line| match line {
Ok(ref x) if !x.is_empty() => {
if output_logs_need_target {
debug!(target: "{shawl-cmd}", "{}", x);
} else {
debug!("stdout: {:?}", x);
}
_ => (),
});
}
_ => (),
});
}
});

Expand All @@ -231,18 +222,16 @@ pub fn run_service(start_arguments: Vec<std::ffi::OsString>) -> windows_service:
return;
}
if let Some(stderr) = stderr_option {
std::io::BufReader::new(stderr)
.lines()
.for_each(|line| match line {
Ok(ref x) if !x.is_empty() => {
if output_logs_need_target {
debug!(target: "{shawl-cmd}", "{}", x);
} else {
debug!("stderr: {:?}", x);
}
std::io::BufReader::new(stderr).lines().for_each(|line| match line {
Ok(ref x) if !x.is_empty() => {
if output_logs_need_target {
debug!(target: "{shawl-cmd}", "{}", x);
} else {
debug!("stderr: {:?}", x);
}
_ => (),
});
}
_ => (),
});
}
});

Expand All @@ -255,9 +244,7 @@ pub fn run_service(start_arguments: Vec<std::ffi::OsString>) -> windows_service:
controls_accepted: ServiceControlAccept::empty(),
exit_code: ServiceExitCode::NO_ERROR,
checkpoint: 0,
wait_hint: std::time::Duration::from_millis(
opts.stop_timeout.unwrap_or(3000) + 1000,
),
wait_hint: std::time::Duration::from_millis(opts.stop_timeout.unwrap_or(3000) + 1000),
process_id: None,
})?;

Expand Down Expand Up @@ -339,8 +326,7 @@ pub fn run_service(start_arguments: Vec<std::ffi::OsString>) -> windows_service:
}
Ok(ProcessStatus::Terminated) => {
info!("Command was terminated by a signal");
service_exit_code =
ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0);
service_exit_code = ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0);
if should_restart_terminated_command(opts.restart, opts.no_restart) {
break 'inner;
} else {
Expand All @@ -349,8 +335,7 @@ pub fn run_service(start_arguments: Vec<std::ffi::OsString>) -> windows_service:
}
Err(e) => {
info!("Error trying to determine command status: {:?}", e);
service_exit_code =
ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0);
service_exit_code = ServiceExitCode::Win32(windows::Win32::Foundation::ERROR_PROCESS_ABORTED.0);
break 'inner;
}
}
Expand Down

0 comments on commit a4ace10

Please sign in to comment.