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 .allow_variants_without_examples(true)
37 .category(Category::Debug)
38 }
39
40 fn run(
41 &self,
42 engine_state: &EngineState,
43 stack: &mut Stack,
44 call: &Call,
45 mut input: PipelineData,
46 ) -> Result<PipelineData, ShellError> {
47 let head = call.head;
48 let ds_fp: Option<String> = call.get_flag(engine_state, stack, "datasource-filepath")?;
49 let ds_ls = call.has_flag(engine_state, stack, "datasource-ls")?;
50 let content_type: Option<String> = call.get_flag(engine_state, stack, "content-type")?;
51
52 let mut metadata = match &mut input {
53 PipelineData::Value(_, metadata)
54 | PipelineData::ListStream(_, metadata)
55 | PipelineData::ByteStream(_, metadata) => metadata.take().unwrap_or_default(),
56 PipelineData::Empty => return Err(ShellError::PipelineEmpty { dst_span: head }),
57 };
58
59 if let Some(content_type) = content_type {
60 metadata.content_type = Some(content_type);
61 }
62
63 match (ds_fp, ds_ls) {
64 (Some(path), false) => metadata.data_source = DataSource::FilePath(path.into()),
65 (None, true) => metadata.data_source = DataSource::Ls,
66 (Some(_), true) => (), (None, false) => (),
68 }
69
70 Ok(input.set_metadata(Some(metadata)))
71 }
72
73 fn examples(&self) -> Vec<Example> {
74 vec![
75 Example {
76 description: "Set the metadata of a table literal",
77 example: "[[name color]; [Cargo.lock '#ff0000'] [Cargo.toml '#00ff00'] [README.md '#0000ff']] | metadata set --datasource-ls",
78 result: None,
79 },
80 Example {
81 description: "Set the metadata of a file path",
82 example: "'crates' | metadata set --datasource-filepath $'(pwd)/crates' | metadata",
83 result: None,
84 },
85 Example {
86 description: "Set the metadata of a file path",
87 example: "'crates' | metadata set --content-type text/plain | metadata",
88 result: Some(Value::test_record(record! {
89 "content_type" => Value::test_string("text/plain"),
90 })),
91 },
92 ]
93 }
94}
95
96#[cfg(test)]
97mod test {
98 use crate::{test_examples_with_commands, Metadata};
99
100 use super::*;
101
102 #[test]
103 fn test_examples() {
104 test_examples_with_commands(MetadataSet {}, &[&Metadata {}])
105 }
106}