diff --git a/more-wallpapers/src/error.rs b/more-wallpapers/src/error.rs index aac6f30..83d79f9 100644 --- a/more-wallpapers/src/error.rs +++ b/more-wallpapers/src/error.rs @@ -1,6 +1,6 @@ #[cfg(target_os = "linux")] use std::env; -use std::io; +use std::{io, process::Command, ffi::OsString}; use thiserror::Error; #[cfg(target_os = "linux")] @@ -12,13 +12,13 @@ use xrandr; #[derive(Debug, Error)] pub enum CommandError { #[cfg(target_os = "linux")] - #[error("failed to execute command {0}: {1}")] - CommandIO(&'static str, std::io::Error), + #[error("failed to execute program {0:?}: {1}")] + CommandIO(OsString, std::io::Error), #[cfg(target_os = "linux")] #[error("{command:?} exit with code {exit_code:?}:\n{}", String::from_utf8_lossy(.stderr))] CommandStatus { - command: &'static str, + command: Command, exit_code: Option, stderr: Vec, }, diff --git a/more-wallpapers/src/linux/mod.rs b/more-wallpapers/src/linux/mod.rs index f535d0a..69fc85f 100644 --- a/more-wallpapers/src/linux/mod.rs +++ b/more-wallpapers/src/linux/mod.rs @@ -1,5 +1,5 @@ use crate::{error::CommandError, load_env_var, Environment, WallpaperBuilder, WallpaperError}; -use std::{ffi::OsStr, process, process::Command}; +use std::{ffi::OsStr, process::Command}; mod cinnamon; mod kde; @@ -77,23 +77,25 @@ pub(crate) fn set_screens_from_builder(builder: WallpaperBuilder) -> Result<(), } /// run a command, check error code and convert the result -fn run(program: &'static str, args: I) -> Result, CommandError> +fn run(program: &str, args: I) -> Result, CommandError> where I: IntoIterator, S: AsRef, { - check_command_error(Command::new(program).args(args).output(), program) + let mut command = Command::new(program); + command.args(args); + run_command(command) } /// allow also checking more complex commands -fn check_command_error( - output: Result, - program: &'static str, +fn run_command( + mut command: Command, ) -> Result, CommandError> { - let output = output.map_err(|err| CommandError::CommandIO(program, err))?; + let output = command.output(); + let output = output.map_err(|err| CommandError::CommandIO(command.get_program().into(), err))?; if !output.status.success() { return Err(CommandError::CommandStatus { - command: program, + command, exit_code: output.status.code(), stderr: output.stderr, }); diff --git a/more-wallpapers/src/linux/sway.rs b/more-wallpapers/src/linux/sway.rs index dc35cc7..f7ea5a3 100644 --- a/more-wallpapers/src/linux/sway.rs +++ b/more-wallpapers/src/linux/sway.rs @@ -1,4 +1,4 @@ -use super::check_command_error; +use super::run_command; use crate::{error::WallpaperError, Mode, Screen}; use serde::Deserialize; use std::process::Command; @@ -47,7 +47,7 @@ struct OutputScreens { pub(crate) fn get_screens() -> Result, WallpaperError> { let mut command = Command::new("swaymsg"); command.args(["-t", "get_outputs"]); - let output = check_command_error(command.output(), "swaymsg")?; + let output = run_command(command)?; let output = String::from_utf8(output).unwrap(); println!("{output}"); let output: Vec = serde_json::from_str(&output)?; @@ -72,7 +72,7 @@ pub(crate) fn set_screens(screens: Vec) -> Result<(), WallpaperError> { .arg("bg") .arg(screen.wallpaper.unwrap()) .arg(format!("{}", SMode::from(screen.mode.unwrap()))); - check_command_error(command.output(), "swaymsg")?; + run_command(command)?; } Ok(()) } diff --git a/more-wallpapers/src/linux/x11.rs b/more-wallpapers/src/linux/x11.rs index ffc43ae..254a5c4 100644 --- a/more-wallpapers/src/linux/x11.rs +++ b/more-wallpapers/src/linux/x11.rs @@ -1,4 +1,4 @@ -use crate::{error::CommandError, linux::check_command_error, Mode, Screen}; +use crate::{error::CommandError, linux::run_command, Mode, Screen}; use std::process::Command; pub(crate) fn get_screens() -> Result, xrandr::XrandrError> { @@ -34,6 +34,6 @@ pub(crate) fn set_screens(screens: Vec) -> Result<(), CommandError> { screen.wallpaper.as_ref().unwrap().as_str(), ]); } - check_command_error(command.output(), "xwallpaper")?; + run_command(command)?; Ok(()) } diff --git a/more-wallpapers/src/linux/xfce.rs b/more-wallpapers/src/linux/xfce.rs index 19755cf..99a7c86 100644 --- a/more-wallpapers/src/linux/xfce.rs +++ b/more-wallpapers/src/linux/xfce.rs @@ -1,12 +1,12 @@ -use super::check_command_error; -use crate::{error::CommandError, load_env_var, Environment, Mode, Screen, WallpaperBuilder, WallpaperError}; +use super::run_command; +use crate::{Mode, Screen, WallpaperError}; use std::{collections::HashMap, ffi::OsStr, process::Command}; fn load_property(property: &str) -> Result { let mut command = Command::new("xfconf-query"); command.args(["--channel", "xfce4-desktop", "p"]); command.arg(property); - let output = check_command_error(command.output(), "xfconf-query")?; + let output = run_command(command)?; let output = String::from_utf8(output).unwrap(); Ok(output) } @@ -14,7 +14,7 @@ fn load_property(property: &str) -> Result { pub(crate) fn get_screens() -> Result, WallpaperError> { let mut command = Command::new("xfconf-query"); command.args(["--channel", "xfce4-desktop", "--list"]); - let output = check_command_error(command.output(), "xfconf-query")?; + let output = run_command(command)?; let output = String::from_utf8(output).unwrap(); // the outpult looks like the following: // @@ -82,7 +82,7 @@ pub(crate) fn set_screens(screens: Vec) -> Result<(), WallpaperError> { fn set_key>(key: String, property: P) -> Result<(), WallpaperError> { let mut command = Command::new("xfconf-query"); command.args(["--channel", "xfce4-desktop", "--set"]).arg(key).arg(property); - check_command_error(command.output(), "xfconf-query")?; + run_command(command)?; Ok(()) }