feat: fancy prints

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-07-09 03:55:38 +03:00
parent ccc77a09e3
commit 9d93e52ac5
5 changed files with 92 additions and 24 deletions

View file

@ -66,7 +66,7 @@ async fn serve((ws_sender, mut ws_receiver): (WebSocketSender, WebSocketReceiver
async fn execute(payload: Payload, debug: bool) -> Option<Output> { async fn execute(payload: Payload, debug: bool) -> Option<Output> {
if debug { if debug {
println!("{:#?}", payload); payload.print();
} }
let command = if cfg!(target_os = "windows") { let command = if cfg!(target_os = "windows") {
"cmd" "cmd"

View file

@ -11,17 +11,44 @@ pub enum Runner {
Server, Server,
Client, Client,
} }
impl Runner {
fn print(&self) {
match self {
Runner::Server => println!("Runner = Server"),
Runner::Client => println!("Runner = Client"),
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum RunnerMode { pub enum RunnerMode {
State(Runner, bool), State(Runner, bool),
} }
impl RunnerMode {
pub fn print(&self) {
match self {
RunnerMode::State(runner, debug) => {
runner.print();
println!("Debug = {}", debug);
}
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Config { pub struct Config {
pub ip: IpAddr, pub ip: IpAddr,
pub port: u16, pub port: u16,
} }
impl Config {
pub fn print(&self) {
println!("IP = {}", self.ip);
println!("Port = {}", self.port);
}
}
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Payload { pub struct Payload {
pub sudo: bool, pub sudo: bool,
@ -29,6 +56,14 @@ pub struct Payload {
pub args: String, pub args: String,
} }
impl Payload {
fn print(&self) {
println!("sudo = {}", self.sudo);
println!("user = {}", self.user);
println!("args = {}", self.args);
}
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Report { pub struct Report {
pub payload: Payload, pub payload: Payload,
@ -36,3 +71,24 @@ pub struct Report {
pub stdout: String, pub stdout: String,
pub stderr: String, pub stderr: String,
} }
impl Report {
fn print(&self) {
println!("-------");
println!("Payload ↓");
self.payload.print();
println!("-------");
if !self.status.is_empty() {
println!("Status ↓ \n{}", self.status);
println!("-------");
}
if !self.stdout.is_empty() {
println!("Stdout ↓ \n{}", self.stdout);
println!("-------");
}
if !self.stderr.is_empty() {
println!("Stderr ↓ \n{}", self.stderr);
println!("-------");
}
}
}

View file

@ -5,23 +5,32 @@ async fn main() {
println!("Hello, world!"); println!("Hello, world!");
let args = take_args(); let args = take_args();
println!("{:#?}", args);
match args { match args {
Some((runner_mode, config)) => match runner_mode { Some((runner_mode, config)) => {
RunnerMode::State(Runner::Server, false) => { println!("-------");
rust_remote::server::start(config, false).await println!("Runner Mode ↓");
runner_mode.print();
println!("-------");
println!("Config ↓");
config.print();
println!("-------");
match runner_mode {
RunnerMode::State(Runner::Server, false) => {
rust_remote::server::start(config, false).await
}
RunnerMode::State(Runner::Server, true) => {
rust_remote::server::start(config, true).await
}
RunnerMode::State(Runner::Client, false) => {
rust_remote::client::start(config, false).await
}
RunnerMode::State(Runner::Client, true) => {
rust_remote::client::start(config, true).await
}
} }
RunnerMode::State(Runner::Server, true) => { }
rust_remote::server::start(config, true).await
}
RunnerMode::State(Runner::Client, false) => {
rust_remote::client::start(config, false).await
}
RunnerMode::State(Runner::Client, true) => {
rust_remote::client::start(config, true).await
}
},
None => { None => {
eprintln!("Error: Take Args"); eprintln!("Error: Take Args");
return; return;

View file

@ -10,7 +10,7 @@ use tokio::{
}; };
use tokio_tungstenite::{accept_async, tungstenite::Message, WebSocketStream}; use tokio_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
use crate::{Config, Payload}; use crate::{Config, Payload, Report};
type WebSocketSender = SplitSink<WebSocketStream<TcpStream>, Message>; type WebSocketSender = SplitSink<WebSocketStream<TcpStream>, Message>;
type WebSocketReceiver = SplitStream<WebSocketStream<TcpStream>>; type WebSocketReceiver = SplitStream<WebSocketStream<TcpStream>>;
@ -34,8 +34,13 @@ pub async fn start(config: Config, debug: bool) {
break; break;
} }
tokio::spawn(async move { tokio::spawn(async move {
let report = receive(ws_receiver, debug).await; match receive(ws_receiver, debug).await {
println!("{:#?}", report); Some(report) => match serde_json::from_str::<Report>(&report) {
Ok(report) => report.print(),
Err(_) => {}
},
None => todo!(),
}
}); });
} }
None => continue, None => continue,
@ -83,7 +88,7 @@ async fn establish_connection(
async fn payload_from_input(debug: bool) -> Option<Payload> { async fn payload_from_input(debug: bool) -> Option<Payload> {
println!("User"); println!("User");
// let user = match get_input() { // let user = match get_input(debug) {
// Some(input) => input, // Some(input) => input,
// None => return None, // None => return None,
// }; // };

View file

@ -4,7 +4,7 @@ use crate::{Config, Runner, RunnerMode};
pub fn take_args() -> Option<(RunnerMode, Config)> { pub fn take_args() -> Option<(RunnerMode, Config)> {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
let mut runner = Runner::Server; let mut runner = Runner::Client;
let mut debug = false; let mut debug = false;
let mut ip = "127.0.0.1".to_string(); let mut ip = "127.0.0.1".to_string();
let mut port = "3444".to_string(); let mut port = "3444".to_string();
@ -30,8 +30,6 @@ pub fn take_args() -> Option<(RunnerMode, Config)> {
} }
}; };
println!("{:#?}", ip);
let port = match port.parse::<u16>() { let port = match port.parse::<u16>() {
Ok(port) => port, Ok(port) => port,
Err(err_val) => { Err(err_val) => {
@ -52,8 +50,8 @@ fn show_help() {
println!("----------------------------------------------------------------------"); println!("----------------------------------------------------------------------");
println!(" -i -> --ip | Specifies IP Address | 127.0.0.1"); println!(" -i -> --ip | Specifies IP Address | 127.0.0.1");
println!(" -p -> --port | Specifies Port Address | 3444"); println!(" -p -> --port | Specifies Port Address | 3444");
println!(" -sv -> --server | Starts as a Server | True"); println!(" -sv -> --server | Starts as a Server | False");
println!(" -cl -> --client | Starts as a Client | False"); println!(" -cl -> --client | Starts as a Client | True");
println!(" -d -> --debug | Starts in Debug Mode | False"); println!(" -d -> --debug | Starts in Debug Mode | False");
println!(" -h -> --help | Shows Help | False"); println!(" -h -> --help | Shows Help | False");
println!("\n\n\n"); println!("\n\n\n");