Skip to main content

CommandContext

Struct CommandContext 

Source
#[non_exhaustive]
pub struct CommandContext { pub credential: CredentialResolver, pub args: ValueMap, pub user_args: ValueMap, pub command_path: String, pub middleware: Middleware, pub raw_matches: Arc<ArgMatches>, }
Expand description

Runtime context passed to advanced command handlers.

Most commands can use RuntimeCommandSpec::new and receive just the credential and effective args. Use this context when a command needs the colon path, user-supplied args, or a snapshot of middleware state.

This struct is constructed by the framework during command dispatch. Consumer code receives it in handler closures and should not construct it directly.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§credential: CredentialResolver

Lazy credential resolver.

§args: ValueMap

Effective arguments, including defaults and framework-injected values.

§user_args: ValueMap

Arguments explicitly supplied by the user.

§command_path: String

Colon-separated command path such as project:list.

§middleware: Middleware

Middleware snapshot for this invocation.

§raw_matches: Arc<ArgMatches>

Raw clap matches for typed argument deserialization via derive.

Implementations§

Source§

impl CommandContext

Source

pub fn config(&self) -> &ConfigFile

Returns the per-application config file as loaded at startup.

Read a consumer-owned section with ConfigFile::section, for example ctx.config().section::<DeployConfig>("deploy")?. Engine-reserved settings are available via ConfigFile::engine.

Snapshot semantics: this is the config loaded once when crate::cli::Cli::new was called. Changes made by config set during the same process invocation (e.g. from a previous Cli::run) are not reflected here; restart the CLI (a new Cli::new) to pick them up. For a one-shot CLI process this is always the current on-disk state.

Source

pub fn environment(&self) -> Result<Environment>

Resolves the active Environment for this invocation.

The active environment name is self.middleware.env, seeded at startup from the persisted active environment or configured default and overridden per invocation by the global --env flag. Resolution merges the compiled-in definition, the environments.toml file layer, and <ENV>_* environment-variable overrides.

§Blocking

When the environments.toml file layer is enabled, this performs synchronous filesystem I/O via Environments::resolve. Call it once per invocation and reuse the result rather than calling it repeatedly inside an async handler on a latency-sensitive path.

§Errors

Returns an error if no environment system was registered via CliConfig::with_environments or if the active name does not resolve to a known environment.

Source

pub fn typed_args<T: FromArgMatches>(&self) -> Result<T>

Deserializes the raw argument matches into a typed args struct.

Use this with #[derive(clap::Args)] structs to get type-safe access to command arguments instead of working with the ValueMap directly.

§Errors

Returns an error if the matches cannot be deserialized into T.

Source

pub async fn credential(&self) -> Result<Credential>

Resolves the credential for this command, triggering the auth flow on first use and memoizing the result.

Convenience wrapper over self.credential.resolve().

§Errors

Returns an error when the command is marked no_auth, or when the auth provider fails to produce a credential.

Source

pub async fn try_credential(&self) -> Result<Option<Credential>>

Resolves the credential when one is available, returning Ok(None) for no-auth commands.

Convenience wrapper over self.credential.try_resolve().

§Errors

Propagates the auth provider error when resolution is attempted and fails.

Source

pub async fn credential_with_scopes( &self, extra: &[String], ) -> Result<Credential>

Resolves a credential that additionally covers extra scopes, on top of the command’s declared scopes.

Use this when the required scopes are only known at runtime (for example a generic API caller that derives scopes from the target endpoint). A scope-aware auth provider re-authenticates when the cached token does not already cover the requested set.

Convenience wrapper over self.credential.resolve_with_scopes().

If the handler also issues HTTP requests through the transport bearer injector, call this before the first request: the injector resolves and caches a scope-unaware token, so stepping up afterwards would not affect requests it already authorized. See CredentialResolver::resolve_with_scopes for the full ordering note.

§Errors

Returns an error when the command is marked no_auth, or when the auth provider fails to produce a credential.

Trait Implementations§

Source§

impl Clone for CommandContext

Source§

fn clone(&self) -> CommandContext

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CommandContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more