Skip to main content

nu_command/filesystem/
mktemp.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::shell_error::generic::GenericError;
3use std::path::PathBuf;
4use uucore::{localized_help_template, translate};
5
6#[derive(Clone)]
7pub struct Mktemp;
8
9impl Command for Mktemp {
10    fn name(&self) -> &str {
11        "mktemp"
12    }
13
14    fn description(&self) -> &str {
15        "Create temporary files or directories using uutils/coreutils mktemp."
16    }
17
18    fn search_terms(&self) -> Vec<&str> {
19        vec![
20            "create",
21            "directory",
22            "file",
23            "folder",
24            "temporary",
25            "coreutils",
26        ]
27    }
28
29    fn signature(&self) -> Signature {
30        Signature::build("mktemp")
31            .input_output_types(vec![(Type::Nothing, Type::String)])
32            .optional(
33                "template",
34                SyntaxShape::String,
35                "Optional pattern from which the name of the file or directory is derived. Must contain at least three 'X's in last component.",
36            )
37            .named("suffix", SyntaxShape::String, "Append suffix to template; must not contain a slash.", None)
38            .named("tmpdir-path", SyntaxShape::Filepath, "Interpret TEMPLATE relative to tmpdir-path. If tmpdir-path is not set use $TMPDIR.", Some('p'))
39            .switch("tmpdir", "Interpret TEMPLATE relative to the system temporary directory. It is implied if template is not provided.", Some('t'))
40            .switch("directory", "Create a directory instead of a file.", Some('d'))
41            .switch("dry", "Don't create a file and just return the path that would have been created.", None)
42            .category(Category::FileSystem)
43    }
44
45    fn examples(&self) -> Vec<Example<'_>> {
46        vec![
47            Example {
48                description: "Make a temporary file with the given suffix in the system temp directory.",
49                example: "mktemp --suffix .txt",
50                result: Some(Value::test_string("/tmp/tmp.lekjbhelyx.txt")),
51            },
52            Example {
53                description: "Make a temporary file named testfile.XXX with the 'X's as random characters in the current working directory.",
54                example: "mktemp testfile.XXX",
55                result: Some(Value::test_string("<WORKING_DIR>/testfile.4kh")),
56            },
57            Example {
58                description: "Make a temporary file with a template in the system temp directory.",
59                example: "mktemp -t testfile.XXX",
60                result: Some(Value::test_string("/tmp/testfile.4kh")),
61            },
62            Example {
63                description: "Make a temporary directory with randomly generated name in the temporary directory.",
64                example: "mktemp -d",
65                result: Some(Value::test_string("/tmp/tmp.NMw9fJr8K0")),
66            },
67        ]
68    }
69
70    fn run(
71        &self,
72        engine_state: &EngineState,
73        stack: &mut Stack,
74        call: &Call,
75        _input: PipelineData,
76    ) -> Result<PipelineData, ShellError> {
77        // setup the uutils error translation
78        let _ = localized_help_template("mktemp");
79
80        let span = call.head;
81        let template = call
82            .rest(engine_state, stack, 0)?
83            .first()
84            .cloned()
85            .map(|i: Spanned<String>| i.item);
86        let directory = call.has_flag(engine_state, stack, "directory")?;
87        let dry_run = call.has_flag(engine_state, stack, "dry")?;
88        let suffix = call.get_flag(engine_state, stack, "suffix")?;
89        let tmpdir = template.is_none() || call.has_flag(engine_state, stack, "tmpdir")?;
90        let tmpdir_path = call
91            .get_flag(engine_state, stack, "tmpdir-path")?
92            .map(|i: Spanned<PathBuf>| i.item);
93        let template = template.unwrap_or("tmp.XXXXXXXXXX".to_string()); // same as default in coreutils
94
95        let tmpdir = if tmpdir_path.is_some() {
96            tmpdir_path
97        } else if directory || tmpdir {
98            Some(std::env::temp_dir())
99        } else {
100            Some(engine_state.cwd(Some(stack))?.into_std_path_buf())
101        };
102
103        let options = uu_mktemp::Options {
104            directory,
105            dry_run,
106            quiet: false,
107            suffix,
108            template: template.into(),
109            tmpdir,
110            treat_as_template: true,
111        };
112
113        let res = match uu_mktemp::mktemp(&options) {
114            Ok(res) => res
115                .into_os_string()
116                .into_string()
117                .map_err(|_| ShellError::NonUtf8 { span })?,
118            Err(error) => {
119                return Err(ShellError::Generic(GenericError::new_internal(
120                    format!("{error}"),
121                    translate!(&error.to_string()),
122                )));
123            }
124        };
125        Ok(PipelineData::value(Value::string(res, span), None))
126    }
127}