Add optional path parameter to ls command

This commit is contained in:
William Brawner 2022-07-24 22:41:23 -06:00
parent f481dcfbf7
commit 997d6e1f8a

View file

@ -47,9 +47,9 @@ fn get(url: &str, destination: &str) -> Result<(), Box<dyn Error>> {
}
}
fn ls() -> String {
fn ls(path: &str) -> String {
let mut files: Vec<String> = Vec::new();
let mut paths: Vec<DirEntry> = fs::read_dir(pwd()).unwrap().map(|r| r.unwrap()).collect();
let mut paths: Vec<DirEntry> = fs::read_dir(path).unwrap().map(|r| r.unwrap()).collect();
paths.sort_by(|a, b| a.file_name().cmp(&b.file_name()));
for file in paths {
files.push(file.file_name().into_string().unwrap());
@ -93,8 +93,14 @@ fn handle_connection(mut stream: TcpStream) {
Ok(_) => String::new(),
Err(e) => (*e).to_string(),
}
},
"ls" => ls(),
}
"ls" => {
let path = match command_iter.next() {
Some(s) => s.to_string(),
None => pwd(),
};
ls(path.as_ref())
}
"ping" => String::from("pong"),
"pwd" => pwd(),
"shell" => shell(command_iter.next().unwrap(), command_iter),