feat: ✨ every parameter comes from start arguments instead of config file
This commit is contained in:
parent
ff4cf381fa
commit
b0eb5dc4c8
6 changed files with 77 additions and 89 deletions
|
@ -1,2 +0,0 @@
|
|||
server_address:127.0.0.1
|
||||
port:2444
|
|
@ -28,26 +28,22 @@ pub async fn start(config: Config, debug: bool) {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn connect(config: &Config, debug: bool) -> Option<(WebSocketSender, WebSocketReceiver)> {
|
||||
let ws_connection =
|
||||
match connect_async(format!("ws://{}:{}", config.server_address, config.port)).await {
|
||||
Ok(ws_connection) => ws_connection,
|
||||
Err(err_val) => {
|
||||
if debug {
|
||||
eprintln!("Error: WebSocket Connection | {}", err_val);
|
||||
}
|
||||
return None;
|
||||
async fn connect(config: &Config, debug: bool) -> Option<(WebSocketSender, WebSocketReceiver)> {
|
||||
let ws_connection = match connect_async(format!("ws://{}:{}", config.ip, config.port)).await {
|
||||
Ok(ws_connection) => ws_connection,
|
||||
Err(err_val) => {
|
||||
if debug {
|
||||
eprintln!("Error: WebSocket Connection | {}", err_val);
|
||||
}
|
||||
};
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
let (ws_sender, ws_receiver) = ws_connection.0.split();
|
||||
Some((ws_sender, ws_receiver))
|
||||
}
|
||||
|
||||
pub async fn serve(
|
||||
(ws_sender, mut ws_receiver): (WebSocketSender, WebSocketReceiver),
|
||||
debug: bool,
|
||||
) {
|
||||
async fn serve((ws_sender, mut ws_receiver): (WebSocketSender, WebSocketReceiver), debug: bool) {
|
||||
let ws_sender = Arc::new(Mutex::new(ws_sender));
|
||||
while let Some(message) = receive(&mut ws_receiver, debug).await {
|
||||
match serde_json::from_str::<Payload>(&message[..]) {
|
||||
|
@ -68,7 +64,7 @@ pub async fn serve(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn execute(payload: Payload, debug: bool) -> Option<Output> {
|
||||
async fn execute(payload: Payload, debug: bool) -> Option<Output> {
|
||||
if debug {
|
||||
println!("{:#?}", payload);
|
||||
}
|
||||
|
@ -87,7 +83,7 @@ pub async fn execute(payload: Payload, debug: bool) -> Option<Output> {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn receive(ws_receiver: &mut WebSocketReceiver, debug: bool) -> Option<String> {
|
||||
async fn receive(ws_receiver: &mut WebSocketReceiver, debug: bool) -> Option<String> {
|
||||
match ws_receiver.next().await {
|
||||
Some(message) => match message {
|
||||
Ok(message) => {
|
||||
|
@ -116,7 +112,7 @@ pub async fn receive(ws_receiver: &mut WebSocketReceiver, debug: bool) -> Option
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn send(
|
||||
async fn send(
|
||||
output: Option<Output>,
|
||||
payload: Payload,
|
||||
ws_sender: Arc<Mutex<WebSocketSender>>,
|
||||
|
|
|
@ -6,18 +6,19 @@ pub mod client;
|
|||
pub mod server;
|
||||
pub mod utils;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Runner {
|
||||
Server,
|
||||
Client,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum RunnerMode {
|
||||
State(Runner, bool),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
pub server_address: IpAddr,
|
||||
pub ip: IpAddr,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -1,22 +1,14 @@
|
|||
use rust_remote::{
|
||||
utils::{read_config, take_args},
|
||||
Runner, RunnerMode,
|
||||
};
|
||||
use rust_remote::{utils::take_args, Runner, RunnerMode};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let config = match read_config() {
|
||||
Some(config) => config,
|
||||
None => {
|
||||
eprintln!("Error: Read Config");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let args = take_args();
|
||||
println!("{:#?}", args);
|
||||
|
||||
match take_args() {
|
||||
Some(runner_mode) => match runner_mode {
|
||||
match args {
|
||||
Some((runner_mode, config)) => match runner_mode {
|
||||
RunnerMode::State(Runner::Server, false) => {
|
||||
rust_remote::server::start(config, false).await
|
||||
}
|
||||
|
|
|
@ -46,8 +46,8 @@ pub async fn start(config: Config, debug: bool) {
|
|||
}
|
||||
}
|
||||
}
|
||||
pub async fn setup(config: Config, debug: bool) -> Option<TcpListener> {
|
||||
match TcpListener::bind(format!("{}:{}", config.server_address, config.port)).await {
|
||||
async fn setup(config: Config, debug: bool) -> Option<TcpListener> {
|
||||
match TcpListener::bind(format!("{}:{}", config.ip, config.port)).await {
|
||||
Ok(listener) => Some(listener),
|
||||
Err(err_val) => {
|
||||
if debug {
|
||||
|
@ -58,7 +58,7 @@ pub async fn setup(config: Config, debug: bool) -> Option<TcpListener> {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn establish_connection(
|
||||
async fn establish_connection(
|
||||
listener: &TcpListener,
|
||||
debug: bool,
|
||||
) -> Option<(WebSocketSender, WebSocketReceiver)> {
|
||||
|
@ -81,7 +81,7 @@ pub async fn establish_connection(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn payload_from_input(debug: bool) -> Option<Payload> {
|
||||
async fn payload_from_input(debug: bool) -> Option<Payload> {
|
||||
println!("User");
|
||||
// let user = match get_input() {
|
||||
// Some(input) => input,
|
||||
|
@ -116,7 +116,7 @@ pub async fn payload_from_input(debug: bool) -> Option<Payload> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_input(debug: bool) -> Option<String> {
|
||||
fn get_input(debug: bool) -> Option<String> {
|
||||
let mut payload_input: String = String::new();
|
||||
match stdin().read_line(&mut payload_input) {
|
||||
Ok(_) => Some(payload_input.trim().to_string()),
|
||||
|
@ -129,7 +129,7 @@ pub fn get_input(debug: bool) -> Option<String> {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn receive(ws_receiver: Arc<Mutex<WebSocketReceiver>>, debug: bool) -> Option<String> {
|
||||
async fn receive(ws_receiver: Arc<Mutex<WebSocketReceiver>>, debug: bool) -> Option<String> {
|
||||
match ws_receiver.lock().await.next().await {
|
||||
Some(message) => match message {
|
||||
Ok(message) => {
|
||||
|
@ -158,7 +158,7 @@ pub async fn receive(ws_receiver: Arc<Mutex<WebSocketReceiver>>, debug: bool) ->
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn send(payload: Payload, ws_sender: Arc<Mutex<WebSocketSender>>, debug: bool) -> bool {
|
||||
async fn send(payload: Payload, ws_sender: Arc<Mutex<WebSocketSender>>, debug: bool) -> bool {
|
||||
let payload = serde_json::json!(payload);
|
||||
let result = ws_sender
|
||||
.lock()
|
||||
|
|
97
src/utils.rs
97
src/utils.rs
|
@ -1,57 +1,58 @@
|
|||
use std::{env, fs::File, io::Read};
|
||||
use std::{env, net::IpAddr};
|
||||
|
||||
use crate::{Config, Runner, RunnerMode};
|
||||
|
||||
pub fn take_args() -> Option<RunnerMode> {
|
||||
pub fn take_args() -> Option<(RunnerMode, Config)> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() > 1 {
|
||||
let runner = match &args[1][..] {
|
||||
"--server" => Runner::Server,
|
||||
"--client" => Runner::Client,
|
||||
_ => return None,
|
||||
};
|
||||
let debug = if args.len() > 2 {
|
||||
match &args[2][..] {
|
||||
"--debug" => true,
|
||||
_ => return None,
|
||||
let mut runner = Runner::Server;
|
||||
let mut debug = false;
|
||||
let mut ip = "127.0.0.1".to_string();
|
||||
let mut port = "3444".to_string();
|
||||
for (i, arg) in args.iter().enumerate() {
|
||||
match arg.as_str() {
|
||||
"--server" | "-sv" => runner = Runner::Server,
|
||||
"--client" | "-cl" => runner = Runner::Client,
|
||||
"--debug" | "-d" => debug = true,
|
||||
"--ip" | "i" => ip = args[i + 1].clone(),
|
||||
"--port" | "p" => port = args[i + 1].clone(),
|
||||
"--help" | "-h" => {
|
||||
show_help();
|
||||
std::process::exit(0);
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
Some(RunnerMode::State(runner, debug))
|
||||
} else {
|
||||
None
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
let ip = match ip.parse::<IpAddr>() {
|
||||
Ok(ip) => ip,
|
||||
Err(err_val) => {
|
||||
eprintln!("Error: IP Parse | {}", err_val);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
let port = match port.parse::<u16>() {
|
||||
Ok(port) => port,
|
||||
Err(err_val) => {
|
||||
eprintln!("Error: Port Parse | {}", err_val);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
let config = Config { ip, port };
|
||||
|
||||
let runner_mode = RunnerMode::State(runner, debug);
|
||||
Some((runner_mode, config))
|
||||
}
|
||||
|
||||
pub fn read_config() -> Option<Config> {
|
||||
let mut config_file = match File::open("configs/config.txt") {
|
||||
Ok(config_file) => config_file,
|
||||
Err(_) => return None,
|
||||
};
|
||||
let mut configs = String::new();
|
||||
match config_file.read_to_string(&mut configs) {
|
||||
Ok(_) => {
|
||||
let configs: Vec<String> = configs.split('\n').map(|x| x.to_string()).collect();
|
||||
let server_address = match configs[0].split(':').last() {
|
||||
Some(server_address_unchecked) => match server_address_unchecked.parse() {
|
||||
Ok(server_address) => server_address,
|
||||
Err(_) => return None,
|
||||
},
|
||||
None => return None,
|
||||
};
|
||||
let port = match configs[1].split(':').last() {
|
||||
Some(port_unchecked) => match port_unchecked.parse() {
|
||||
Ok(port) => port,
|
||||
Err(_) => return None,
|
||||
},
|
||||
None => return None,
|
||||
};
|
||||
Some(Config {
|
||||
server_address,
|
||||
port,
|
||||
})
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
fn show_help() {
|
||||
println!("\n\n\n");
|
||||
println!(" Arguments | Details | Defaults");
|
||||
println!("----------------------------------------------------------------------");
|
||||
println!(" -i -> --ip | Specifies IP Address | 127.0.0.1");
|
||||
println!(" -p -> --port | Specifies Port Address | 3444");
|
||||
println!(" -sv -> --server | Starts as a Server | True");
|
||||
println!(" -cl -> --client | Starts as a Client | False");
|
||||
println!(" -d -> --debug | Starts in Debug Mode | False");
|
||||
println!(" -h -> --help | Shows Help | False");
|
||||
println!("\n\n\n");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue