1use crate::globals::kill_task_by_id;
2use nu_engine::CallExt;
3use nu_protocol::{
4 Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
5 engine::{Call, Command, EngineState, Stack},
6};
7
8#[derive(Clone)]
9pub struct JobKill;
10
11impl Command for JobKill {
12 fn name(&self) -> &str {
13 "job kill"
14 }
15
16 fn signature(&self) -> Signature {
17 Signature::build("job kill")
18 .required("id", SyntaxShape::Int, "id of job to kill")
19 .input_output_type(Type::Nothing, Type::Nothing)
20 .category(Category::System)
21 }
22
23 fn description(&self) -> &str {
24 "Kill a background job by ID."
25 }
26
27 fn run(
28 &self,
29 engine_state: &EngineState,
30 stack: &mut Stack,
31 call: &Call,
32 _input: PipelineData,
33 ) -> Result<PipelineData, ShellError> {
34 let id: i64 = call.req(engine_state, stack, 0)?;
35 if id < 0 {
36 return Err(ShellError::GenericError {
37 error: "invalid id".to_string(),
38 msg: format!("task id must be non-negative, got {}", id),
39 span: Some(call.head),
40 help: None,
41 inner: vec![],
42 });
43 }
44
45 let killed = kill_task_by_id(id as usize);
46
47 // Return a boolean indicating whether a task was found & killed.
48 Ok(Value::bool(killed, call.head).into_pipeline_data())
49 }
50}