nu_command/experimental/
job_tag.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::JobId;
3
4#[derive(Clone)]
5pub struct JobTag;
6
7impl Command for JobTag {
8    fn name(&self) -> &str {
9        "job tag"
10    }
11
12    fn description(&self) -> &str {
13        "Add a description tag to a background job."
14    }
15
16    fn signature(&self) -> nu_protocol::Signature {
17        Signature::build("job tag")
18            .category(Category::Experimental)
19            .required("id", SyntaxShape::Int, "The id of the job to tag.")
20            .required(
21                "tag",
22                SyntaxShape::OneOf(vec![SyntaxShape::String, SyntaxShape::Nothing]),
23                "The tag to assign to the job.",
24            )
25            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
26    }
27
28    fn search_terms(&self) -> Vec<&str> {
29        vec!["describe", "desc"]
30    }
31
32    fn run(
33        &self,
34        engine_state: &EngineState,
35        stack: &mut Stack,
36        call: &Call,
37        _input: PipelineData,
38    ) -> Result<PipelineData, ShellError> {
39        let head = call.head;
40
41        let id_arg: Spanned<usize> = call.req(engine_state, stack, 0)?;
42        let id = JobId::new(id_arg.item);
43
44        let tag: Option<String> = call.req(engine_state, stack, 1)?;
45
46        let mut jobs = engine_state.jobs.lock().expect("jobs lock is poisoned!");
47
48        match jobs.lookup_mut(id) {
49            None => return Err(JobError::NotFound { span: head, id }.into()),
50            Some(job) => job.assign_tag(tag),
51        }
52
53        Ok(Value::nothing(head).into_pipeline_data())
54    }
55
56    fn examples(&self) -> Vec<Example<'_>> {
57        vec![
58            Example {
59                example: "let id = job spawn { sleep 10sec }; job tag $id abc ",
60                description: "Tag a newly spawned job",
61                result: None,
62            },
63            Example {
64                example: "let id = job spawn { sleep 10sec }; job tag $id abc; job tag $id null",
65                description: "Remove the tag of a job",
66                result: None,
67            },
68        ]
69    }
70}