Refactor and add message command

This commit is contained in:
Tobias Reisinger 2023-11-26 22:30:59 +01:00
parent e87bed00a2
commit fa959136be
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
8 changed files with 370 additions and 263 deletions

View file

@ -7,10 +7,11 @@ use crate::response::channel::ResponseChannel;
use crate::response::client::ResponseClient;
mod response;
mod utils;
mod wrappers;
mod commands;
mod parameter;
mod cli;
mod utils;
fn channel_or_exit(channel_res: Result<Option<ResponseChannel>, String>) -> ResponseChannel {
channel_res.unwrap_or_else(|err| {
@ -44,14 +45,14 @@ fn main() {
}
let mut connection = connection.unwrap();
utils::skip_welcome(&mut connection);
utils::login(&mut connection);
wrappers::skip_welcome(&mut connection);
wrappers::login(&mut connection);
// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
match &cli {
match cli {
Commands::Channels(args) => {
match utils::get_channels(&mut connection, args.spacers) {
match wrappers::get_channels(&mut connection, args.spacers) {
Ok(channels) => {
for channel in channels {
println!("{}", channel.channel_name);
@ -65,7 +66,7 @@ fn main() {
}
Commands::Clients => {
match utils::get_clients(&mut connection) {
match wrappers::get_clients(&mut connection) {
Ok(clients) => {
for client in clients {
println!("{}", client.client_nickname);
@ -91,7 +92,7 @@ fn main() {
if args.want_client() {
let client = client_or_exit(args.client(&mut connection));
match utils::fetch_client(&mut connection, &[client]) {
match wrappers::fetch_client(&mut connection, &[client]) {
Ok(_) => println!("Successfully fetched client."),
Err(msg) => {
println!("Failed to fetch client: {}", msg);
@ -103,7 +104,7 @@ fn main() {
if args.want_channel() {
let channel = channel_or_exit(args.channel(&mut connection));
match utils::fetch_channel(&mut connection, channel) {
match wrappers::fetch_channel(&mut connection, channel) {
Ok(_) => println!("Successfully fetched channel."),
Err(msg) => {
println!("Failed to fetch channel: {}", msg);
@ -113,11 +114,26 @@ fn main() {
}
}
Commands::Message(args) => {
let target = args.target(&mut connection).unwrap_or_else(|err| {
println!("Failed to get message target: {}", err);
exit(1);
});
match wrappers::send_text_message(&mut connection, target, args.message) {
Ok(_) => println!("Successfully sent message."),
Err(msg) => {
println!("Failed to send message: {}", msg);
exit(1);
}
}
}
Commands::Move(args) => {
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]) {
match wrappers::move_client(&mut connection, &channel, &[client]) {
Ok(resp) => println!("Successfully moved client: {}", resp),
Err(msg) => {
println!("Failed to move client: {}", msg);
@ -127,7 +143,7 @@ fn main() {
}
Commands::Update(args) => {
match utils::update_client(&mut connection, &args.to_parameter_list()) {
match wrappers::update_client(&mut connection, args.to_parameter_list()) {
Ok(_) => println!("Successfully updated client."),
Err(msg) => {
println!("Failed to update client: {}", msg);