Implement REPL

This commit is contained in:
Josh Hansen 2023-08-11 14:20:59 -07:00
parent 62b72e615e
commit f0309baac7

View file

@ -1,5 +1,6 @@
use std::{
collections::HashMap,
ffi::OsString,
fs::{create_dir_all, hard_link},
io::Write,
path::{Path, PathBuf},
@ -447,7 +448,21 @@ fn repl() -> rustyline::Result<()> {
match readline {
Ok(line) => {
rl.add_history_entry(line.as_str())?;
println!("Line: {}", line);
match Cli::try_parse_from(
std::iter::once(OsString::from(PKG_NAME.as_str()))
.chain(line.split_terminator(" ").map(OsString::from)),
) {
Ok(args) => match args.command.as_ref() {
Some(cmd) => {
run_command(cmd);
}
None => {}
},
Err(e) => {
eprintln!("{}", e);
}
}
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
@ -469,59 +484,63 @@ fn repl() -> rustyline::Result<()> {
Ok(())
}
fn run_command(cmd: &Commands) {
match cmd {
Commands::Cp {
src,
dest,
fields,
verbose,
} => {
cp_or_mv(src, dest, fields, verbose, CopyOrMove::Copy);
}
Commands::Mv {
src,
dest,
fields,
verbose,
} => {
cp_or_mv(src, dest, fields, verbose, CopyOrMove::Move);
}
Commands::Rm {
paths,
fields,
verbose,
} => {
rm(paths, fields, verbose);
}
Commands::Get {
paths,
fields,
json,
where_,
flat,
} => {
get(paths, fields, json, where_, &!*flat);
}
Commands::Set {
paths,
field_assignments,
verbose,
} => {
set(paths, field_assignments, verbose);
}
Commands::Idx {
src,
dest,
keys,
verbose,
} => {
idx(src, dest, keys, verbose);
}
}
}
fn main() {
let cli = Cli::parse();
match cli.command.as_ref() {
None => repl().unwrap_or_else(|e| panic!("Could not initialize REPL: {}", e)),
Some(command) => match command {
Commands::Cp {
src,
dest,
fields,
verbose,
} => {
cp_or_mv(src, dest, fields, verbose, CopyOrMove::Copy);
}
Commands::Mv {
src,
dest,
fields,
verbose,
} => {
cp_or_mv(src, dest, fields, verbose, CopyOrMove::Move);
}
Commands::Rm {
paths,
fields,
verbose,
} => {
rm(paths, fields, verbose);
}
Commands::Get {
paths,
fields,
json,
where_,
flat,
} => {
get(paths, fields, json, where_, &!*flat);
}
Commands::Set {
paths,
field_assignments,
verbose,
} => {
set(paths, field_assignments, verbose);
}
Commands::Idx {
src,
dest,
keys,
verbose,
} => {
idx(src, dest, keys, verbose);
}
},
Some(command) => run_command(command),
}
}