-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move AppendCommand buffer to nu/commands
Moved the handler's buffered version of `AppendCommand` to `nu/commands` to make loading custom commands into the engine more modular.
- Loading branch information
Showing
11 changed files
with
179 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use nu_engine::CallExt; | ||
use nu_protocol::engine::{Call, Command, EngineState, Stack}; | ||
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Type, Value}; | ||
|
||
use crate::nu::util::value_to_json; | ||
use crate::store::{Frame, Store, TTL}; | ||
|
||
#[derive(Clone)] | ||
pub struct AppendCommand { | ||
output: Arc<Mutex<Vec<Frame>>>, | ||
store: Store, | ||
} | ||
|
||
impl AppendCommand { | ||
pub fn new(store: Store, output: Arc<Mutex<Vec<Frame>>>) -> Self { | ||
Self { output, store } | ||
} | ||
} | ||
|
||
impl Command for AppendCommand { | ||
fn name(&self) -> &str { | ||
".append" | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
Signature::build(".append") | ||
.input_output_types(vec![(Type::Any, Type::Any)]) | ||
.required("topic", SyntaxShape::String, "this clip's topic") | ||
.named( | ||
"meta", | ||
SyntaxShape::Record(vec![]), | ||
"arbitrary metadata", | ||
None, | ||
) | ||
.named( | ||
"ttl", | ||
SyntaxShape::String, | ||
r#"TTL specification: 'forever', 'ephemeral', 'time:<milliseconds>', or 'head:<n>'"#, | ||
None, | ||
) | ||
.category(Category::Experimental) | ||
} | ||
|
||
fn description(&self) -> &str { | ||
"Writes its input to the CAS and buffers a frame for later batch processing. The frame will include the content hash, any provided metadata and TTL settings. Meant for use with handlers that need to batch multiple appends." | ||
} | ||
|
||
fn run( | ||
&self, | ||
engine_state: &EngineState, | ||
stack: &mut Stack, | ||
call: &Call, | ||
input: PipelineData, | ||
) -> Result<PipelineData, ShellError> { | ||
let span = call.head; | ||
|
||
let topic: String = call.req(engine_state, stack, 0)?; | ||
let meta: Option<Value> = call.get_flag(engine_state, stack, "meta")?; | ||
let ttl_str: Option<String> = call.get_flag(engine_state, stack, "ttl")?; | ||
|
||
let ttl = ttl_str | ||
.map(|s| TTL::from_query(Some(&format!("ttl={}", s)))) | ||
.transpose() | ||
.map_err(|e| ShellError::GenericError { | ||
error: "Invalid TTL format".into(), | ||
msg: e.to_string(), | ||
span: Some(span), | ||
help: Some("TTL must be one of: 'forever', 'ephemeral', 'time:<milliseconds>', or 'head:<n>'".into()), | ||
inner: vec![], | ||
})?; | ||
|
||
let input_value = input.into_value(span)?; | ||
|
||
let rt = tokio::runtime::Runtime::new() | ||
.map_err(|e| ShellError::IOError { msg: e.to_string() })?; | ||
|
||
let hash = rt.block_on(async { | ||
crate::nu::util::write_pipeline_to_cas( | ||
PipelineData::Value(input_value.clone(), None), | ||
&self.store, | ||
span, | ||
) | ||
.await | ||
})?; | ||
|
||
let frame = Frame::with_topic(topic) | ||
.maybe_meta(meta.map(|v| value_to_json(&v))) | ||
.maybe_hash(hash) | ||
.maybe_ttl(ttl) | ||
.build(); | ||
|
||
self.output.lock().unwrap().push(frame); | ||
|
||
Ok(PipelineData::Empty) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,6 @@ | ||
mod append_command; | ||
mod cas_command; | ||
mod cat_command; | ||
mod head_command; | ||
mod remove_command; | ||
|
||
use crate::store::Store; | ||
use nu_protocol::engine::EngineState; | ||
|
||
pub fn add_custom_commands(store: Store, mut engine_state: EngineState) -> EngineState { | ||
let delta = { | ||
let mut working_set = nu_protocol::engine::StateWorkingSet::new(&engine_state); | ||
working_set.add_decl(Box::new(cas_command::CasCommand::new(store.clone()))); | ||
working_set.add_decl(Box::new(append_command::AppendCommand::new(store.clone()))); | ||
working_set.add_decl(Box::new(head_command::HeadCommand::new(store.clone()))); | ||
working_set.add_decl(Box::new(remove_command::RemoveCommand::new(store.clone()))); | ||
working_set.add_decl(Box::new(cat_command::CatCommand::new(store.clone()))); | ||
|
||
// Add the .rm alias | ||
let alias_expression = "alias .rm = .remove"; | ||
let _ = nu_parser::parse(&mut working_set, None, alias_expression.as_bytes(), false); | ||
|
||
working_set.render() | ||
}; | ||
|
||
if let Err(err) = engine_state.merge_delta(delta) { | ||
eprintln!("Error adding custom commands: {:?}", err); | ||
} | ||
|
||
engine_state | ||
} | ||
pub mod append_command; | ||
pub mod append_command_buffered; | ||
pub mod cas_command; | ||
pub mod cat_command; | ||
pub mod head_command; | ||
pub mod remove_command; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.