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

55 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;
2021-10-25 18:58:58 +02:00
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape};
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,
2021-10-25 08:31:39 +02:00
engine_state: &EngineState,
stack: &mut Stack,
2021-10-07 23:18:03 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-10-25 08:31:39 +02:00
let target: String = call.req(engine_state, stack, 0)?;
let rest: Vec<String> = call.rest(engine_state, stack, 1)?;
2021-10-08 00:20:23 +02:00
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
}
}
Ok(PipelineData::new(call.head))
2021-10-08 00:20:23 +02:00
}
2021-10-07 23:18:03 +02:00
}