pub struct Context { /* private fields */ }Expand description
Context for Clawless commands
This struct provides information about the environment that Clawless commands are executed in, access to shared resources, and configuration settings. It is passed to each command by the Clawless runtime when executing commands.
§Examples
#[derive(Debug, Args)]
pub struct GreetArgs {
name: String,
}
#[command]
pub async fn greet(args: GreetArgs, context: Context) -> CommandResult {
message!("Hello, {}!", args.name);
Ok(())
}Implementations§
Source§impl Context
impl Context
Sourcepub fn interactivity(&self) -> Interactivity
pub fn interactivity(&self) -> Interactivity
Whether a user is present who can answer the application
Source§impl Context
impl Context
Sourcepub fn current_working_directory(&self) -> &CurrentWorkingDirectory
pub fn current_working_directory(&self) -> &CurrentWorkingDirectory
The working directory in which a command was called
Sourcepub fn cancellation(&self) -> &Cancellation
pub fn cancellation(&self) -> &Cancellation
The cancellation token for cooperative shutdown
Source§impl Context
impl Context
Sourcepub fn builder() -> ContextBuilder
pub fn builder() -> ContextBuilder
Creates a new Context instance
When current_working_directory is omitted, it is auto-detected from the environment.
When provided explicitly (e.g., in tests), the given value is used directly.
When interactivity is omitted, the context reports Interactivity::NonInteractive.
§Errors
Returns ContextError::CurrentWorkingDirectory if current_working_directory is not
provided and the current working directory cannot be determined from the environment.
§Examples
// Production: CWD auto-detected
let context = Context::builder()
.output(output)
.build()?;
// Tests: explicit CWD
let context = Context::builder()
.current_working_directory(tmp.path())
.output(output)
.build()?;Source§impl Context
impl Context
Sourcepub fn process(&self) -> Process
pub fn process(&self) -> Process
Returns the interface that runs external programs
The returned Process reports every run through the output of this context and stops a
run when the cancellation token of this context is cancelled. A command therefore gets live
output and cooperative shutdown without wiring either of them itself.
Each call builds a handle over the same channel and the same token, so a command can call this method wherever it runs a program instead of holding the handle.
The handle takes the default grace period, which is the time a program gets to end itself
before Clawless kills it. A command that drives a program needing longer builds its own
handle with Process::builder.
§Examples
#[command]
pub async fn build(args: BuildArgs, context: Context) -> CommandResult {
context
.process()
.run(Invocation::new("cargo").arg("build"))
.await?
.require_success()?;
Ok(())
}Sourcepub fn prompt(&self) -> Prompt
pub fn prompt(&self) -> Prompt
Returns the interface that asks the user a question
The returned Prompt uses the output, the cancellation token, and the
Interactivity of this context. Each call builds a handle over the same channel and the
same token, so a command does not need to keep the handle.
§Examples
#[command]
pub async fn publish(args: PublishArgs, context: Context) -> CommandResult {
match context.prompt().confirm("Publish the release?").await? {
Confirmation::Yes => message!("Publishing"),
Confirmation::No => message!("Nothing published"),
}
Ok(())
}