-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathpower_off.rs
More file actions
72 lines (67 loc) · 2.37 KB
/
Copy pathpower_off.rs
File metadata and controls
72 lines (67 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use clap::Args;
use color_eyre::{Result, eyre::WrapErr};
use argument_parsers::{Connection, RobotAddress, number_to_ip};
use futures_util::{StreamExt, stream::FuturesUnordered};
use repository::Repository;
use robot::Robot;
use crate::progress_indicator::ProgressIndicator;
#[derive(Args)]
pub struct Arguments {
/// Power off all Robots
#[arg(long, visible_alias = "alle")]
pub all: bool,
/// The Robots to power off e.g. 20w or 10.1.24.22
#[arg(required = true, conflicts_with = "all", num_args = 1..)]
pub robots: Vec<RobotAddress>,
}
pub async fn power_off(arguments: Arguments, repository: &Repository) -> Result<()> {
if arguments.all {
let team = repository
.read_team_configuration()
.await
.wrap_err("failed to get team configuration")?;
let addresses = team
.robots
.iter()
.map(|robot| async move {
let host = number_to_ip(robot.number, Connection::Wired)?;
match Robot::try_new_with_ping(host).await {
Ok(robot) => Ok(robot),
Err(_) => {
let host = number_to_ip(robot.number, Connection::Wireless)?;
Robot::try_new_with_ping(host).await
}
}
})
.collect::<FuturesUnordered<_>>()
.collect::<Vec<_>>()
.await;
ProgressIndicator::new()
.map_tasks(
addresses.into_iter().filter_map(|robot| robot.ok()),
"Powering off...",
|robot, _progress_bar| async move {
robot
.power_off()
.await
.wrap_err_with(|| format!("failed to power {robot} off"))
},
)
.await;
} else {
ProgressIndicator::new()
.map_tasks(
arguments.robots,
"Powering off...",
|robot_address, _progress_bar| async move {
let robot = Robot::try_new_with_ping(robot_address.ip).await?;
robot
.power_off()
.await
.wrap_err_with(|| format!("failed to power {robot_address} off"))
},
)
.await;
}
Ok(())
}