From 55e35880019f5ee90dd100a2f15b746f2ed3c67e Mon Sep 17 00:00:00 2001 From: Jonson Petard <41122242+greenhat616@users.noreply.github.com> Date: Fri, 25 Oct 2024 18:04:41 +0800 Subject: [PATCH] chore: use const instead --- nyanpasu-utils/src/os/os_impl/windows.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/nyanpasu-utils/src/os/os_impl/windows.rs b/nyanpasu-utils/src/os/os_impl/windows.rs index b5154d6..575e0d5 100644 --- a/nyanpasu-utils/src/os/os_impl/windows.rs +++ b/nyanpasu-utils/src/os/os_impl/windows.rs @@ -26,13 +26,16 @@ async fn execute_command(command: &str, args: &[&str]) -> IoResult { } pub async fn get_current_user_sid() -> IoResult { - let wmic_command = "cmd"; - let wmic_args = ["/C", "wmic useraccount where name='%username%' get sid"]; - let powershell_command = "powershell"; - let powershell_args = ["-Command", "[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value"]; - let fallback_powershell_args = ["-Command", "Get-WmiObject Win32_UserAccount -Filter \"Name='$env:USERNAME'\" | Select-Object -ExpandProperty SID"]; + const CMD_BINARY: &str = "cmd"; + const WMIC_ARGS: [&str; 2] = ["/C", "wmic useraccount where name='%username%' get sid"]; + const POWERSHELL_BINARY: &str = "powershell"; + const POWERSHELL_ARGS: [&str; 2] = [ + "-Command", + "[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value", + ]; + const FALLBACK_POWERSHELL_ARGS: [&str; 2] = ["-Command", "Get-WmiObject Win32_UserAccount -Filter \"Name='$env:USERNAME'\" | Select-Object -ExpandProperty SID"]; - match execute_command(wmic_command, &wmic_args).await { + match execute_command(CMD_BINARY, &WMIC_ARGS).await { Ok(output_str) => { let lines: Vec<&str> = output_str.lines().collect(); if lines.len() < 2 { @@ -42,9 +45,9 @@ pub async fn get_current_user_sid() -> IoResult { } Err(_) => { // Fallback to PowerShell if wmic fails - match execute_command(powershell_command, &powershell_args).await { + match execute_command(POWERSHELL_BINARY, &POWERSHELL_ARGS).await { Ok(sid) => Ok(sid), - Err(_) => execute_command(powershell_command, &fallback_powershell_args).await, + Err(_) => execute_command(POWERSHELL_BINARY, &FALLBACK_POWERSHELL_ARGS).await, } } } @@ -68,4 +71,4 @@ mod tests { } } } -} \ No newline at end of file +}