Skip to content

Commit

Permalink
breaking: make ADBDeviceExt dyn-compatible (#70)
Browse files Browse the repository at this point in the history
* feat: make ADBDeviceExt dyn-compatible

* feat: clean CLI code
  • Loading branch information
cli-s1n authored Dec 6, 2024
1 parent 66d1244 commit 5dfd30c
Show file tree
Hide file tree
Showing 29 changed files with 453 additions and 584 deletions.
12 changes: 0 additions & 12 deletions adb_cli/src/commands/emu.rs

This file was deleted.

11 changes: 0 additions & 11 deletions adb_cli/src/commands/mod.rs

This file was deleted.

61 changes: 0 additions & 61 deletions adb_cli/src/commands/usb.rs

This file was deleted.

20 changes: 20 additions & 0 deletions adb_cli/src/handlers/emulator_commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use adb_client::ADBEmulatorDevice;

use crate::models::{EmuCommand, EmulatorCommand};

pub fn handle_emulator_commands(emulator_command: EmulatorCommand) -> anyhow::Result<()> {
let mut emulator = ADBEmulatorDevice::new(emulator_command.serial, None)?;

match emulator_command.command {
EmuCommand::Sms {
phone_number,
content,
} => {
emulator.send_sms(&phone_number, &content)?;
log::info!("SMS sent to {phone_number}");
}
EmuCommand::Rotate => emulator.rotate()?,
}

Ok(())
}
78 changes: 78 additions & 0 deletions adb_cli/src/handlers/host_commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use adb_client::{ADBServer, DeviceShort, MDNSBackend, Result};

use crate::models::{HostCommand, MdnsCommand, ServerCommand};

pub fn handle_host_commands(server_command: ServerCommand<HostCommand>) -> Result<()> {
let mut adb_server = ADBServer::new(server_command.address);

match server_command.command {
HostCommand::Version => {
let version = adb_server.version()?;
log::info!("Android Debug Bridge version {}", version);
log::info!("Package version {}-rust", std::env!("CARGO_PKG_VERSION"));
}
HostCommand::Kill => {
adb_server.kill()?;
}
HostCommand::Devices { long } => {
if long {
log::info!("List of devices attached (extended)");
for device in adb_server.devices_long()? {
log::info!("{}", device);
}
} else {
log::info!("List of devices attached");
for device in adb_server.devices()? {
log::info!("{}", device);
}
}
}
HostCommand::TrackDevices => {
let callback = |device: DeviceShort| {
log::info!("{}", device);
Ok(())
};
log::info!("Live list of devices attached");
adb_server.track_devices(callback)?;
}
HostCommand::Pair { address, code } => {
adb_server.pair(address, code)?;
log::info!("Paired device {address}");
}
HostCommand::Connect { address } => {
adb_server.connect_device(address)?;
log::info!("Connected to {address}");
}
HostCommand::Disconnect { address } => {
adb_server.disconnect_device(address)?;
log::info!("Disconnected {address}");
}
HostCommand::Mdns { subcommand } => match subcommand {
MdnsCommand::Check => {
let check = adb_server.mdns_check()?;
let server_status = adb_server.server_status()?;
match server_status.mdns_backend {
MDNSBackend::Unknown => log::info!("unknown mdns backend..."),
MDNSBackend::Bonjour => match check {
true => log::info!("mdns daemon version [Bonjour]"),
false => log::info!("ERROR: mdns daemon unavailable"),
},
MDNSBackend::OpenScreen => {
log::info!("mdns daemon version [Openscreen discovery 0.0.0]")
}
}
}
MdnsCommand::Services => {
log::info!("List of discovered mdns services");
for service in adb_server.mdns_services()? {
log::info!("{}", service);
}
}
},
HostCommand::ServerStatus => {
log::info!("{}", adb_server.server_status()?);
}
}

Ok(())
}
35 changes: 35 additions & 0 deletions adb_cli/src/handlers/local_commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{fs::File, io::Write};

use adb_client::ADBServerDevice;
use anyhow::{anyhow, Result};

use crate::models::LocalDeviceCommand;

pub fn handle_local_commands(
mut device: ADBServerDevice,
local_device_commands: LocalDeviceCommand,
) -> Result<()> {
match local_device_commands {
LocalDeviceCommand::HostFeatures => {
let features = device
.host_features()?
.iter()
.map(|v| v.to_string())
.reduce(|a, b| format!("{a},{b}"))
.ok_or(anyhow!("cannot list features"))?;
log::info!("Available host features: {features}");

Ok(())
}
LocalDeviceCommand::List { path } => Ok(device.list(path)?),
LocalDeviceCommand::Logcat { path } => {
let writer: Box<dyn Write> = if let Some(path) = path {
let f = File::create(path)?;
Box::new(f)
} else {
Box::new(std::io::stdout())
};
Ok(device.get_logs(writer)?)
}
}
}
7 changes: 7 additions & 0 deletions adb_cli/src/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod emulator_commands;
mod host_commands;
mod local_commands;

pub use emulator_commands::handle_emulator_commands;
pub use host_commands::handle_host_commands;
pub use local_commands::handle_local_commands;
Loading

0 comments on commit 5dfd30c

Please sign in to comment.