run_cli_command: add additional rpcenv parameter

This commit is contained in:
Dietmar Maurer 2020-05-06 12:22:56 +02:00
parent 3133316a8a
commit d7c5af499b

View File

@ -55,11 +55,10 @@ async fn handle_simple_command_future(
prefix: &str, prefix: &str,
cli_cmd: &CliCommand, cli_cmd: &CliCommand,
args: Vec<String>, args: Vec<String>,
mut rpcenv: CliEnvironment,
) -> Result<(), Error> { ) -> Result<(), Error> {
let params = parse_arguments(prefix, cli_cmd, args)?; let params = parse_arguments(prefix, cli_cmd, args)?;
let mut rpcenv = CliEnvironment::new();
match cli_cmd.info.handler { match cli_cmd.info.handler {
ApiHandler::Sync(handler) => match (handler)(params, &cli_cmd.info, &mut rpcenv) { ApiHandler::Sync(handler) => match (handler)(params, &cli_cmd.info, &mut rpcenv) {
Ok(value) => { Ok(value) => {
@ -101,12 +100,11 @@ fn handle_simple_command(
prefix: &str, prefix: &str,
cli_cmd: &CliCommand, cli_cmd: &CliCommand,
args: Vec<String>, args: Vec<String>,
mut rpcenv: CliEnvironment,
run: Option<fn(ApiFuture) -> Result<Value, Error>>, run: Option<fn(ApiFuture) -> Result<Value, Error>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let params = parse_arguments(prefix, cli_cmd, args)?; let params = parse_arguments(prefix, cli_cmd, args)?;
let mut rpcenv = CliEnvironment::new();
match cli_cmd.info.handler { match cli_cmd.info.handler {
ApiHandler::Sync(handler) => match (handler)(params, &cli_cmd.info, &mut rpcenv) { ApiHandler::Sync(handler) => match (handler)(params, &cli_cmd.info, &mut rpcenv) {
Ok(value) => { Ok(value) => {
@ -270,16 +268,17 @@ pub async fn handle_command_future(
def: Arc<CommandLineInterface>, def: Arc<CommandLineInterface>,
prefix: &str, prefix: &str,
mut args: Vec<String>, mut args: Vec<String>,
rpcenv: CliEnvironment,
) -> Result<(), Error> { ) -> Result<(), Error> {
set_help_context(Some(def.clone())); set_help_context(Some(def.clone()));
let result = match &*def { let result = match &*def {
CommandLineInterface::Simple(ref cli_cmd) => { CommandLineInterface::Simple(ref cli_cmd) => {
handle_simple_command_future(&prefix, &cli_cmd, args).await handle_simple_command_future(&prefix, &cli_cmd, args, rpcenv).await
} }
CommandLineInterface::Nested(ref map) => { CommandLineInterface::Nested(ref map) => {
let cli_cmd = parse_nested_command(&prefix, &map, &mut args)?; let cli_cmd = parse_nested_command(&prefix, &map, &mut args)?;
handle_simple_command_future(&prefix, &cli_cmd, args).await handle_simple_command_future(&prefix, &cli_cmd, args, rpcenv).await
} }
}; };
@ -296,17 +295,18 @@ pub fn handle_command(
def: Arc<CommandLineInterface>, def: Arc<CommandLineInterface>,
prefix: &str, prefix: &str,
mut args: Vec<String>, mut args: Vec<String>,
rpcenv: CliEnvironment,
run: Option<fn(ApiFuture) -> Result<Value, Error>>, run: Option<fn(ApiFuture) -> Result<Value, Error>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
set_help_context(Some(def.clone())); set_help_context(Some(def.clone()));
let result = match &*def { let result = match &*def {
CommandLineInterface::Simple(ref cli_cmd) => { CommandLineInterface::Simple(ref cli_cmd) => {
handle_simple_command(&prefix, &cli_cmd, args, run) handle_simple_command(&prefix, &cli_cmd, args, rpcenv, run)
} }
CommandLineInterface::Nested(ref map) => { CommandLineInterface::Nested(ref map) => {
let cli_cmd = parse_nested_command(&prefix, &map, &mut args)?; let cli_cmd = parse_nested_command(&prefix, &map, &mut args)?;
handle_simple_command(&prefix, &cli_cmd, args, run) handle_simple_command(&prefix, &cli_cmd, args, rpcenv, run)
} }
}; };
@ -358,7 +358,7 @@ fn prepare_cli_command(def: &CommandLineInterface) -> (String, Vec<String>) {
/// - ``bashcomplete``: Output bash completions instead of running the command. /// - ``bashcomplete``: Output bash completions instead of running the command.
/// - ``printdoc``: Output ReST documentation. /// - ``printdoc``: Output ReST documentation.
/// ///
pub async fn run_async_cli_command<C: Into<CommandLineInterface>>(def: C) { pub async fn run_async_cli_command<C: Into<CommandLineInterface>>(def: C, rpcenv: CliEnvironment) {
let def = match def.into() { let def = match def.into() {
CommandLineInterface::Simple(cli_cmd) => CommandLineInterface::Simple(cli_cmd), CommandLineInterface::Simple(cli_cmd) => CommandLineInterface::Simple(cli_cmd),
CommandLineInterface::Nested(map) => CommandLineInterface::Nested(map.insert_help()), CommandLineInterface::Nested(map) => CommandLineInterface::Nested(map.insert_help()),
@ -366,7 +366,7 @@ pub async fn run_async_cli_command<C: Into<CommandLineInterface>>(def: C) {
let (prefix, args) = prepare_cli_command(&def); let (prefix, args) = prepare_cli_command(&def);
if handle_command_future(Arc::new(def), &prefix, args) if handle_command_future(Arc::new(def), &prefix, args, rpcenv)
.await .await
.is_err() .is_err()
{ {
@ -381,6 +381,7 @@ pub async fn run_async_cli_command<C: Into<CommandLineInterface>>(def: C) {
/// async commands simply fail). /// async commands simply fail).
pub fn run_cli_command<C: Into<CommandLineInterface>>( pub fn run_cli_command<C: Into<CommandLineInterface>>(
def: C, def: C,
rpcenv: CliEnvironment,
run: Option<fn(ApiFuture) -> Result<Value, Error>>, run: Option<fn(ApiFuture) -> Result<Value, Error>>,
) { ) {
let def = match def.into() { let def = match def.into() {
@ -390,7 +391,7 @@ pub fn run_cli_command<C: Into<CommandLineInterface>>(
let (prefix, args) = prepare_cli_command(&def); let (prefix, args) = prepare_cli_command(&def);
if handle_command(Arc::new(def), &prefix, args, run).is_err() { if handle_command(Arc::new(def), &prefix, args, rpcenv, run).is_err() {
std::process::exit(-1); std::process::exit(-1);
} }
} }