nushell/crates/nu-command/src/filesystem/touch.rs

54 lines
1.5 KiB
Rust
Raw Normal View History

2021-10-07 23:18:03 +02:00
use std::fs::OpenOptions;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
2021-10-25 06:01:02 +02:00
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape, Value};
2021-10-07 23:18:03 +02:00
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-10-07 23:18:03 +02:00
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")
}
fn usage(&self) -> &str {
"Creates one or more files."
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
2021-10-25 06:01:02 +02:00
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-10-08 00:20:23 +02:00
let target: String = call.req(context, 0)?;
let rest: Vec<String> = call.rest(context, 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,
));
}
2021-10-07 23:18:03 +02:00
}
}
2021-10-25 06:01:02 +02:00
Ok(PipelineData::new())
2021-10-08 00:20:23 +02:00
}
2021-10-07 23:18:03 +02:00
}