mirror of
https://github.com/nushell/nushell
synced 2025-01-24 10:55:17 +00:00
1235d516a5
* feat: update #4518, add examples for env,let-env,rm,touch and date list-timezone * fix typo * update example for `date list-timezone` command
70 lines
2 KiB
Rust
70 lines
2 KiB
Rust
use std::fs::OpenOptions;
|
|
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape};
|
|
|
|
#[derive(Clone)]
|
|
pub struct Touch;
|
|
|
|
impl Command for Touch {
|
|
fn name(&self) -> &str {
|
|
"touch"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("touch")
|
|
.required(
|
|
"filename",
|
|
SyntaxShape::Filepath,
|
|
"the path of the file you want to create",
|
|
)
|
|
.rest("rest", SyntaxShape::Filepath, "additional files to create")
|
|
.category(Category::FileSystem)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Creates one or more files."
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
_input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let target: String = call.req(engine_state, stack, 0)?;
|
|
let rest: Vec<String> = call.rest(engine_state, stack, 1)?;
|
|
|
|
for (index, item) in vec![target].into_iter().chain(rest).enumerate() {
|
|
match OpenOptions::new().write(true).create(true).open(&item) {
|
|
Ok(_) => continue,
|
|
Err(err) => {
|
|
return Err(ShellError::CreateNotPossible(
|
|
format!("Failed to create file: {}", err),
|
|
call.positional[index].span,
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(PipelineData::new(call.head))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Creates \"fixture.json\"",
|
|
example: "touch fixture.json",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Creates files a, b and c",
|
|
example: "touch a b c",
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|