nu_command/filesystem/
mktemp.rs1#[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 .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 current working directory.",
49 example: "mktemp --suffix .txt",
50 result: Some(Value::test_string("<WORKING_DIR>/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 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 .unwrap_or("tmp.XXXXXXXXXX".to_string()); let directory = call.has_flag(engine_state, stack, "directory")?;
88 let dry_run = call.has_flag(engine_state, stack, "dry")?;
89 let suffix = call.get_flag(engine_state, stack, "suffix")?;
90 let tmpdir = call.has_flag(engine_state, stack, "tmpdir")?;
91 let tmpdir_path = call
92 .get_flag(engine_state, stack, "tmpdir-path")?
93 .map(|i: Spanned<PathBuf>| i.item);
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 #[allow(deprecated)]
101 Some(current_dir(engine_state, stack)?)
102 };
103
104 let options = uu_mktemp::Options {
105 directory,
106 dry_run,
107 quiet: false,
108 suffix,
109 template: template.into(),
110 tmpdir,
111 treat_as_template: true,
112 };
113
114 let res = match uu_mktemp::mktemp(&options) {
115 Ok(res) => res
116 .into_os_string()
117 .into_string()
118 .map_err(|_| ShellError::NonUtf8 { span })?,
119 Err(error) => {
120 return Err(ShellError::GenericError {
121 error: format!("{error}"),
122 msg: translate!(&error.to_string()),
123 span: None,
124 help: None,
125 inner: vec![],
126 });
127 }
128 };
129 Ok(PipelineData::value(Value::string(res, span), None))
130 }
131}