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            .input_output_types(vec![(Type::Nothing, Type::record())])
20            .category(Category::FileSystem)
21    }
22
23    fn description(&self) -> &str {
24        "Import idx state from disk."
25    }
26
27    fn extra_description(&self) -> &str {
28        "Reads a SQLite snapshot created by `idx export` and hydrates the runtime from stored rows. Watch mode is not restored from the snapshot."
29    }
30
31    fn examples(&self) -> Vec<Example<'_>> {
32        vec![Example {
33            description: "Restore an idx index from a snapshot on disk",
34            example: "idx import ~/my-index.db",
35            result: None,
36        }]
37    }
38
39    fn run(
40        &self,
41        engine_state: &EngineState,
42        stack: &mut Stack,
43        call: &Call,
44        _input: PipelineData,
45    ) -> Result<PipelineData, ShellError> {
46        let path: Spanned<String> = call.req(engine_state, stack, 0)?;
47        let cwd = engine_state.cwd(Some(stack))?;
48        let abs = nu_path::expand_path_with(path.item, cwd, true);
49        Ok(PipelineData::value(
50            restore_snapshot(abs.as_ref(), call.head)?,
51            None,
52        ))
53    }
54}