diff options
author | Thomas Bertschinger <tahbertschinger@gmail.com> | 2024-05-01 20:58:59 -0600 |
---|---|---|
committer | Thomas Bertschinger <tahbertschinger@gmail.com> | 2024-05-07 21:29:32 -0400 |
commit | 2f3f9ff7d1f871668cf9b0fad18e42266789ef28 (patch) | |
tree | 99432ba3630e4350007c0782d72f8ebb4a4df682 | |
parent | e74310fb24e91e5f7522ad60bbf7726dbe0ccb03 (diff) |
WIP: add option to put command on command line
instead of using REPL
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
-rw-r--r-- | src/commands/debug/mod.rs | 70 | ||||
-rw-r--r-- | src/commands/debug/parser.rs | 6 |
2 files changed, 59 insertions, 17 deletions
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<std::path::PathBuf>, + + #[arg(short, long)] + command: Option<String>, } #[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 { fn prompt() { print!("bcachefs> "); @@ -76,8 +104,34 @@ pub fn debug(argv: Vec<String>) -> 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<String>) -> 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 <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"); - }; + 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<DebugCommand> { +pub fn parse_command(input: &str) -> anyhow::Result<DebugCommand> { match parse_command_inner(input) { - Ok((_, c)) => Some(c), - Err(_) => None, + Ok((_, c)) => Ok(c), + Err(e) => Err(anyhow::anyhow!("{e}")), } } |