Skip to main content

nu_command/filesystem/idx/
import.rs

1use super::state::restore_snapshot;
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct IdxImport;
6
7impl Command for IdxImport {
8    fn name(&self) -> &str {
9        "idx import"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build(self.name())
14            .required(
15                "filepath",
16                SyntaxShape::Filepath,
17                "Path to a stored idx snapshot.",
18            )
19            .switch(
20                "no-watch",
21                "Disable filesystem watching after import (watching is enabled by default).",
22                None,
23            )
24            .input_output_types(vec![(Type::Nothing, Type::record())])
25            .category(Category::FileSystem)
26    }
27
28    fn description(&self) -> &str {
29        "Import idx state from disk."
30    }
31
32    fn extra_description(&self) -> &str {
33        "Reads a SQLite snapshot created by `idx export` and auto-initializes idx runtime for immediate queries."
34    }
35
36    fn examples(&self) -> Vec<Example<'_>> {
37        vec![Example {
38            description: "Restore an idx index from a snapshot on disk.",
39            example: "idx import ~/my-index.db",
40            result: None,
41        }]
42    }
43
44    fn run(
45        &self,
46        engine_state: &EngineState,
47        stack: &mut Stack,
48        call: &Call,
49        _input: PipelineData,
50    ) -> Result<PipelineData, ShellError> {
51        let path: Spanned<String> = call.req(engine_state, stack, 0)?;
52        let cwd = engine_state.cwd(Some(stack))?;
53        let abs = nu_path::expand_path_with(path.item, cwd, true);
54        let no_watch = call.has_flag(engine_state, stack, "no-watch")?;
55
56        Ok(PipelineData::value(
57            restore_snapshot(abs.as_ref(), no_watch, call.head)?,
58            None,
59        ))
60    }
61}