nu_command/filesystem/idx/
init.rs1use 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. Filesystem watching is enabled by default; pass `--no-watch` to disable it."
45 }
46
47 fn examples(&self) -> Vec<Example<'_>> {
48 vec![
49 Example {
50 description: "Initialize idx for the current directory.",
51 example: "idx init .",
52 result: None,
53 },
54 Example {
55 description: "Initialize idx and wait for the initial scan to complete.",
56 example: "idx init . --wait",
57 result: None,
58 },
59 Example {
60 description: "Initialize idx without filesystem watching.",
61 example: "idx init . --no-watch",
62 result: None,
63 },
64 ]
65 }
66
67 fn run(
68 &self,
69 engine_state: &EngineState,
70 stack: &mut Stack,
71 call: &Call,
72 _input: PipelineData,
73 ) -> Result<PipelineData, ShellError> {
74 let path: Spanned<String> = call.req(engine_state, stack, 0)?;
75 let wait = call.has_flag(engine_state, stack, "wait")?;
76 let no_watch = call.has_flag(engine_state, stack, "no-watch")?;
77 let no_indexing = call.has_flag(engine_state, stack, "no-content-indexing")?;
78 let follow = call.has_flag(engine_state, stack, "follow-symlinks")?;
79 let cwd = engine_state.cwd(Some(stack))?;
80 let abs = nu_path::expand_path_with(path.item, cwd, true);
81 let watch = !no_watch;
82 let content_indexing = !no_indexing;
83 let status = init_runtime(&abs, watch, wait, follow, content_indexing, call.head)?;
84 Ok(PipelineData::value(status.to_value(call.head), None))
85 }
86}