step eight: list files
This commit is contained in:
parent
9e0d9fed73
commit
852157e955
17
src/base.rs
17
src/base.rs
|
@ -0,0 +1,17 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
pub fn write_tree(directory: String) {
|
||||||
|
let entries = fs::read_dir(&directory).unwrap();
|
||||||
|
|
||||||
|
for entry in entries {
|
||||||
|
let item = entry.unwrap();
|
||||||
|
let metadata = item.metadata().unwrap();
|
||||||
|
let name = item.file_name();
|
||||||
|
let full = format!("{}/{}", directory, name.to_str().unwrap());
|
||||||
|
if metadata.is_file() {
|
||||||
|
println!("{}", full);
|
||||||
|
} else if metadata.is_dir() {
|
||||||
|
write_tree(full);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,12 +19,17 @@ fn main() {
|
||||||
.about("outputs the original object from the provided hash")
|
.about("outputs the original object from the provided hash")
|
||||||
.arg(Arg::with_name("hash").index(1).required(true)),
|
.arg(Arg::with_name("hash").index(1).required(true)),
|
||||||
)
|
)
|
||||||
|
.subcommand(
|
||||||
|
SubCommand::with_name("write-tree")
|
||||||
|
.about("write the current working directory to the database"),
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
match matches.subcommand_name() {
|
match matches.subcommand_name() {
|
||||||
Some("init") => init(),
|
Some("init") => init(),
|
||||||
Some("hash-object") => hash_object(matches),
|
Some("hash-object") => hash_object(matches),
|
||||||
Some("cat-file") => cat_file(matches),
|
Some("cat-file") => cat_file(matches),
|
||||||
|
Some("write-tree") => write_tree(),
|
||||||
_ => println!("unknown sub command"),
|
_ => println!("unknown sub command"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,3 +59,7 @@ fn cat_file(matches: ArgMatches) {
|
||||||
println!("{}", file_contents)
|
println!("{}", file_contents)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_tree() {
|
||||||
|
base::write_tree(".".to_owned())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue