From adabc839bffa6c0d6de83d8f98b429c394299f1e Mon Sep 17 00:00:00 2001 From: xiuxiu62 Date: Thu, 7 Oct 2021 14:18:03 -0700 Subject: [PATCH] add touch command --- crates/nu-command/src/default_context.rs | 1 + crates/nu-command/src/filesystem/mod.rs | 2 + crates/nu-command/src/filesystem/touch.rs | 56 +++++++++++++++++++++++ crates/nu-protocol/src/shell_error.rs | 4 ++ 4 files changed, 63 insertions(+) create mode 100644 crates/nu-command/src/filesystem/touch.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index fd2ea37555..06abdd8bbd 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -41,6 +41,7 @@ pub fn create_default_context() -> Rc> { working_set.add_decl(Box::new(Select)); working_set.add_decl(Box::new(Sys)); working_set.add_decl(Box::new(Table)); + working_set.add_decl(Box::new(Touch)); working_set.add_decl(Box::new(Use)); working_set.add_decl(Box::new(Where)); working_set.add_decl(Box::new(Wrap)); diff --git a/crates/nu-command/src/filesystem/mod.rs b/crates/nu-command/src/filesystem/mod.rs index 2d2212766a..926ffe2e54 100644 --- a/crates/nu-command/src/filesystem/mod.rs +++ b/crates/nu-command/src/filesystem/mod.rs @@ -2,9 +2,11 @@ mod cd; mod cp; mod ls; mod mv; +mod touch; mod util; pub use cd::Cd; pub use cp::Cp; pub use ls::Ls; pub use mv::Mv; +pub use touch::Touch; diff --git a/crates/nu-command/src/filesystem/touch.rs b/crates/nu-command/src/filesystem/touch.rs new file mode 100644 index 0000000000..c895538858 --- /dev/null +++ b/crates/nu-command/src/filesystem/touch.rs @@ -0,0 +1,56 @@ +use std::fs::OpenOptions; + +use nu_engine::CallExt; +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EvaluationContext}; +use nu_protocol::{ShellError, Signature, SyntaxShape, Value}; + +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, + _input: Value, + ) -> Result { + touch(context, call) + } +} + +fn touch(context: &EvaluationContext, call: &Call) -> Result { + let target: String = call.req(context, 0)?; + let rest: Vec = 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, + )); + } + } + } + + Ok(Value::Nothing { span: call.head }) +} diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 6a7464508e..ab7da16eae 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -109,6 +109,10 @@ pub enum ShellError { #[error("Move not possible")] #[diagnostic(code(nu::shell::move_not_possible_single), url(docsrs))] MoveNotPossibleSingle(String, #[label("{0}")] Span), + + #[error("Create not possible")] + #[diagnostic(code(nu::shell::move_not_possible_single), url(docsrs))] + CreateNotPossible(String, #[label("{0}")] Span), } impl From for ShellError {