nu_command/debug/
metadata_set.rs1use nu_engine::command_prelude::*;
2use nu_protocol::DataSource;
3
4#[derive(Clone)]
5pub struct MetadataSet;
6
7impl Command for MetadataSet {
8 fn name(&self) -> &str {
9 "metadata set"
10 }
11
12 fn description(&self) -> &str {
13 "Set the metadata for items in the stream."
14 }
15
16 fn signature(&self) -> nu_protocol::Signature {
17 Signature::build("metadata set")
18 .input_output_types(vec![(Type::Any, Type::Any)])
19 .switch(
20 "datasource-ls",
21 "Assign the DataSource::Ls metadata to the input",
22 Some('l'),
23 )
24 .named(
25 "datasource-filepath",
26 SyntaxShape::Filepath,
27 "Assign the DataSource::FilePath metadata to the input",
28 Some('f'),
29 )
30 .named(
31 "content-type",
32 SyntaxShape::String,
33 "Assign content type metadata to the input",
34 Some('c'),
35 )
36 .named(
37 "merge",
38 SyntaxShape::Record(vec![]),
39 "Merge arbitrary metadata fields",
40 Some('m'),
41 )
42 .allow_variants_without_examples(true)
43 .category(Category::Debug)
44 }
45
46 fn run(
47 &self,
48 engine_state: &EngineState,
49 stack: &mut Stack,
50 call: &Call,
51 mut input: PipelineData,
52 ) -> Result<PipelineData, ShellError> {
53 let head = call.head;
54 let ds_fp: Option<String> = call.get_flag(engine_state, stack, "datasource-filepath")?;
55 let ds_ls = call.has_flag(engine_state, stack, "datasource-ls")?;
56 let content_type: Option<String> = call.get_flag(engine_state, stack, "content-type")?;
57 let merge: Option<Value> = call.get_flag(engine_state, stack, "merge")?;
58
59 let mut metadata = match &mut input {
60 PipelineData::Value(_, metadata)
61 | PipelineData::ListStream(_, metadata)
62 | PipelineData::ByteStream(_, metadata) => metadata.take().unwrap_or_default(),
63 PipelineData::Empty => return Err(ShellError::PipelineEmpty { dst_span: head }),
64 };
65
66 if let Some(content_type) = content_type {
67 metadata.content_type = Some(content_type);
68 }
69
70 if let Some(merge) = merge {
71 let custom_record = merge.as_record()?;
72 for (key, value) in custom_record {
73 metadata.custom.insert(key.clone(), value.clone());
74 }
75 }
76
77 match (ds_fp, ds_ls) {
78 (Some(path), false) => metadata.data_source = DataSource::FilePath(path.into()),
79 (None, true) => metadata.data_source = DataSource::Ls,
80 (Some(_), true) => {
81 return Err(ShellError::IncompatibleParameters {
82 left_message: "cannot use `--datasource-filepath`".into(),
83 left_span: call
84 .get_flag_span(stack, "datasource-filepath")
85 .expect("has flag"),
86 right_message: "with `--datasource-ls`".into(),
87 right_span: call
88 .get_flag_span(stack, "datasource-ls")
89 .expect("has flag"),
90 });
91 }
92 (None, false) => (),
93 }
94
95 Ok(input.set_metadata(Some(metadata)))
96 }
97
98 fn examples(&self) -> Vec<Example<'_>> {
99 vec![
100 Example {
101 description: "Set the metadata of a table literal",
102 example: "[[name color]; [Cargo.lock '#ff0000'] [Cargo.toml '#00ff00'] [README.md '#0000ff']] | metadata set --datasource-ls",
103 result: None,
104 },
105 Example {
106 description: "Set the metadata of a file path",
107 example: "'crates' | metadata set --datasource-filepath $'(pwd)/crates'",
108 result: None,
109 },
110 Example {
111 description: "Set the content type metadata",
112 example: "'crates' | metadata set --content-type text/plain | metadata | get content_type",
113 result: Some(Value::test_string("text/plain")),
114 },
115 Example {
116 description: "Set custom metadata",
117 example: r#""data" | metadata set --merge {custom_key: "value"} | metadata | get custom_key"#,
118 result: Some(Value::test_string("value")),
119 },
120 ]
121 }
122}
123
124#[cfg(test)]
125mod test {
126 use crate::{Metadata, test_examples_with_commands};
127
128 use super::*;
129
130 #[test]
131 fn test_examples() {
132 test_examples_with_commands(MetadataSet {}, &[&Metadata {}])
133 }
134}