Skip to content

Commit 984e9b0

Browse files
committed
cleanup scenarios fns
1 parent ccc778f commit 984e9b0

File tree

1 file changed

+120
-79
lines changed

1 file changed

+120
-79
lines changed

src/rust-cli/scenarios.rs

Lines changed: 120 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -25,111 +25,152 @@ pub enum ScenarioCommand {
2525
/// Stop a scenario with <PID>
2626
Stop { pid: u64 },
2727
}
28+
async fn handle_available(params: ObjectParams) -> anyhow::Result<()> {
29+
let data = make_rpc_call("scenarios_available", params)
30+
.await
31+
.context("Failed to fetch available scenarios")?;
32+
if let serde_json::Value::Array(scenarios) = data {
33+
let mut table = Table::new();
34+
table.add_row(row!["Scenario", "Description"]);
35+
for scenario in scenarios {
36+
if let serde_json::Value::Array(details) = scenario {
37+
if details.len() == 2 {
38+
let name = details[0].as_str().unwrap_or("Unknown");
39+
let description = details[1].as_str().unwrap_or("No description");
40+
table.add_row(row![name, description]);
41+
}
42+
}
43+
}
44+
table.printstd();
45+
} else {
46+
println!("Unexpected response format.");
47+
}
48+
Ok(())
49+
}
50+
51+
async fn handle_run(
52+
mut params: ObjectParams,
53+
scenario: &str,
54+
additional_args: &Vec<String>,
55+
) -> anyhow::Result<()> {
56+
params
57+
.insert("scenario", scenario)
58+
.context("Add scenario to params")?;
59+
params
60+
.insert("additional_args", additional_args)
61+
.context("Add additional_args to params")?;
62+
let data = make_rpc_call("scenarios_run", params)
63+
.await
64+
.context("Failed to run scenario")?;
65+
println!("{:?}", data);
66+
Ok(())
67+
}
68+
69+
async fn handle_run_file(
70+
mut params: ObjectParams,
71+
scenario_path: &PathBuf,
72+
additional_args: &Vec<String>,
73+
) -> anyhow::Result<()> {
74+
let file_contents = std::fs::read(scenario_path).context("Failed to read scenario file")?;
75+
let scenario_base64 = general_purpose::STANDARD.encode(file_contents);
76+
params
77+
.insert("scenario_base64", scenario_base64)
78+
.context("Add scenario to params")?;
79+
params
80+
.insert("additional_args", additional_args)
81+
.context("Add additional_args to params")?;
82+
let data = make_rpc_call("scenarios_run_file", params)
83+
.await
84+
.context("Failed to run scenario")?;
85+
println!("{:?}", data);
86+
Ok(())
87+
}
88+
89+
async fn handle_active(params: ObjectParams) -> anyhow::Result<()> {
90+
let data = make_rpc_call("scenarios_list_running", params)
91+
.await
92+
.context("Failed to list running scenarios")?;
93+
if let serde_json::Value::Array(scenarios) = data {
94+
let mut table = Table::new();
95+
table.add_row(row!["PID", "Command", "Network", "Active"]);
96+
for scenario in scenarios {
97+
if let serde_json::Value::Object(details) = scenario {
98+
let pid = details
99+
.get("pid")
100+
.and_then(|v| v.as_i64())
101+
.map_or_else(|| "Unknown".to_string(), |v| v.to_string());
102+
let cmd = details
103+
.get("cmd")
104+
.and_then(|v| v.as_str())
105+
.unwrap_or("Unknown");
106+
let network = details
107+
.get("network")
108+
.and_then(|v| v.as_str())
109+
.unwrap_or("Unknown");
110+
let active = details
111+
.get("active")
112+
.and_then(|v| v.as_bool())
113+
.map_or_else(|| "Unknown".to_string(), |v| v.to_string());
114+
table.add_row(row![pid, cmd, network, active]);
115+
}
116+
}
117+
table.printstd();
118+
} else {
119+
println!("Unexpected response format.");
120+
}
121+
Ok(())
122+
}
123+
124+
async fn handle_stop(mut params: ObjectParams, pid: &u64) -> anyhow::Result<()> {
125+
params.insert("pid", pid).context("Add pid to params")?;
126+
let data = make_rpc_call("scenarios_stop", params)
127+
.await
128+
.context("Failed to stop running scenario")?;
129+
if let serde_json::Value::String(message) = data {
130+
println!("{}", message);
131+
} else {
132+
println!("Unexpected response format.");
133+
}
134+
Ok(())
135+
}
28136

