Init
This commit is contained in:
commit
13abafae9d
12 changed files with 1095 additions and 0 deletions
src
119
src/cli.rs
Normal file
119
src/cli.rs
Normal file
|
@ -0,0 +1,119 @@
|
|||
use clap::{Parser, Subcommand, Args};
|
||||
use telnet::Telnet;
|
||||
use crate::parameter::{Parameter, ParameterList};
|
||||
use crate::response_classes::{ResponseChannel, ResponseClient};
|
||||
use crate::utils;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
#[command(propagate_version = true)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
Channels(ChannelsArgs),
|
||||
Clients,
|
||||
Fetch(FetchArgs),
|
||||
Move(MoveArgs),
|
||||
Update(UpdateArgs),
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct ChannelsArgs {
|
||||
#[arg(long)]
|
||||
pub spacers: bool,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct FetchArgs {
|
||||
#[arg(long)]
|
||||
strict_client: bool,
|
||||
client: String,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct MoveArgs {
|
||||
#[arg(long)]
|
||||
strict_client: bool,
|
||||
#[arg(long)]
|
||||
strict_channel: bool,
|
||||
channel: String,
|
||||
client: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct UpdateArgs {
|
||||
#[arg(long, short)]
|
||||
name: Option<String>,
|
||||
#[arg(long, short)]
|
||||
away: Option<String>,
|
||||
#[arg(long, short)]
|
||||
back: bool,
|
||||
#[arg(long, short)]
|
||||
microphone: Option<bool>,
|
||||
#[arg(long, short)]
|
||||
speakers: Option<bool>,
|
||||
}
|
||||
|
||||
|
||||
impl FetchArgs {
|
||||
pub fn client(&self, connection: &mut Telnet) -> Result<Option<ResponseClient>, String> {
|
||||
utils::find_client(connection, &self.client, self.strict_client)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl MoveArgs {
|
||||
pub fn channel(&self, connection: &mut Telnet) -> Result<Option<ResponseChannel>, String> {
|
||||
utils::find_channel(connection, &self.channel, self.strict_channel)
|
||||
}
|
||||
pub fn client(&self, connection: &mut Telnet) -> Result<Option<ResponseClient>, String> {
|
||||
match &self.client {
|
||||
Some(client) => {
|
||||
utils::find_client(connection, client, self.strict_client)
|
||||
}
|
||||
None => {
|
||||
match utils::find_self(connection) {
|
||||
Ok(client) => Ok(Some(client)),
|
||||
Err(msg) => Err(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UpdateArgs {
|
||||
pub fn to_parameter_list(&self) -> ParameterList {
|
||||
let mut params: ParameterList = Vec::new();
|
||||
|
||||
if let Some(name) = &self.name {
|
||||
params.push(Parameter::new(String::from("client_nickname"), name.clone()));
|
||||
}
|
||||
|
||||
if let Some(away) = &self.away {
|
||||
params.push(Parameter::new(String::from("client_away_message"), away.clone()));
|
||||
params.push(Parameter::new(String::from("client_away"), String::from("1")));
|
||||
}
|
||||
if self.back {
|
||||
params.push(Parameter::new(String::from("client_away"), String::from("0")));
|
||||
}
|
||||
|
||||
if let Some(microphone) = self.microphone {
|
||||
let muted = u8::from(!microphone).to_string();
|
||||
params.push(Parameter::new(String::from("client_input_muted"), muted));
|
||||
}
|
||||
if let Some(speakers) = self.speakers {
|
||||
let muted = u8::from(!speakers).to_string();
|
||||
params.push(Parameter::new(String::from("client_output_muted"), muted));
|
||||
}
|
||||
|
||||
params
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init() -> Commands {
|
||||
Cli::parse().command
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue