xs/nu/commands/
cas_post_command.rs1use nu_protocol::engine::{Call, Command, EngineState, Stack};
2use nu_protocol::shell_error::generic::GenericError;
3use nu_protocol::{Category, PipelineData, ShellError, Signature, Type, Value};
4
5use crate::nu::util;
6use crate::store::Store;
7
8#[derive(Clone)]
9pub struct CasPostCommand {
10 store: Store,
11}
12
13impl CasPostCommand {
14 pub fn new(store: Store) -> Self {
15 Self { store }
16 }
17}
18
19impl Command for CasPostCommand {
20 fn name(&self) -> &str {
21 ".cas-post"
22 }
23
24 fn signature(&self) -> Signature {
25 Signature::build(".cas-post")
26 .input_output_types(vec![(Type::Any, Type::String)])
27 .category(Category::Experimental)
28 }
29
30 fn description(&self) -> &str {
31 "Write the pipeline input to the CAS and return its integrity hash. The write counterpart to .cas."
32 }
33
34 fn run(
35 &self,
36 _engine_state: &EngineState,
37 _stack: &mut Stack,
38 call: &Call,
39 input: PipelineData,
40 ) -> Result<PipelineData, ShellError> {
41 match util::write_pipeline_to_cas(input, &self.store, call.head).map_err(|boxed| *boxed)? {
42 Some(hash) => Ok(PipelineData::Value(
43 Value::string(hash.to_string(), call.head),
44 None,
45 )),
46 None => Err(ShellError::Generic(GenericError::new(
47 "Empty input",
48 "Nothing to write to the CAS",
49 call.head,
50 ))),
51 }
52 }
53}