Skip to main content

nu_command/filesystem/idx/
init.rs

1use super::state::init_runtime;
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct IdxInit;
6
7impl Command for IdxInit {
8    fn name(&self) -> &str {
9        "idx init"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build(self.name())
14            .required("path", SyntaxShape::Directory, "Path to index.")
15            .switch(
16                "wait",
17                "Block until the initial scan completes before returning.",
18                Some('w'),
19            )
20            .switch(
21                "follow-symlinks",
22                "Whether to follow symlinks when indexing.",
23                Some('f'),
24            )
25            .input_output_types(vec![(Type::Nothing, Type::record())])
26            .category(Category::FileSystem)
27    }
28
29    fn description(&self) -> &str {
30        "Initialize the in-memory idx index for a path."
31    }
32
33    fn extra_description(&self) -> &str {
34        "By default idx init returns immediately and indexing continues in the background. Use `idx status` to check when scanning completes. Pass `--wait` to block until the initial scan finishes."
35    }
36
37    fn examples(&self) -> Vec<Example<'_>> {
38        vec![
39            Example {
40                description: "Initialize idx for the current directory",
41                example: "idx init .",
42                result: None,
43            },
44            Example {
45                description: "Initialize idx and wait for the initial scan to complete",
46                example: "idx init . --wait",
47                result: None,
48            },
49        ]
50    }
51
52    fn run(
53        &self,
54        engine_state: &EngineState,
55        stack: &mut Stack,
56        call: &Call,
57        _input: PipelineData,
58    ) -> Result<PipelineData, ShellError> {
59        let path: Spanned<String> = call.req(engine_state, stack, 0)?;
60        let wait = call.has_flag(engine_state, stack, "wait")?;
61        let follow = call.has_flag(engine_state, stack, "follow-symlinks")?;
62        let cwd = engine_state.cwd(Some(stack))?;
63        let abs = nu_path::expand_path_with(path.item, cwd, true);
64        // There is a functionality in fff-search to update the index via watch but it was non-trivial to get working
65        // So, for now, let's always default to watch = false.
66        let watch = false;
67        let status = init_runtime(&abs, watch, wait, follow, call.head)?;
68        Ok(PipelineData::value(status.to_value(call.head), None))
69    }
70}