nu_command/filesystem/
mktemp.rs1use nu_engine::command_prelude::*;
2use std::path::PathBuf;
3use uucore::{localized_help_template, translate};
4
5#[derive(Clone)]
6pub struct Mktemp;
7
8impl Command for Mktemp {
9 fn name(&self) -> &str {
10 "mktemp"
11 }
12
13 fn description(&self) -> &str {
14 "Create temporary files or directories using uutils/coreutils mktemp."
15 }
16
17 fn search_terms(&self) -> Vec<&str> {
18 vec![
19 "create",
20 "directory",
21 "file",
22 "folder",
23 "temporary",
24 "coreutils",
25 ]
26 }
27
28 fn signature(&self) -> Signature {
29 Signature::build("mktemp")
30 .input_output_types(vec![(Type::Nothing, Type::String)])
31 .optional(
32 "template",
33 SyntaxShape::String,
34 "Optional pattern from which the name of the file or directory is derived. Must contain at least three 'X's in last component.",
35 )
36 .named("suffix", SyntaxShape::String, "Append suffix to template; must not contain a slash.", None)
37 .named("tmpdir-path", SyntaxShape::Filepath, "Interpret TEMPLATE relative to tmpdir-path. If tmpdir-path is not set use $TMPDIR", Some('p'))
38 .switch("tmpdir", "Interpret TEMPLATE relative to the system temporary directory.", Some('t'))
39 .switch("directory", "Create a directory instead of a file.", Some('d'))
40 .switch("dry", "Don't create a file and just return the path that would have been created.", None)
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 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()); 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 = 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
94 let tmpdir = if tmpdir_path.is_some() {
95 tmpdir_path
96 } else if directory || tmpdir {
97 Some(std::env::temp_dir())
98 } else {
99 Some(engine_state.cwd(Some(stack))?.into_std_path_buf())
100 };
101
102 let options = uu_mktemp::Options {
103 directory,
104 dry_run,
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}