From 8600e9f9944a110ed9799621a0c850df7fcced0f Mon Sep 17 00:00:00 2001 From: Giovani Date: Fri, 9 Jul 2021 14:30:23 -0400 Subject: [PATCH] feat: add ability to pass path argument --- Cargo.toml | 1 + src/main.rs | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 30a2f5a..b2333d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +colored = "2.0.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3b026ae..fbed3d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,33 @@ use std::fs; +use std::env; +use colored::Colorize; + +// \x1b is used to print in red fn main() { - let paths = fs::read_dir(".").unwrap(); + let args: Vec = env::args().collect(); + if args.len() != 2 { + println!("{}", "ERR: Invalid number of arguments provided".red()); + return + } - for result in paths { - let path = result.unwrap().path(); - let metadata = fs::metadata(path.to_str().unwrap()).unwrap(); + let is_ok = fs::metadata(args[1].to_owned()).is_ok(); + if !is_ok { + println!("{}", "ERR: Path provided does not exist".red()); + return + } + + let is_dir = fs::metadata(args[1].to_owned()).unwrap().is_dir(); + if !is_dir { + println!("{}", "ERR: Path provided is not a directory".red()); + return + } + + let objects = fs::read_dir(args[1].to_owned()).unwrap(); + for result in objects { + let object = result.unwrap().path(); + let metadata = fs::metadata(object.to_str().unwrap()).unwrap(); let type_ = if metadata.is_dir() { "Dirc" } else { "File" }; - println!("{}: {}", type_, path.display()) + println!("{}: {}", type_, object.display()) } } \ No newline at end of file