From 122ff1f19c3b27f546e1606d4190cd669b358771 Mon Sep 17 00:00:00 2001 From: Jack Wright <56345+ayax79@users.noreply.github.com> Date: Tue, 2 Jul 2024 13:35:55 -0700 Subject: [PATCH] Add the ability to set content-type metadata with `metadata set` (#13284) # Description With #13254, the content-type pipeline metadata field was added. This pull request allows it to be manipulated with `metadata set` # User-Facing Changes * `metadata set` now has a `--content-type` flag --- crates/nu-command/src/debug/metadata_set.rs | 68 +++++++++++---------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/crates/nu-command/src/debug/metadata_set.rs b/crates/nu-command/src/debug/metadata_set.rs index 5d6ba63745..e4ee97a76b 100644 --- a/crates/nu-command/src/debug/metadata_set.rs +++ b/crates/nu-command/src/debug/metadata_set.rs @@ -1,5 +1,5 @@ use nu_engine::command_prelude::*; -use nu_protocol::{DataSource, PipelineMetadata}; +use nu_protocol::DataSource; #[derive(Clone)] pub struct MetadataSet; @@ -27,6 +27,12 @@ impl Command for MetadataSet { "Assign the DataSource::FilePath metadata to the input", Some('f'), ) + .named( + "content-type", + SyntaxShape::String, + "Assign content type metadata to the input", + Some('c'), + ) .allow_variants_without_examples(true) .category(Category::Debug) } @@ -41,35 +47,30 @@ impl Command for MetadataSet { let head = call.head; let ds_fp: Option = call.get_flag(engine_state, stack, "datasource-filepath")?; let ds_ls = call.has_flag(engine_state, stack, "datasource-ls")?; + let content_type: Option = call.get_flag(engine_state, stack, "content-type")?; + + let metadata = input + .metadata() + .clone() + .unwrap_or_default() + .with_content_type(content_type); match (ds_fp, ds_ls) { - (Some(path), false) => { - let metadata = PipelineMetadata { - data_source: DataSource::FilePath(path.into()), - content_type: None, - }; - Ok(input.into_pipeline_data_with_metadata( - head, - engine_state.ctrlc.clone(), - metadata, - )) - } - (None, true) => { - let metadata = PipelineMetadata { - data_source: DataSource::Ls, - content_type: None, - }; - Ok(input.into_pipeline_data_with_metadata( - head, - engine_state.ctrlc.clone(), - metadata, - )) - } - _ => Err(ShellError::IncorrectValue { - msg: "Expected either --datasource-ls(-l) or --datasource-filepath(-f)".to_string(), - val_span: head, - call_span: head, - }), + (Some(path), false) => Ok(input.into_pipeline_data_with_metadata( + head, + engine_state.ctrlc.clone(), + metadata.with_data_source(DataSource::FilePath(path.into())), + )), + (None, true) => Ok(input.into_pipeline_data_with_metadata( + head, + engine_state.ctrlc.clone(), + metadata.with_data_source(DataSource::Ls), + )), + _ => Ok(input.into_pipeline_data_with_metadata( + head, + engine_state.ctrlc.clone(), + metadata, + )), } } @@ -85,18 +86,23 @@ impl Command for MetadataSet { example: "'crates' | metadata set --datasource-filepath $'(pwd)/crates' | metadata", result: None, }, + Example { + description: "Set the metadata of a file path", + example: "'crates' | metadata set --content-type text/plain | metadata", + result: Some(Value::record(record!("content_type" => Value::string("text/plain", Span::test_data())), Span::test_data())), + }, ] } } #[cfg(test)] mod test { + use crate::{test_examples_with_commands, Metadata}; + use super::*; #[test] fn test_examples() { - use crate::test_examples; - - test_examples(MetadataSet {}) + test_examples_with_commands(MetadataSet {}, &[&Metadata {}]) } }