diff --git a/src/bin/ls.rs b/src/bin/ls.rs index 2bde7f3..8aa8434 100644 --- a/src/bin/ls.rs +++ b/src/bin/ls.rs @@ -5,14 +5,15 @@ extern crate serde_json; extern crate serde_derive; use a03::*; -use std::net::{TcpListener, TcpStream, Shutdown}; +use std::net::{TcpListener, TcpStream, Shutdown, SocketAddrV4, Ipv4Addr}; use std::borrow::Cow; use std::thread; use std::io::Read; use std::io::Write; fn main() { - let mut stream = TcpStream::connect("localhost:6770").unwrap(); + let endpoint = parse_endpoint_from_cli(0); + let mut stream = TcpStream::connect(endpoint).unwrap(); serde_json::to_writer( &mut stream, &Packet { @@ -20,12 +21,11 @@ fn main() { json: None, }) .unwrap(); - println!("Message sent!"); stream.flush().unwrap(); stream.shutdown(Shutdown::Write).unwrap(); let files: FilePaths = serde_json::from_reader(&mut stream).unwrap(); for path in files.paths.iter() { - println!("Path: {}", path); + println!("/home/{}", path); } } diff --git a/src/lib.rs b/src/lib.rs index df82560..bd6a7ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,11 @@ extern crate serde_derive; use std::borrow::Cow; use std::net::Ipv4Addr; +use std::net::SocketAddrV4; +use std::str::FromStr; +//use std:: + +const DEFAULT_PORT: &str = "8000"; #[derive(Serialize, Deserialize, Debug)] pub enum PacketType { @@ -81,3 +86,15 @@ pub struct BlockQuery { pub data_node: DataNode, pub chunk_id: String } + +pub fn parse_endpoint_from_cli(arg_index : usize) -> String { + let mut args: Vec = std::env::args().skip(1).collect(); + let endpoint_arg: String = args.get(arg_index).expect("No IP provided").clone(); + + if endpoint_arg.contains(":") { + endpoint_arg + } else { + format!("{}:{}", endpoint_arg, DEFAULT_PORT) + } +} +