diff --git a/adb_cli/src/main.rs b/adb_cli/src/main.rs index 088bdac3..d4e0861c 100644 --- a/adb_cli/src/main.rs +++ b/adb_cli/src/main.rs @@ -48,8 +48,7 @@ fn run_command(mut device: Box, command: DeviceCommands) -> AD device.shell(&mut std::io::stdin(), Box::new(std::io::stdout()))?; } } else { - let commands: Vec<&str> = commands.iter().map(String::as_str).collect(); - device.shell_command(&commands, &mut std::io::stdout())?; + device.shell_command(&commands.join(" "), &mut std::io::stdout())?; } } DeviceCommands::Pull { diff --git a/adb_cli/src/models/device.rs b/adb_cli/src/models/device.rs index 13fcc7dd..9de5f56a 100644 --- a/adb_cli/src/models/device.rs +++ b/adb_cli/src/models/device.rs @@ -7,7 +7,10 @@ use super::RebootTypeCommand; #[derive(Parser, Debug)] pub enum DeviceCommands { /// Spawn an interactive shell or run a list of commands on the device - Shell { commands: Vec }, + Shell { + #[arg(trailing_var_arg = true)] + commands: Vec, + }, /// Pull a file from device Pull { source: String, destination: String }, /// Push a file on device diff --git a/adb_client/src/adb_device_ext.rs b/adb_client/src/adb_device_ext.rs index c20ee555..3463cbc3 100644 --- a/adb_client/src/adb_device_ext.rs +++ b/adb_client/src/adb_device_ext.rs @@ -9,7 +9,7 @@ use crate::{RebootType, Result}; /// Trait representing all features available on ADB devices. pub trait ADBDeviceExt { /// Runs command in a shell on the device, and write its output and error streams into output. - fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()>; + fn shell_command(&mut self, command: &dyn AsRef, output: &mut dyn Write) -> Result<()>; /// Starts an interactive shell session on the device. /// Input data is read from reader and write to writer. @@ -50,16 +50,12 @@ pub trait ADBDeviceExt { ) -> Result> { let mut output = Vec::new(); self.shell_command( - &[ - "am", - "start", - &format!( - "{}/{}.{}", - package.as_ref(), - package.as_ref(), - activity.as_ref() - ), - ], + &format!( + "am start {}/{}.{}", + package.as_ref(), + package.as_ref(), + activity.as_ref() + ), &mut output, )?; diff --git a/adb_client/src/message_devices/adb_message_device_commands.rs b/adb_client/src/message_devices/adb_message_device_commands.rs index c5f193d8..58cb9594 100644 --- a/adb_client/src/message_devices/adb_message_device_commands.rs +++ b/adb_client/src/message_devices/adb_message_device_commands.rs @@ -12,7 +12,7 @@ use std::{ impl ADBDeviceExt for ADBMessageDevice { #[inline] - fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> { + fn shell_command(&mut self, command: &dyn AsRef, output: &mut dyn Write) -> Result<()> { self.shell_command(command, output) } diff --git a/adb_client/src/message_devices/commands/shell.rs b/adb_client/src/message_devices/commands/shell.rs index d959db11..c7e6ff2c 100644 --- a/adb_client/src/message_devices/commands/shell.rs +++ b/adb_client/src/message_devices/commands/shell.rs @@ -13,9 +13,13 @@ use crate::{ impl ADBMessageDevice { /// Runs 'command' in a shell on the device, and write its output and error streams into output. - pub(crate) fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> { + pub(crate) fn shell_command( + &mut self, + command: &dyn AsRef, + output: &mut dyn Write, + ) -> Result<()> { let session = self.open_session(&ADBLocalCommand::ShellCommand( - command.join(" "), + command.as_ref().to_string(), Vec::new(), ))?; diff --git a/adb_client/src/message_devices/tcp/adb_tcp_device.rs b/adb_client/src/message_devices/tcp/adb_tcp_device.rs index 8d46f3b3..e8751faa 100644 --- a/adb_client/src/message_devices/tcp/adb_tcp_device.rs +++ b/adb_client/src/message_devices/tcp/adb_tcp_device.rs @@ -102,7 +102,7 @@ impl ADBTcpDevice { impl ADBDeviceExt for ADBTcpDevice { #[inline] - fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> { + fn shell_command(&mut self, command: &dyn AsRef, output: &mut dyn Write) -> Result<()> { self.inner.shell_command(command, output) } diff --git a/adb_client/src/message_devices/usb/README.md b/adb_client/src/message_devices/usb/README.md index bc9bfd9b..4160913a 100644 --- a/adb_client/src/message_devices/usb/README.md +++ b/adb_client/src/message_devices/usb/README.md @@ -8,7 +8,7 @@ use adb_client::{usb::ADBUSBDevice, ADBDeviceExt}; let vendor_id = 0x04e8; let product_id = 0x6860; let mut device = ADBUSBDevice::new(vendor_id, product_id).expect("cannot find device"); -device.shell_command(&["df", "-h"], &mut std::io::stdout()); +device.shell_command(&"df -h", &mut std::io::stdout()); ``` ## Push a file to the device diff --git a/adb_client/src/message_devices/usb/adb_usb_device.rs b/adb_client/src/message_devices/usb/adb_usb_device.rs index 6001a315..cf7003dc 100644 --- a/adb_client/src/message_devices/usb/adb_usb_device.rs +++ b/adb_client/src/message_devices/usb/adb_usb_device.rs @@ -236,7 +236,7 @@ impl ADBUSBDevice { impl ADBDeviceExt for ADBUSBDevice { #[inline] - fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> { + fn shell_command(&mut self, command: &dyn AsRef, output: &mut dyn Write) -> Result<()> { self.inner.shell_command(command, output) } diff --git a/adb_client/src/server_device/README.md b/adb_client/src/server_device/README.md index 27bf6216..03718586 100644 --- a/adb_client/src/server_device/README.md +++ b/adb_client/src/server_device/README.md @@ -7,7 +7,7 @@ use adb_client::{server::ADBServer, ADBDeviceExt}; let mut server = ADBServer::default(); let mut device = server.get_device().expect("cannot get device"); -device.shell_command(&["df", "-h"], &mut std::io::stdout()); +device.shell_command(&"df -h", &mut std::io::stdout()); ``` ## Push a file to the device diff --git a/adb_client/src/server_device/adb_server_device_commands.rs b/adb_client/src/server_device/adb_server_device_commands.rs index 2cc15eae..7921838e 100644 --- a/adb_client/src/server_device/adb_server_device_commands.rs +++ b/adb_client/src/server_device/adb_server_device_commands.rs @@ -13,7 +13,7 @@ use super::ADBServerDevice; const BUFFER_SIZE: usize = 65535; impl ADBDeviceExt for ADBServerDevice { - fn shell_command(&mut self, command: &[&str], output: &mut dyn Write) -> Result<()> { + fn shell_command(&mut self, command: &dyn AsRef, output: &mut dyn Write) -> Result<()> { let supported_features = self.host_features()?; if !supported_features.contains(&HostFeatures::ShellV2) && !supported_features.contains(&HostFeatures::Cmd) @@ -25,7 +25,6 @@ impl ADBDeviceExt for ADBServerDevice { // Prepare shell command arguments let mut args = Vec::new(); - let command_string = command.join(" "); // Add v2 mode if supported if supported_features.contains(&HostFeatures::ShellV2) { @@ -41,7 +40,7 @@ impl ADBDeviceExt for ADBServerDevice { // Send the request self.transport .send_adb_request(&ADBCommand::Local(ADBLocalCommand::ShellCommand( - command_string, + command.as_ref().to_string(), args, )))?; diff --git a/adb_client/src/server_device/commands/logcat.rs b/adb_client/src/server_device/commands/logcat.rs index be4f5f79..9db9e283 100644 --- a/adb_client/src/server_device/commands/logcat.rs +++ b/adb_client/src/server_device/commands/logcat.rs @@ -49,6 +49,6 @@ impl Write for LogFilter { impl ADBServerDevice { /// Get logs from device pub fn get_logs(&mut self, output: W) -> Result<()> { - self.shell_command(&["exec logcat"], &mut LogFilter::new(output)) + self.shell_command(&"exec logcat", &mut LogFilter::new(output)) } } diff --git a/pyadb_client/src/adb_server_device.rs b/pyadb_client/src/adb_server_device.rs index f4d2b0a7..7f56bb59 100644 --- a/pyadb_client/src/adb_server_device.rs +++ b/pyadb_client/src/adb_server_device.rs @@ -20,11 +20,9 @@ impl PyADBServerDevice { } /// Run shell commands on device and return the output (stdout + stderr merged) - #[expect(clippy::needless_pass_by_value)] - pub fn shell_command(&mut self, commands: Vec) -> Result> { + pub fn shell_command(&mut self, command: &str) -> Result> { let mut output = Vec::new(); - let commands: Vec<&str> = commands.iter().map(|x| &**x).collect(); - self.0.shell_command(&commands, &mut output)?; + self.0.shell_command(&command, &mut output)?; Ok(output) } diff --git a/pyadb_client/src/adb_usb_device.rs b/pyadb_client/src/adb_usb_device.rs index e429be0f..505bf7d5 100644 --- a/pyadb_client/src/adb_usb_device.rs +++ b/pyadb_client/src/adb_usb_device.rs @@ -21,11 +21,9 @@ impl PyADBUSBDevice { } /// Run shell commands on device and return the output (stdout + stderr merged) - #[expect(clippy::needless_pass_by_value)] - pub fn shell_command(&mut self, commands: Vec) -> Result> { + pub fn shell_command(&mut self, command: &str) -> Result> { let mut output = Vec::new(); - let commands: Vec<&str> = commands.iter().map(|x| &**x).collect(); - self.0.shell_command(&commands, &mut output)?; + self.0.shell_command(&command, &mut output)?; Ok(output) }