refactor: Use slice patterns for CLI command matching (#1412)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>pull/24376/head
parent
39234468f0
commit
44e134792b
|
@ -45,42 +45,44 @@ impl TryInto<ReplCommand> for String {
|
|||
|
||||
debug!(?raw_commands, ?commands, "processing tokens");
|
||||
|
||||
if !commands.is_empty() && commands[0] == "help" {
|
||||
if commands.len() > 1 {
|
||||
// Get something we can more easily pattern match on
|
||||
let commands = commands.iter().map(|s| s.as_str()).collect::<Vec<_>>();
|
||||
|
||||
match commands.as_slice() {
|
||||
["help"] => Ok(ReplCommand::Help),
|
||||
["help", ..] => {
|
||||
let extra_content = commands[1..].join(" ");
|
||||
warn!(%extra_content, "ignoring tokens after 'help'");
|
||||
}
|
||||
Ok(ReplCommand::Help)
|
||||
} else if commands.len() == 1 && commands[0] == "observer" {
|
||||
Ok(ReplCommand::Observer)
|
||||
} else if commands.len() == 1 && commands[0] == "exit" {
|
||||
Ok(ReplCommand::Exit)
|
||||
} else if commands.len() == 1 && commands[0] == "quit" {
|
||||
Ok(ReplCommand::Exit)
|
||||
} else if commands.len() == 2 && commands[0] == "use" && commands[1] == "database" {
|
||||
// USE DATABASE
|
||||
}
|
||||
["observer"] => Ok(ReplCommand::Observer),
|
||||
["exit"] => Ok(ReplCommand::Exit),
|
||||
["quit"] => Ok(ReplCommand::Exit),
|
||||
["use", "database"] => {
|
||||
Err("name not specified. Usage: USE DATABASE <name>".to_string())
|
||||
} else if commands.len() == 3 && commands[0] == "use" && commands[1] == "database" {
|
||||
} // USE DATABASE
|
||||
["use", "database", _name] => {
|
||||
// USE DATABASE <name>
|
||||
Ok(ReplCommand::UseDatabase {
|
||||
db_name: raw_commands[2].to_string(),
|
||||
})
|
||||
} else if commands.len() == 2 && commands[0] == "use" {
|
||||
}
|
||||
["use", _command] => {
|
||||
// USE <name>
|
||||
Ok(ReplCommand::UseDatabase {
|
||||
db_name: raw_commands[1].to_string(),
|
||||
})
|
||||
} else if commands.len() == 2 && commands[0] == "show" && commands[1] == "databases" {
|
||||
Ok(ReplCommand::ShowDatabases)
|
||||
} else if commands.len() == 3 && commands[0] == "set" && commands[1] == "format" {
|
||||
Ok(ReplCommand::SetFormat {
|
||||
}
|
||||
["show", "databases"] => Ok(ReplCommand::ShowDatabases),
|
||||
["set", "format", _format] => Ok(ReplCommand::SetFormat {
|
||||
format: raw_commands[2].to_string(),
|
||||
})
|
||||
} else {
|
||||
// Default is to treat the entire string like SQL
|
||||
}),
|
||||
_ => {
|
||||
// By default, treat the entire string as SQL
|
||||
Ok(ReplCommand::SqlCommand { sql: self })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ReplCommand {
|
||||
|
|
Loading…
Reference in New Issue