mirror of
https://github.com/nushell/nushell.git
synced 2024-11-28 19:33:47 +01:00
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
This commit is contained in:
parent
0d060aeae8
commit
122ff1f19c
@ -1,5 +1,5 @@
|
|||||||
use nu_engine::command_prelude::*;
|
use nu_engine::command_prelude::*;
|
||||||
use nu_protocol::{DataSource, PipelineMetadata};
|
use nu_protocol::DataSource;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MetadataSet;
|
pub struct MetadataSet;
|
||||||
@ -27,6 +27,12 @@ impl Command for MetadataSet {
|
|||||||
"Assign the DataSource::FilePath metadata to the input",
|
"Assign the DataSource::FilePath metadata to the input",
|
||||||
Some('f'),
|
Some('f'),
|
||||||
)
|
)
|
||||||
|
.named(
|
||||||
|
"content-type",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"Assign content type metadata to the input",
|
||||||
|
Some('c'),
|
||||||
|
)
|
||||||
.allow_variants_without_examples(true)
|
.allow_variants_without_examples(true)
|
||||||
.category(Category::Debug)
|
.category(Category::Debug)
|
||||||
}
|
}
|
||||||
@ -41,35 +47,30 @@ impl Command for MetadataSet {
|
|||||||
let head = call.head;
|
let head = call.head;
|
||||||
let ds_fp: Option<String> = call.get_flag(engine_state, stack, "datasource-filepath")?;
|
let ds_fp: Option<String> = call.get_flag(engine_state, stack, "datasource-filepath")?;
|
||||||
let ds_ls = call.has_flag(engine_state, stack, "datasource-ls")?;
|
let ds_ls = call.has_flag(engine_state, stack, "datasource-ls")?;
|
||||||
|
let content_type: Option<String> = 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) {
|
match (ds_fp, ds_ls) {
|
||||||
(Some(path), false) => {
|
(Some(path), false) => Ok(input.into_pipeline_data_with_metadata(
|
||||||
let metadata = PipelineMetadata {
|
head,
|
||||||
data_source: DataSource::FilePath(path.into()),
|
engine_state.ctrlc.clone(),
|
||||||
content_type: None,
|
metadata.with_data_source(DataSource::FilePath(path.into())),
|
||||||
};
|
)),
|
||||||
Ok(input.into_pipeline_data_with_metadata(
|
(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,
|
head,
|
||||||
engine_state.ctrlc.clone(),
|
engine_state.ctrlc.clone(),
|
||||||
metadata,
|
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,
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,18 +86,23 @@ impl Command for MetadataSet {
|
|||||||
example: "'crates' | metadata set --datasource-filepath $'(pwd)/crates' | metadata",
|
example: "'crates' | metadata set --datasource-filepath $'(pwd)/crates' | metadata",
|
||||||
result: None,
|
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)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use crate::{test_examples_with_commands, Metadata};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_examples() {
|
fn test_examples() {
|
||||||
use crate::test_examples;
|
test_examples_with_commands(MetadataSet {}, &[&Metadata {}])
|
||||||
|
|
||||||
test_examples(MetadataSet {})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user