diff --git a/src/commands/debug/mod.rs b/src/commands/debug/mod.rs index e5651921..0a88cbb5 100644 --- a/src/commands/debug/mod.rs +++ b/src/commands/debug/mod.rs @@ -16,6 +16,9 @@ use bch_bindgen::c::bpos; pub struct Cli { #[arg(required(true))] devices: Vec, + + #[arg(short, long)] + command: Option, } #[derive(Debug)] @@ -69,6 +72,31 @@ fn dump(fs: &Fs, cmd: DumpCommand) { } } +fn usage() { + println!("Usage:"); + println!(" dump "); + println!(" update .="); +} + +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) -> i32 { fn prompt() { print!("bcachefs> "); @@ -76,8 +104,34 @@ pub fn debug(argv: Vec) -> i32 { } let opt = Cli::parse_from(argv); - 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) { Ok(fs) => fs, Err(_) => { @@ -85,22 +139,10 @@ pub fn debug(argv: Vec) -> i32 { } }; - let type_list = bkey_types::get_bkey_type_info(); - prompt(); let stdin = std::io::stdin(); for line in stdin.lock().lines() { - let line = line.unwrap(); - if let Some(cmd) = parser::parse_command(&line) { - match cmd { - // usage: dump - DebugCommand::Dump(cmd) => dump(&fs, cmd), - // usage: update .= - DebugCommand::Update(cmd) => update(&fs, &type_list, cmd), - } - } else { - println!("failed to parse a command"); - }; + do_command(&fs, &type_list, &line.unwrap()); prompt(); } diff --git a/src/commands/debug/parser.rs b/src/commands/debug/parser.rs index 550860c9..79fadc61 100644 --- a/src/commands/debug/parser.rs +++ b/src/commands/debug/parser.rs @@ -81,9 +81,9 @@ fn parse_command_inner(input: &str) -> IResult<&str, DebugCommand> { } } -pub fn parse_command(input: &str) -> Option { +pub fn parse_command(input: &str) -> anyhow::Result { match parse_command_inner(input) { - Ok((_, c)) => Some(c), - Err(_) => None, + Ok((_, c)) => Ok(c), + Err(e) => Err(anyhow::anyhow!("{e}")), } }