Add fetch-channel command

This commit is contained in:
Tobias Reisinger 2023-11-17 17:32:15 +01:00
parent 970c1ee2c2
commit 244a7073fe
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
9 changed files with 183 additions and 54 deletions

View file

@ -8,6 +8,29 @@ mod cli;
use std::process::exit;
use telnet::Telnet;
use crate::cli::Commands;
use crate::response_classes::{ResponseChannel, ResponseClient};
fn channel_or_exit(channel_res: Result<Option<ResponseChannel>, String>) -> ResponseChannel {
channel_res.unwrap_or_else(|err| {
println!("Failed to find channel: {}", err);
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find channel.");
exit(1);
})
}
fn client_or_exit(client_res: Result<Option<ResponseClient>, String>) -> ResponseClient {
client_res.unwrap_or_else(|err| {
println!("Failed to find client: {}", err);
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find client.");
exit(1);
})
}
fn main() {
@ -55,42 +78,43 @@ fn main() {
}
Commands::Fetch(args) => {
let client = args.client(&mut connection).unwrap_or_else(|err| {
println!("Failed to find client for move: {}", err);
if args.want_client() && args.want_channel() {
println!("Fetching both clients and channels is not supported.");
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find client for move.");
exit(1);
});
}
if !args.want_client() && !args.want_channel() {
println!("No clients or channels specified.");
exit(1);
}
match utils::fetch_client(&mut connection, &[client]) {
Ok(resp) => println!("Successfully fetched client: {}", resp),
Err(msg) => {
println!("Failed to fetch client: {}", msg);
exit(1);
if args.want_client() {
let client = client_or_exit(args.client(&mut connection));
match utils::fetch_client(&mut connection, &[client]) {
Ok(_) => println!("Successfully fetched client."),
Err(msg) => {
println!("Failed to fetch client: {}", msg);
exit(1);
}
}
}
if args.want_channel() {
let channel = channel_or_exit(args.channel(&mut connection));
match utils::fetch_channel(&mut connection, channel) {
Ok(_) => println!("Successfully fetched channel."),
Err(msg) => {
println!("Failed to fetch channel: {}", msg);
exit(1);
}
}
}
}
Commands::Move(args) => {
let channel = args.channel(&mut connection).unwrap_or_else(|err| {
println!("Failed to find channel for move: {}", err);
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find channel for move.");
exit(1);
});
let client = args.client(&mut connection).unwrap_or_else(|err| {
println!("Failed to find client for move: {}", err);
exit(1);
})
.unwrap_or_else(|| {
println!("Failed to find client for move.");
exit(1);
});
let channel = channel_or_exit(args.channel(&mut connection));
let client = client_or_exit(args.client(&mut connection));
match utils::move_client(&mut connection, &channel, &[client]) {
Ok(resp) => println!("Successfully moved client: {}", resp),