WIP: add option to put command on command line

instead of using REPL

Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
This commit is contained in:
Thomas Bertschinger 2024-05-01 20:58:59 -06:00
parent e74310fb24
commit 2f3f9ff7d1
2 changed files with 59 additions and 17 deletions

View File

@ -16,6 +16,9 @@ use bch_bindgen::c::bpos;
pub struct Cli { pub struct Cli {
#[arg(required(true))] #[arg(required(true))]
devices: Vec<std::path::PathBuf>, devices: Vec<std::path::PathBuf>,
#[arg(short, long)]
command: Option<String>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -69,6 +72,31 @@ fn dump(fs: &Fs, cmd: DumpCommand) {
} }
} }
fn usage() {
println!("Usage:");
println!(" dump <btree_type> <bpos>");
println!(" update <btree_type> <bpos> <bkey_type>.<field>=<value>");
}
fn do_command(fs: &Fs, type_list: &bkey_types::BkeyTypes, cmd: &str) -> i32 {
match parser::parse_command(cmd) {
Ok(cmd) => {
match cmd {
DebugCommand::Dump(cmd) => dump(fs, cmd),
DebugCommand::Update(cmd) => update(fs, type_list, cmd),
};
0
}
Err(e) => {
println!("{e}");
usage();
1
}
}
}
pub fn debug(argv: Vec<String>) -> i32 { pub fn debug(argv: Vec<String>) -> i32 {
fn prompt() { fn prompt() {
print!("bcachefs> "); print!("bcachefs> ");
@ -76,8 +104,34 @@ pub fn debug(argv: Vec<String>) -> i32 {
} }
let opt = Cli::parse_from(argv); let opt = Cli::parse_from(argv);
let fs_opts: bcachefs::bch_opts = Default::default(); let fs_opts: bcachefs::bch_opts = Default::default();
let type_list = bkey_types::get_bkey_type_info();
if let Some(cmd) = opt.command {
return match parser::parse_command(&cmd) {
Ok(cmd) => {
let fs = match Fs::open(&opt.devices, fs_opts) {
Ok(fs) => fs,
Err(_) => {
return 1;
}
};
match cmd {
DebugCommand::Dump(cmd) => dump(&fs, cmd),
DebugCommand::Update(cmd) => update(&fs, &type_list, cmd),
}
0
}
Err(e) => {
println!("{e}");
usage();
1
}
};
}
let fs = match Fs::open(&opt.devices, fs_opts) { let fs = match Fs::open(&opt.devices, fs_opts) {
Ok(fs) => fs, Ok(fs) => fs,
Err(_) => { Err(_) => {
@ -85,22 +139,10 @@ pub fn debug(argv: Vec<String>) -> i32 {
} }
}; };
let type_list = bkey_types::get_bkey_type_info();
prompt(); prompt();
let stdin = std::io::stdin(); let stdin = std::io::stdin();
for line in stdin.lock().lines() { for line in stdin.lock().lines() {
let line = line.unwrap(); do_command(&fs, &type_list, &line.unwrap());
if let Some(cmd) = parser::parse_command(&line) {
match cmd {
// usage: dump <btree_type> <bpos>
DebugCommand::Dump(cmd) => dump(&fs, cmd),
// usage: update <btree_type> <bpos> <bkey_type>.<field>=<value>
DebugCommand::Update(cmd) => update(&fs, &type_list, cmd),
}
} else {
println!("failed to parse a command");
};
prompt(); prompt();
} }

View File

@ -81,9 +81,9 @@ fn parse_command_inner(input: &str) -> IResult<&str, DebugCommand> {
} }
} }
pub fn parse_command(input: &str) -> Option<DebugCommand> { pub fn parse_command(input: &str) -> anyhow::Result<DebugCommand> {
match parse_command_inner(input) { match parse_command_inner(input) {
Ok((_, c)) => Some(c), Ok((_, c)) => Ok(c),
Err(_) => None, Err(e) => Err(anyhow::anyhow!("{e}")),
} }
} }