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                "no-watch",
22                "Disable filesystem watching after the initial scan (watching is enabled by default).",
23                None,
24            )
25            .switch(
26                "no-content-indexing",
27                "Disable file content indexing (content indexing is enabled by default).",
28                None,
29            )
30            .switch(
31                "follow-symlinks",
32                "Whether to follow symlinks when indexing.",
33                Some('f'),
34            )
35            .input_output_types(vec![(Type::Nothing, Type::record())])
36            .category(Category::FileSystem)
37    }
38
39    fn description(&self) -> &str {
40        "Initialize the in-memory idx index for a path."
41    }
42
43    fn extra_description(&self) -> &str {
44        "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. \
45Filesystem watching is enabled by default so the index stays warm and `idx watch` can stream events; pass `--no-watch` to disable both."
46    }
47
48    fn examples(&self) -> Vec<Example<'_>> {
49        vec![
50            Example {
51                description: "Initialize idx for the current directory.",
52                example: "idx init .",
53                result: None,
54            },
55            Example {
56                description: "Initialize idx and wait for the initial scan to complete.",
57                example: "idx init . --wait",
58                result: None,
59            },
60            Example {
61                description: "Initialize idx without filesystem watching.",
62                example: "idx init . --no-watch",
63                result: None,
64            },
65        ]
66    }
67
68    fn run(
69        &self,
70        engine_state: &EngineState,
71        stack: &mut Stack,
72        call: &Call,
73        _input: PipelineData,
74    ) -> Result<PipelineData, ShellError> {
75        let path: Spanned<String> = call.req(engine_state, stack, 0)?;
76        let wait = call.has_flag(engine_state, stack, "wait")?;
77        let no_watch = call.has_flag(engine_state, stack, "no-watch")?;
78        let no_indexing = call.has_flag(engine_state, stack, "no-content-indexing")?;
79        let follow = call.has_flag(engine_state, stack, "follow-symlinks")?;
80        let cwd = engine_state.cwd(Some(stack))?;
81        let abs = nu_path::expand_path_with(path.item, cwd, true);
82        let watch = !no_watch;
83        let content_indexing = !no_indexing;
84        let status = init_runtime(&abs, watch, wait, follow, content_indexing, call.head)?;
85        Ok(PipelineData::value(status.to_value(call.head), None))
86    }
87}