nu_command/filesystem/
mktemp.rs

1#[allow(deprecated)]
2use nu_engine::{command_prelude::*, env::current_dir};
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.", Some('t'))
40            .switch("directory", "Create a directory instead of a file.", Some('d'))
41            .category(Category::FileSystem)
42    }
43
44    fn examples(&self) -> Vec<Example<'_>> {
45        vec![
46            Example {
47                description: "Make a temporary file with the given suffix in the current working directory.",
48                example: "mktemp --suffix .txt",
49                result: Some(Value::test_string("<WORKING_DIR>/tmp.lekjbhelyx.txt")),
50            },
51            Example {
52                description: "Make a temporary file named testfile.XXX with the 'X's as random characters in the current working directory.",
53                example: "mktemp testfile.XXX",
54                result: Some(Value::test_string("<WORKING_DIR>/testfile.4kh")),
55            },
56            Example {
57                description: "Make a temporary file with a template in the system temp directory.",
58                example: "mktemp -t testfile.XXX",
59                result: Some(Value::test_string("/tmp/testfile.4kh")),
60            },
61            Example {
62                description: "Make a temporary directory with randomly generated name in the temporary directory.",
63                example: "mktemp -d",
64                result: Some(Value::test_string("/tmp/tmp.NMw9fJr8K0")),
65            },
66        ]
67    }
68
69    fn run(
70        &self,
71        engine_state: &EngineState,
72        stack: &mut Stack,
73        call: &Call,
74        _input: PipelineData,
75    ) -> Result<PipelineData, ShellError> {
76        // setup the uutils error translation
77        let _ = localized_help_template("mktemp");
78
79        let span = call.head;
80        let template = call
81            .rest(engine_state, stack, 0)?
82            .first()
83            .cloned()
84            .map(|i: Spanned<String>| i.item)
85            .unwrap_or("tmp.XXXXXXXXXX".to_string()); // same as default in coreutils
86        let directory = call.has_flag(engine_state, stack, "directory")?;
87        let suffix = call.get_flag(engine_state, stack, "suffix")?;
88        let tmpdir = call.has_flag(engine_state, stack, "tmpdir")?;
89        let tmpdir_path = call
90            .get_flag(engine_state, stack, "tmpdir-path")?
91            .map(|i: Spanned<PathBuf>| i.item);
92
93        let tmpdir = if tmpdir_path.is_some() {
94            tmpdir_path
95        } else if directory || tmpdir {
96            Some(std::env::temp_dir())
97        } else {
98            #[allow(deprecated)]
99            Some(current_dir(engine_state, stack)?)
100        };
101
102        let options = uu_mktemp::Options {
103            directory,
104            dry_run: false,
105            quiet: false,
106            suffix,
107            template: template.into(),
108            tmpdir,
109            treat_as_template: true,
110        };
111
112        let res = match uu_mktemp::mktemp(&options) {
113            Ok(res) => res
114                .into_os_string()
115                .into_string()
116                .map_err(|_| ShellError::NonUtf8 { span })?,
117            Err(error) => {
118                return Err(ShellError::GenericError {
119                    error: format!("{error}"),
120                    msg: translate!(&error.to_string()),
121                    span: None,
122                    help: None,
123                    inner: vec![],
124                });
125            }
126        };
127        Ok(PipelineData::value(Value::string(res, span), None))
128    }
129}