diff --git a/src/main.rs b/src/main.rs index 2a1b71e..cad6be3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ use crate::sls::{LogLayer, LogReceiver}; mod ds_inhibit; mod hardware; mod manager; +mod process; mod sls; #[cfg(test)] diff --git a/src/manager.rs b/src/manager.rs index 3408c35..faf116c 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -6,12 +6,13 @@ */ use anyhow::{ensure, Result}; -use std::{ffi::OsStr, fmt}; -use tokio::{fs, fs::File, io::AsyncWriteExt, process::Command}; +use std::fmt; +use tokio::{fs, fs::File, io::AsyncWriteExt}; use tracing::{error, warn}; use zbus::{interface, zvariant::Fd}; use crate::hardware::{variant, HardwareVariant}; +use crate::process::{run_script, script_output}; #[derive(PartialEq, Debug, Copy, Clone)] #[repr(u32)] @@ -78,31 +79,6 @@ const GPU_CLOCKS_PATH: &str = "/sys/class/drm/card0/device/pp_od_clk_voltage"; const SYSTEMCTL_PATH: &str = "/usr/bin/systemctl"; const TRACE_CMD_PATH: &str = "/usr/bin/trace-cmd"; -async fn script_exit_code(executable: &str, args: &[impl AsRef]) -> Result { - // Run given script and return true on success - let mut child = Command::new(executable).args(args).spawn()?; - let status = child.wait().await?; - Ok(status.success()) -} - -async fn run_script(name: &str, executable: &str, args: &[impl AsRef]) -> Result { - // Run given script to get exit code and return true on success. - // Return false on failure, but also print an error if needed - script_exit_code(executable, args) - .await - .inspect_err(|message| warn!("Error running {name} {message}")) -} - -async fn script_output(executable: &str, args: &[impl AsRef]) -> Result { - // Run given command and return the output given - let output = Command::new(executable).args(args).output(); - - let output = output.await?; - - let s = std::str::from_utf8(&output.stdout)?; - Ok(s.to_string()) -} - async fn setup_iwd_config(want_override: bool) -> std::io::Result<()> { // Copy override.conf file into place or out of place depending // on install value diff --git a/src/process.rs b/src/process.rs new file mode 100644 index 0000000..a5e6847 --- /dev/null +++ b/src/process.rs @@ -0,0 +1,36 @@ +/* + * Copyright © 2023 Collabora Ltd. + * Copyright © 2024 Valve Software + * + * SPDX-License-Identifier: MIT + */ + +use anyhow::Result; +use std::ffi::OsStr; +use tokio::process::Command; +use tracing::warn; + +pub async fn script_exit_code(executable: &str, args: &[impl AsRef]) -> Result { + // Run given script and return true on success + let mut child = Command::new(executable).args(args).spawn()?; + let status = child.wait().await?; + Ok(status.success()) +} + +pub async fn run_script(name: &str, executable: &str, args: &[impl AsRef]) -> Result { + // Run given script to get exit code and return true on success. + // Return false on failure, but also print an error if needed + script_exit_code(executable, args) + .await + .inspect_err(|message| warn!("Error running {name} {message}")) +} + +pub async fn script_output(executable: &str, args: &[impl AsRef]) -> Result { + // Run given command and return the output given + let output = Command::new(executable).args(args).output(); + + let output = output.await?; + + let s = std::str::from_utf8(&output.stdout)?; + Ok(s.to_string()) +}