29137
pub async fn handle_scenario_command(
30138
command: &ScenarioCommand,
31-
mut params: ObjectParams,
139+
params: ObjectParams,
32140
) -> anyhow::Result<()> {
33141
match command {
34142
ScenarioCommand::Available {} => {
35-
let data = make_rpc_call("scenarios_available", params)
143+
handle_available(params)
36144
.await
37-
.context("Failed to fetch available scenarios")?;
38-
if let serde_json::Value::Array(scenarios) = data {
39-
let mut table = Table::new();
40-
table.add_row(row!["Scenario", "Description"]);
41-
for scenario in scenarios {
42-
if let serde_json::Value::Array(details) = scenario {
43-
if details.len() == 2 {
44-
let name = details[0].as_str().unwrap_or("Unknown");
45-
let description = details[1].as_str().unwrap_or("No description");
46-
table.add_row(row![name, description]);
47-
}
48-
}
49-
}
50-
table.printstd();
51-
} else {
52-
println!("Unexpected response format.");
53-
}
145+
.context("List available scenarios")?;
54146
}
55147
ScenarioCommand::Run {
56148
scenario,
57149
additional_args,
58150
} => {
59-
params
60-
.insert("scenario", scenario)
61-
.context("Add scenario to params")?;
62-
params
63-
.insert("additional_args", additional_args)
64-
.context("Add additional_args to params")?;
65-
let data = make_rpc_call("scenarios_run", params)
151+
handle_run(params, scenario, additional_args)
66152
.await
67-
.context("Failed to run scenario")?;
68-
println!("{:?}", data);
153+
.context("Run scenario from remote")?;
69154
}
70155

71156
ScenarioCommand::RunFile {
72157
scenario_path,
73158
additional_args,
74159
} => {
75-
let file_contents =
76-
std::fs::read(scenario_path).context("Failed to read scenario file")?;
77-
let scenario_base64 = general_purpose::STANDARD.encode(file_contents);
78-
params
79-
.insert("scenario_base64", scenario_base64)
80-
.context("Add scenario to params")?;
81-
params
82-
.insert("additional_args", additional_args)
83-
.context("Add additional_args to params")?;
84-
let data = make_rpc_call("scenarios_run_file", params)
160+
handle_run_file(params, scenario_path, additional_args)
85161
.await
86-
.context("Failed to run scenario")?;
87-
println!("{:?}", data);
162+
.context("Run scenario file from path")?;
88163
}
89164
ScenarioCommand::Active {} => {
90-
let data = make_rpc_call("scenarios_list_running", params)
165+
handle_active(params)
91166
.await
92-
.context("Failed to list running scenarios")?;
93-
if let serde_json::Value::Array(scenarios) = data {
94-
let mut table = Table::new();
95-
table.add_row(row!["PID", "Command", "Network", "Active"]);
96-
for scenario in scenarios {
97-
if let serde_json::Value::Object(details) = scenario {
98-
let pid = details
99-
.get("pid")
100-
.and_then(|v| v.as_i64())
101-
.map_or_else(|| "Unknown".to_string(), |v| v.to_string());
102-
let cmd = details
103-
.get("cmd")
104-
.and_then(|v| v.as_str())
105-
.unwrap_or("Unknown");
106-
let network = details
107-
.get("network")
108-
.and_then(|v| v.as_str())
109-
.unwrap_or("Unknown");
110-
let active = details
111-
.get("active")
112-
.and_then(|v| v.as_bool())
113-
.map_or_else(|| "Unknown".to_string(), |v| v.to_string());
114-
table.add_row(row![pid, cmd, network, active]);
115-
}
116-
}
117-
table.printstd();
118-
} else {
119-
println!("Unexpected response format.");
120-
}
167+
.context("List active scenarios")?;
121168
}
122169
ScenarioCommand::Stop { pid } => {
123-
params.insert("pid", pid).context("Add pid to params")?;
124-
let data = make_rpc_call("scenarios_stop", params)
170+
handle_stop(params, pid)
125171
.await
126-
.context("Failed to stop running scenario")?;
127-
if let serde_json::Value::String(message) = data {
128-
println!("{}", message);
129-
} else {
130-
println!("Unexpected response format.");
131-
}
172+
.context(format!("Stop running scenario with pid: {}", pid))?;
132173
}
133-
}
174+
};
134175
Ok(())
135176
}

0 commit comments

Comments
 (0)