From aa95e6bdc844a4fcc79e27f329a9f41695eef649 Mon Sep 17 00:00:00 2001 From: ysthakur <45539777+ysthakur@users.noreply.github.com> Date: Wed, 10 Jul 2024 21:29:57 -0400 Subject: [PATCH] Update utouch command to match latest uu_touch --- Cargo.lock | 68 ++++++++++--------- Cargo.toml | 2 +- crates/nu-command/src/filesystem/utouch.rs | 78 ++++++++++++---------- 3 files changed, 81 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11500a4d24..160c3fe952 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6377,7 +6377,7 @@ dependencies = [ "indicatif", "libc", "quick-error 2.0.1", - "uucore 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore 0.0.26", "walkdir", "xattr", ] @@ -6389,7 +6389,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "040aa4584036b2f65e05387b0ea9ac468afce1db325743ce5f350689fd9ce4ae" dependencies = [ "clap", - "uucore 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore 0.0.26", ] [[package]] @@ -6401,7 +6401,7 @@ dependencies = [ "clap", "rand", "tempfile", - "uucore 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore 0.0.26", ] [[package]] @@ -6413,18 +6413,19 @@ dependencies = [ "clap", "fs_extra", "indicatif", - "uucore 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore 0.0.26", ] [[package]] name = "uu_touch" -version = "0.0.26" +version = "0.0.27" +source = "git+https://github.com/uutils/coreutils.git#c90b69398f708b4ab3b70c7497995f05f0ff1290" dependencies = [ "chrono", "clap", "filetime", "parse_datetime", - "uucore 0.0.26", + "uucore 0.0.27", "windows-sys 0.48.0", ] @@ -6436,7 +6437,7 @@ checksum = "5951832d73199636bde6c0d61cf960932b3c4450142c290375bc10c7abed6db5" dependencies = [ "clap", "platform-info", - "uucore 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore 0.0.26", ] [[package]] @@ -6447,7 +6448,7 @@ checksum = "e3b44166eb6335aeac42744ea368cc4c32d3f2287a4ff765a5ce44d927ab8bb4" dependencies = [ "clap", "libc", - "uucore 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore 0.0.26", "windows-sys 0.48.0", ] @@ -6463,21 +6464,6 @@ dependencies = [ "nix", "once_cell", "os_display", - "uucore_procs 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", - "wild", -] - -[[package]] -name = "uucore" -version = "0.0.26" -dependencies = [ - "clap", - "glob", - "libc", - "nix", - "number_prefix", - "once_cell", - "os_display", "uucore_procs 0.0.26", "wild", ] @@ -6496,7 +6482,7 @@ dependencies = [ "number_prefix", "once_cell", "os_display", - "uucore_procs 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "uucore_procs 0.0.26", "walkdir", "wild", "winapi-util", @@ -6505,12 +6491,19 @@ dependencies = [ ] [[package]] -name = "uucore_procs" -version = "0.0.26" +name = "uucore" +version = "0.0.27" +source = "git+https://github.com/uutils/coreutils.git#c90b69398f708b4ab3b70c7497995f05f0ff1290" dependencies = [ - "proc-macro2", - "quote", - "uuhelp_parser 0.0.26", + "clap", + "glob", + "libc", + "nix", + "number_prefix", + "once_cell", + "os_display", + "uucore_procs 0.0.27", + "wild", ] [[package]] @@ -6521,12 +6514,18 @@ checksum = "1a233a488da42f3ddb0aaa8a9f75a969e3f37e4de7e909d2d23f6aa3ee401d20" dependencies = [ "proc-macro2", "quote", - "uuhelp_parser 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)", + "uuhelp_parser 0.0.26", ] [[package]] -name = "uuhelp_parser" -version = "0.0.26" +name = "uucore_procs" +version = "0.0.27" +source = "git+https://github.com/uutils/coreutils.git#c90b69398f708b4ab3b70c7497995f05f0ff1290" +dependencies = [ + "proc-macro2", + "quote", + "uuhelp_parser 0.0.27", +] [[package]] name = "uuhelp_parser" @@ -6534,6 +6533,11 @@ version = "0.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "425a23c7b7145bc7620c9c445817c37b1f78b6790aee9f208133f3c028975b60" +[[package]] +name = "uuhelp_parser" +version = "0.0.27" +source = "git+https://github.com/uutils/coreutils.git#c90b69398f708b4ab3b70c7497995f05f0ff1290" + [[package]] name = "uuid" version = "1.8.0" diff --git a/Cargo.toml b/Cargo.toml index 286e23cb75..975441d4e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -163,7 +163,7 @@ uu_cp = "0.0.25" uu_mkdir = "0.0.25" uu_mktemp = "0.0.25" uu_mv = "0.0.25" -uu_touch = { path = "../coreutils/src/uu/touch" } +uu_touch = { git = "https://github.com/uutils/coreutils.git" } uu_whoami = "0.0.25" uu_uname = "0.0.25" uucore = "0.0.25" diff --git a/crates/nu-command/src/filesystem/utouch.rs b/crates/nu-command/src/filesystem/utouch.rs index 7bf712b1eb..b7e94ac5c5 100644 --- a/crates/nu-command/src/filesystem/utouch.rs +++ b/crates/nu-command/src/filesystem/utouch.rs @@ -5,14 +5,17 @@ use chrono::{DateTime, FixedOffset}; use filetime::FileTime; use nu_engine::CallExt; +use nu_path::expand_path_with; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, + Category, Example, NuGlob, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, }; use uu_touch::error::TouchError; use uu_touch::{ChangeTimes, InputFile, Options, Source}; +use super::util::get_rest_for_glob_pattern; + #[derive(Clone)] pub struct UTouch; @@ -28,21 +31,21 @@ impl Command for UTouch { fn signature(&self) -> Signature { Signature::build("utouch") .input_output_types(vec![ (Type::Nothing, Type::Nothing) ]) - .required( - "filename", - SyntaxShape::Filepath, - "The path of the file you want to create.", + .rest( + "files", + SyntaxShape::OneOf(vec![SyntaxShape::GlobPattern, SyntaxShape::Filepath]), + "The file(s) to create." ) .named( "reference", SyntaxShape::Filepath, - "change the file or directory time to the time of the reference file/directory", + "use the access and modification times of the reference file/directory instead of the current time", Some('r'), ) .named( "timestamp", SyntaxShape::DateTime, - "use the given time instead of the current time", + "use the given timestamp instead of the current time", Some('t') ) .named( @@ -53,12 +56,12 @@ impl Command for UTouch { ) .switch( "modified", - "change the modification time of the file or directory. If no timestamp, date or reference file/directory is given, the current time is used", + "change only the modification time (if used with -a, access time is changed too)", Some('m'), ) .switch( "access", - "change the access time of the file or directory. If no timestamp, date or reference file/directory is given, the current time is used", + "change only the access time (if used with -m, modification time is changed too)", Some('a'), ) .switch( @@ -90,8 +93,16 @@ impl Command for UTouch { let change_atime: bool = call.has_flag(engine_state, stack, "access")?; let no_create: bool = call.has_flag(engine_state, stack, "no-create")?; let no_deref: bool = call.has_flag(engine_state, stack, "no-dereference")?; - let target: Spanned = call.req(engine_state, stack, 0)?; - let rest: Vec> = call.rest(engine_state, stack, 1)?; + let file_globs: Vec> = + get_rest_for_glob_pattern(engine_state, stack, call, 0)?; + let cwd = engine_state.cwd(Some(stack))?; + + if file_globs.is_empty() { + return Err(ShellError::MissingParameter { + param_name: "requires file paths".to_string(), + span: call.head, + }); + } let (reference_file, reference_span) = if let Some(reference) = call.get_flag::>(engine_state, stack, "reference")? @@ -144,15 +155,14 @@ impl Command for UTouch { ChangeTimes::Both }; - let mut files = vec![InputFile::Path(PathBuf::from(target.item))]; - let mut file_spans = vec![target.span]; - for file in rest { - files.push(InputFile::Path(PathBuf::from(file.item))); - file_spans.push(file.span); + let mut input_files = Vec::new(); + for file_glob in &file_globs { + let path = expand_path_with(file_glob.item.as_ref(), &cwd, file_glob.item.is_expand()); + input_files.push(InputFile::Path(path)); } if let Err(err) = uu_touch::touch( - &files, + &input_files, &Options { no_create, no_deref, @@ -163,13 +173,21 @@ impl Command for UTouch { }, ) { let nu_err = match err { + TouchError::TouchFileError { path, index, error } => ShellError::GenericError { + error: format!("Could not touch {}", path.display()), + msg: error.to_string(), + span: Some(file_globs[index].span), + help: None, + inner: Vec::new(), + }, TouchError::InvalidDateFormat(date) => ShellError::IncorrectValue { msg: format!("Invalid date: {}", date), - val_span: date_span.expect("utouch was given a date"), + val_span: date_span.expect("utouch should've been given a date"), call_span: call.head, }, TouchError::ReferenceFileInaccessible(reference_path, io_err) => { - let span = reference_span.expect("utouch was given a reference file"); + let span = + reference_span.expect("utouch should've been given a reference file"); if io_err.kind() == ErrorKind::NotFound { // todo merge main into this to say which file not found ShellError::FileNotFound { @@ -177,11 +195,13 @@ impl Command for UTouch { file: reference_path.display().to_string(), } } else { - io_to_nu_err( - io_err, - format!("Failed to read metadata of {}", reference_path.display()), - span, - ) + ShellError::GenericError { + error: io_err.to_string(), + msg: format!("Failed to read metadata of {}", reference_path.display()), + span: Some(span), + help: None, + inner: Vec::new(), + } } } _ => ShellError::GenericError { @@ -233,13 +253,3 @@ impl Command for UTouch { ] } } - -fn io_to_nu_err(err: std::io::Error, msg: String, span: Span) -> ShellError { - ShellError::GenericError { - error: err.to_string(), - msg, - span: Some(span), - help: None, - inner: Vec::new(), - } -}