Skip to main content

Module command

Module command 

Source
Expand description

The Command trait, its descriptor, and the link-time registration slice.

A Command is an opinionated async fn(App) -> Result<()> bundled with a small static descriptor (CommandSpec). Commands self- register via a linkme distributed slice so no manual wiring is needed when authoring new commands.

§Registration pattern

use rtb_app::command::{BUILTIN_COMMANDS, Command, CommandSpec};
use rtb_app::linkme::distributed_slice;

pub struct Deploy;

#[async_trait::async_trait]
impl Command for Deploy {
    fn spec(&self) -> &CommandSpec {
        static SPEC: CommandSpec = CommandSpec {
            name: "deploy",
            about: "Deploy the thing",
            ..CommandSpec::DEFAULT
        };
        &SPEC
    }

    async fn run(&self, _app: rtb_app::app::App) -> miette::Result<()> {
        Ok(())
    }
}

#[distributed_slice(BUILTIN_COMMANDS)]
fn __register_deploy() -> Box<dyn Command> { Box::new(Deploy) }

rtb-cli::Application::run iterates BUILTIN_COMMANDS at startup, filters by the runtime Features set, and registers each remaining command with clap.

Structs§

CommandSpec
Static descriptor of a Command.

Statics§

BUILTIN_COMMANDS
Link-time registry of Command factory functions.
BUILTIN_PRERUN_HOOKS
Link-time registry of PreRunHooks run before command dispatch.

Traits§

Command
The contract every CLI subcommand implements.

Type Aliases§

PreRunFuture
A boxed, Send future returned by a PreRunHook.
PreRunHook
A pre-run hook, invoked with the App before command dispatch.