|
| 1 | +use std::process::Stdio; |
| 2 | +use tokio::process::Command; |
| 3 | + |
| 4 | +use super::{Driver, Status}; |
| 5 | +use crate::blocks::prelude::*; |
| 6 | + |
| 7 | +pub struct WarpDriver; |
| 8 | + |
| 9 | +impl WarpDriver { |
| 10 | + pub async fn new() -> WarpDriver { |
| 11 | + WarpDriver |
| 12 | + } |
| 13 | + |
| 14 | + async fn run_network_command(arg: &str) -> Result<()> { |
| 15 | + Command::new("warp-cli") |
| 16 | + .args([arg]) |
| 17 | + .stdin(Stdio::null()) |
| 18 | + .stdout(Stdio::null()) |
| 19 | + .spawn() |
| 20 | + .error(format!("Problem running warp-cli command: {arg}"))? |
| 21 | + .wait() |
| 22 | + .await |
| 23 | + .error(format!("Problem running warp-cli command: {arg}"))?; |
| 24 | + Ok(()) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[async_trait] |
| 29 | +impl Driver for WarpDriver { |
| 30 | + async fn get_status(&self) -> Result<Status> { |
| 31 | + let stdout = Command::new("warp-cli") |
| 32 | + .args(["status"]) |
| 33 | + .output() |
| 34 | + .await |
| 35 | + .error("Problem running warp-cli command")? |
| 36 | + .stdout; |
| 37 | + |
| 38 | + let status = String::from_utf8(stdout).error("warp-cli produced non-UTF8 output")?; |
| 39 | + |
| 40 | + if status.contains("Status update: Disconnected") { |
| 41 | + return Ok(Status::Disconnected); |
| 42 | + } else if status.contains("Status update: Connected") { |
| 43 | + return Ok(Status::Connected { |
| 44 | + country: "".to_string(), // because warp-cli doesn't provide country/server info |
| 45 | + country_flag: "".to_string(), // no country means no flag |
| 46 | + }); |
| 47 | + } |
| 48 | + Ok(Status::Error) |
| 49 | + } |
| 50 | + |
| 51 | + async fn toggle_connection(&self, status: &Status) -> Result<()> { |
| 52 | + match status { |
| 53 | + Status::Connected { .. } => Self::run_network_command("disconnect").await?, |
| 54 | + Status::Disconnected => Self::run_network_command("connect").await?, |
| 55 | + Status::Error => (), |
| 56 | + } |
| 57 | + Ok(()) |
| 58 | + } |
| 59 | +} |
0 commit comments