Fix plugin's commandconfig

This commit is contained in:
Jonathan Turner 2019-07-16 19:08:35 +12:00
parent 7ffab5441b
commit 3ebb6ba991
10 changed files with 65 additions and 31 deletions

View File

@ -6,6 +6,7 @@ use crate::commands::classified::{
}; };
use crate::commands::command::sink; use crate::commands::command::sink;
use crate::commands::plugin::JsonRpc; use crate::commands::plugin::JsonRpc;
use crate::commands::plugin::{PluginCommand, PluginSink};
use crate::context::Context; use crate::context::Context;
crate use crate::errors::ShellError; crate use crate::errors::ShellError;
use crate::evaluate::Scope; use crate::evaluate::Scope;
@ -43,8 +44,6 @@ impl<T> MaybeOwned<'a, T> {
} }
fn load_plugin(path: &std::path::Path, context: &mut Context) -> Result<(), ShellError> { fn load_plugin(path: &std::path::Path, context: &mut Context) -> Result<(), ShellError> {
use crate::commands::{command, plugin};
let mut child = std::process::Command::new(path) let mut child = std::process::Command::new(path)
.stdin(std::process::Stdio::piped()) .stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped())
@ -70,20 +69,17 @@ fn load_plugin(path: &std::path::Path, context: &mut Context) -> Result<(), Shel
Ok(jrpc) => match jrpc.params { Ok(jrpc) => match jrpc.params {
Ok(params) => { Ok(params) => {
let fname = path.to_string_lossy(); let fname = path.to_string_lossy();
//println!("Loaded: {} from {}", params.name, fname);
if params.is_filter { if params.is_filter {
let fname = fname.to_string(); let fname = fname.to_string();
context.add_commands(vec![command( let name = params.name.clone();
&params.name, context.add_commands(vec![Arc::new(PluginCommand::new(
Box::new(move |x| plugin::filter_plugin(fname.clone(), x)), name, fname, params,
)]); ))]);
Ok(()) Ok(())
} else if params.is_sink { } else if params.is_sink {
let fname = fname.to_string(); let fname = fname.to_string();
context.add_sinks(vec![sink( let name = params.name.clone();
&params.name, context.add_sinks(vec![Arc::new(PluginSink::new(name, fname, params))]);
Box::new(move |x| plugin::sink_plugin(fname.clone(), x)),
)]);
Ok(()) Ok(())
} else { } else {
Ok(()) Ok(())

View File

@ -1,4 +1,5 @@
#[doc(hidden)] #[doc(hidden)]
#[allow(unused)]
macro_rules! named_type { macro_rules! named_type {
($name:ident) => { ($name:ident) => {
$crate::parser::registry::NamedType::$($name)* $crate::parser::registry::NamedType::$($name)*

View File

@ -1,6 +1,7 @@
use crate::commands::command::SinkCommandArgs;
use crate::errors::ShellError; use crate::errors::ShellError;
use crate::parser::registry;
use crate::prelude::*; use crate::prelude::*;
use derive_new::new;
use serde::{self, Deserialize, Serialize}; use serde::{self, Deserialize, Serialize};
use std::io::prelude::*; use std::io::prelude::*;
use std::io::BufReader; use std::io::BufReader;
@ -32,6 +33,44 @@ pub enum NuResult {
}, },
} }
#[derive(new)]
pub struct PluginCommand {
name: String,
path: String,
config: registry::CommandConfig,
}
impl Command for PluginCommand {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
filter_plugin(self.path.clone(), args)
}
fn name(&self) -> &str {
&self.name
}
fn config(&self) -> registry::CommandConfig {
self.config.clone()
}
}
#[derive(new)]
pub struct PluginSink {
name: String,
path: String,
config: registry::CommandConfig,
}
impl Sink for PluginSink {
fn run(&self, args: SinkCommandArgs) -> Result<(), ShellError> {
sink_plugin(self.path.clone(), args)
}
fn name(&self) -> &str {
&self.name
}
fn config(&self) -> registry::CommandConfig {
self.config.clone()
}
}
pub fn filter_plugin(path: String, args: CommandArgs) -> Result<OutputStream, ShellError> { pub fn filter_plugin(path: String, args: CommandArgs) -> Result<OutputStream, ShellError> {
let mut child = std::process::Command::new(path) let mut child = std::process::Command::new(path)
.stdin(std::process::Stdio::piped()) .stdin(std::process::Stdio::piped())

View File

@ -32,4 +32,4 @@ pub use cli::cli;
pub use errors::ShellError; pub use errors::ShellError;
pub use object::base::{Primitive, Value}; pub use object::base::{Primitive, Value};
pub use parser::parse::text::Text; pub use parser::parse::text::Text;
pub use parser::registry::{Args, CommandConfig, NamedType, NamedValue, PositionalType}; pub use parser::registry::{Args, CommandConfig, NamedType, PositionalType};

View File

@ -54,9 +54,6 @@ impl ExtractType for Spanned<Value> {
} }
} }
#[derive(Debug)]
pub struct FilePath;
impl ExtractType for std::path::PathBuf { impl ExtractType for std::path::PathBuf {
fn syntax_type() -> hir::SyntaxType { fn syntax_type() -> hir::SyntaxType {
hir::SyntaxType::Path hir::SyntaxType::Path
@ -66,7 +63,7 @@ impl ExtractType for std::path::PathBuf {
match &value { match &value {
Spanned { Spanned {
item: Value::Primitive(Primitive::String(p)), item: Value::Primitive(Primitive::String(p)),
span, ..
} => Ok(PathBuf::from(p)), } => Ok(PathBuf::from(p)),
other => Err(ShellError::type_error("Path", other.spanned_type_name())), other => Err(ShellError::type_error("Path", other.spanned_type_name())),
} }

View File

@ -21,7 +21,7 @@ pub fn baseline_parse_token_as_string(token: &Token, source: &Text) -> hir::Expr
} }
RawToken::Variable(span) => hir::Expression::variable(span, token.span), RawToken::Variable(span) => hir::Expression::variable(span, token.span),
RawToken::Integer(_) => hir::Expression::bare(token.span), RawToken::Integer(_) => hir::Expression::bare(token.span),
RawToken::Size(int, unit) => hir::Expression::bare(token.span), RawToken::Size(_, _) => hir::Expression::bare(token.span),
RawToken::Bare => hir::Expression::bare(token.span), RawToken::Bare => hir::Expression::bare(token.span),
RawToken::String(span) => hir::Expression::string(span, token.span), RawToken::String(span) => hir::Expression::string(span, token.span),
} }

View File

@ -102,15 +102,15 @@ impl TokenNode {
pub fn type_name(&self) -> String { pub fn type_name(&self) -> String {
match self { match self {
TokenNode::Token(t) => t.type_name(), TokenNode::Token(t) => t.type_name(),
TokenNode::Call(s) => "command", TokenNode::Call(_) => "command",
TokenNode::Delimited(d) => d.type_name(), TokenNode::Delimited(d) => d.type_name(),
TokenNode::Pipeline(s) => "pipeline", TokenNode::Pipeline(_) => "pipeline",
TokenNode::Operator(s) => "operator", TokenNode::Operator(_) => "operator",
TokenNode::Flag(s) => "flag", TokenNode::Flag(_) => "flag",
TokenNode::Member(s) => "member", TokenNode::Member(_) => "member",
TokenNode::Whitespace(s) => "whitespace", TokenNode::Whitespace(_) => "whitespace",
TokenNode::Error(s) => "error", TokenNode::Error(_) => "error",
TokenNode::Path(s) => "path", TokenNode::Path(_) => "path",
} }
.to_string() .to_string()
} }

View File

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
#[allow(unused)] #[allow(unused)]
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub enum NamedType { pub enum NamedType {
Switch, Switch,
Mandatory(SyntaxType), Mandatory(SyntaxType),
@ -36,6 +36,7 @@ impl PositionalType {
PositionalType::Mandatory(name.to_string(), SyntaxType::Block) PositionalType::Mandatory(name.to_string(), SyntaxType::Block)
} }
#[allow(unused)]
crate fn to_coerce_hint(&self) -> Option<SyntaxType> { crate fn to_coerce_hint(&self) -> Option<SyntaxType> {
match self { match self {
PositionalType::Mandatory(_, SyntaxType::Block) PositionalType::Mandatory(_, SyntaxType::Block)
@ -59,7 +60,7 @@ impl PositionalType {
} }
} }
#[derive(Debug, Getters, Serialize, Deserialize)] #[derive(Debug, Getters, Serialize, Deserialize, Clone)]
#[get = "crate"] #[get = "crate"]
pub struct CommandConfig { pub struct CommandConfig {
pub name: String, pub name: String,

View File

@ -14,14 +14,14 @@ impl BinaryView {
impl Plugin for BinaryView { impl Plugin for BinaryView {
fn config(&mut self) -> Result<CommandConfig, ShellError> { fn config(&mut self) -> Result<CommandConfig, ShellError> {
let mut named = IndexMap::new(); let mut named = IndexMap::new();
named.insert("--lores".to_string(), NamedType::Switch); named.insert("lores".to_string(), NamedType::Switch);
Ok(CommandConfig { Ok(CommandConfig {
name: "binaryview".to_string(), name: "binaryview".to_string(),
positional: vec![], positional: vec![],
is_filter: false, is_filter: false,
is_sink: true, is_sink: true,
named, named,
rest_positional: true, rest_positional: false,
}) })
} }

View File

@ -34,7 +34,7 @@ macro_rules! trace_stream {
crate use crate::cli::MaybeOwned; crate use crate::cli::MaybeOwned;
crate use crate::commands::command::{ crate use crate::commands::command::{
Command, CommandAction, CommandArgs, ReturnSuccess, ReturnValue, Command, CommandAction, CommandArgs, ReturnSuccess, ReturnValue, Sink, SinkCommandArgs,
}; };
crate use crate::context::Context; crate use crate::context::Context;
crate use crate::env::host::handle_unexpected; crate use crate::env::host::handle_unexpected;