feat: add ability to pass path argument

This commit is contained in:
2021-07-09 14:30:23 -04:00
parent 002fff3eb4
commit 8600e9f994
2 changed files with 27 additions and 5 deletions

View File

@@ -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"

View File

@@ -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<String> = 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())
}
}