mirror of
https://github.com/nushell/nushell.git
synced 2025-04-09 21:28:55 +02:00
parent
11352f87f0
commit
ee26590011
@ -10,7 +10,8 @@ pub struct Touch;
|
|||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct TouchArgs {
|
pub struct TouchArgs {
|
||||||
pub target: Tagged<PathBuf>,
|
target: Tagged<PathBuf>,
|
||||||
|
rest: Vec<Tagged<PathBuf>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@ -19,14 +20,16 @@ impl WholeStreamCommand for Touch {
|
|||||||
"touch"
|
"touch"
|
||||||
}
|
}
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("touch").required(
|
Signature::build("touch")
|
||||||
|
.required(
|
||||||
"filename",
|
"filename",
|
||||||
SyntaxShape::Path,
|
SyntaxShape::Path,
|
||||||
"the path of the file you want to create",
|
"the path of the file you want to create",
|
||||||
)
|
)
|
||||||
|
.rest(SyntaxShape::Path, "additional files to create")
|
||||||
}
|
}
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"creates a file"
|
"creates one or more files"
|
||||||
}
|
}
|
||||||
async fn run(
|
async fn run(
|
||||||
&self,
|
&self,
|
||||||
@ -37,27 +40,40 @@ impl WholeStreamCommand for Touch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![
|
||||||
|
Example {
|
||||||
description: "Creates \"fixture.json\"",
|
description: "Creates \"fixture.json\"",
|
||||||
example: "touch fixture.json",
|
example: "touch fixture.json",
|
||||||
result: None,
|
result: None,
|
||||||
}]
|
},
|
||||||
|
Example {
|
||||||
|
description: "Creates files a, b and c",
|
||||||
|
example: "touch a b c",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn touch(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
async fn touch(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
let registry = registry.clone();
|
let registry = registry.clone();
|
||||||
let (TouchArgs { target }, _) = args.process(®istry).await?;
|
let (TouchArgs { target, rest }, _) = args.process(®istry).await?;
|
||||||
|
|
||||||
match OpenOptions::new().write(true).create(true).open(&target) {
|
for item in vec![target].into_iter().chain(rest.into_iter()) {
|
||||||
Ok(_) => Ok(OutputStream::empty()),
|
match OpenOptions::new().write(true).create(true).open(&item) {
|
||||||
Err(err) => Err(ShellError::labeled_error(
|
Ok(_) => continue,
|
||||||
|
Err(err) => {
|
||||||
|
return Err(ShellError::labeled_error(
|
||||||
"File Error",
|
"File Error",
|
||||||
err.to_string(),
|
err.to_string(),
|
||||||
&target.tag,
|
&item.tag,
|
||||||
)),
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(OutputStream::empty())
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -10,7 +10,22 @@ fn creates_a_file_when_it_doesnt_exist() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let path = dirs.test().join("i_will_be_created.txt");
|
let path = dirs.test().join("i_will_be_created.txt");
|
||||||
|
|
||||||
assert!(path.exists());
|
assert!(path.exists());
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn creates_two_files() {
|
||||||
|
Playground::setup("create_test_2", |dirs, _sandbox| {
|
||||||
|
nu!(
|
||||||
|
cwd: dirs.test(),
|
||||||
|
"touch a b"
|
||||||
|
);
|
||||||
|
|
||||||
|
let path = dirs.test().join("a");
|
||||||
|
assert!(path.exists());
|
||||||
|
|
||||||
|
let path2 = dirs.test().join("b");
|
||||||
|
assert!(path2.exists());
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# touch
|
# touch
|
||||||
|
|
||||||
Create a file in the current or an already existent directory.
|
Create one or more files in the current or an already existent directory.
|
||||||
It has no effect on existing files.
|
It has no effect on existing files.
|
||||||
Unlike GNU touch, the access time and the modified time are not updated.
|
Unlike GNU touch, the access time and the modified time are not updated.
|
||||||
|
|
||||||
@ -41,3 +41,15 @@ Create a file within an already existent folder.
|
|||||||
modified │ 0 secs ago
|
modified │ 0 secs ago
|
||||||
──────────┴───────────
|
──────────┴───────────
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Create three files at oince
|
||||||
|
```shell
|
||||||
|
> touch a b c
|
||||||
|
> ls
|
||||||
|
────┬────────────────────┬──────┬──────────┬──────────────
|
||||||
|
# │ name │ type │ size │ modified
|
||||||
|
────┼────────────────────┼──────┼──────────┼──────────────
|
||||||
|
0 │ a │ File │ 0 B │ 0 sec ago
|
||||||
|
1 │ b │ File │ 0 B │ 0 sec ago
|
||||||
|
2 │ c │ File │ 0 B │ 0 sec ago
|
||||||
|
────┴────────────────────┴──────┴──────────┴──────────────
|
Loading…
Reference in New Issue
Block a user