Error handling, specify host

main
~erin 2023-04-02 23:29:32 -04:00
parent a51d5808db
commit a9fb779c35
Signed by: erin
GPG Key ID: 9A8E308CEFA37A47
1 changed files with 33 additions and 8 deletions

View File

@ -3,6 +3,7 @@ use paris::{error, info, success};
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fmt;
use std::process::exit;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
use url::Url;
@ -12,6 +13,12 @@ use uuid::Uuid;
#[command(name = "forge-server")]
#[command(about = "A simple remote build system", long_about = None)]
struct Cli {
#[arg(value_name = "IP")]
host: String,
#[arg(value_name = "PORT", default_value_t = 9134)]
port: u16,
#[command(subcommand)]
command: Commands,
}
@ -135,8 +142,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
tag,
} => {
// Connect to a peer
let mut stream = TcpStream::connect("127.0.0.1:9134").await?;
success!("Connected to: <green>127.0.0.1:9134<//>");
let remote = format!("{}:{}", args.host, args.port);
let mut stream = match TcpStream::connect(&remote).await {
Ok(s) => s,
Err(e) => {
error!("Could not connect to remote host: {}", e);
exit(1);
}
};
success!("Connected to: <green>{}<//>", &remote);
let test_command = Command {
build_system: build,
@ -154,12 +168,23 @@ async fn main() -> Result<(), Box<dyn Error>> {
basename: "serde_json".to_string(),
};
let j = serde_json::to_string(&test_message)?;
stream.write_all(&j.as_bytes()).await?;
info!(
"Sent JSON:\n<bright-white>{}<//>",
serde_json::to_string_pretty(&test_message)?
);
let j = match serde_json::to_string(&test_message) {
Ok(s) => s,
Err(e) => {
error!("Could not serialize JSON message: {}", e);
exit(1);
}
};
match stream.write_all(&j.as_bytes()).await {
Ok(_) => {
info!(
"Sent JSON:\n<bright-white>{}<//>",
serde_json::to_string_pretty(&test_message)?
);
}
Err(e) => error!("Could not write to stream: {}", e),
}
}
}