2022-04-17 05:07:38 +02:00
|
|
|
use nu_protocol::engine::{EngineState, Stack};
|
2022-04-09 04:55:02 +02:00
|
|
|
use nu_protocol::{
|
|
|
|
ast::{Argument, Call, Expr, Expression},
|
|
|
|
engine::Command,
|
|
|
|
ShellError, Signature,
|
|
|
|
};
|
|
|
|
use nu_protocol::{PipelineData, Spanned, Type};
|
2022-02-11 19:38:10 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct KnownExternal {
|
|
|
|
pub name: String,
|
|
|
|
pub signature: Box<Signature>,
|
|
|
|
pub usage: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Command for KnownExternal {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
*self.signature.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
&self.usage
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_known_external(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-08-03 01:40:07 +02:00
|
|
|
fn is_builtin(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-02-11 19:38:10 +01:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let call_span = call.span();
|
2022-04-09 04:55:02 +02:00
|
|
|
let head_span = call.head;
|
|
|
|
let decl_id = engine_state
|
2022-05-07 21:39:22 +02:00
|
|
|
.find_decl("run-external".as_bytes(), &[])
|
2023-03-06 18:33:09 +01:00
|
|
|
.ok_or(ShellError::ExternalNotSupported { span: head_span })?;
|
2022-04-09 04:55:02 +02:00
|
|
|
|
|
|
|
let command = engine_state.get_decl(decl_id);
|
|
|
|
|
|
|
|
let mut extern_call = Call::new(head_span);
|
|
|
|
|
2023-01-22 20:40:18 +01:00
|
|
|
let extern_name = if let Some(name_bytes) = engine_state.find_decl_name(call.decl_id, &[]) {
|
|
|
|
String::from_utf8_lossy(name_bytes)
|
|
|
|
} else {
|
2023-03-06 18:33:09 +01:00
|
|
|
return Err(ShellError::NushellFailedSpanned {
|
|
|
|
msg: "known external name not found".to_string(),
|
|
|
|
label: "could not find name for this command".to_string(),
|
|
|
|
span: call.head,
|
|
|
|
});
|
2023-01-22 20:40:18 +01:00
|
|
|
};
|
2022-04-09 07:17:48 +02:00
|
|
|
|
|
|
|
let extern_name: Vec<_> = extern_name.split(' ').collect();
|
|
|
|
|
2022-04-09 04:55:02 +02:00
|
|
|
let arg_extern_name = Expression {
|
2022-04-09 07:17:48 +02:00
|
|
|
expr: Expr::String(extern_name[0].to_string()),
|
2022-04-09 04:55:02 +02:00
|
|
|
span: call.head,
|
|
|
|
ty: Type::String,
|
|
|
|
custom_completion: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
extern_call.add_positional(arg_extern_name);
|
|
|
|
|
2022-04-09 07:17:48 +02:00
|
|
|
for subcommand in extern_name.into_iter().skip(1) {
|
|
|
|
extern_call.add_positional(Expression {
|
|
|
|
expr: Expr::String(subcommand.to_string()),
|
|
|
|
span: call.head,
|
|
|
|
ty: Type::String,
|
|
|
|
custom_completion: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-09 04:55:02 +02:00
|
|
|
for arg in &call.arguments {
|
|
|
|
match arg {
|
|
|
|
Argument::Positional(positional) => extern_call.add_positional(positional.clone()),
|
2022-04-09 07:17:48 +02:00
|
|
|
Argument::Named(named) => {
|
|
|
|
if let Some(short) = &named.1 {
|
|
|
|
extern_call.add_positional(Expression {
|
|
|
|
expr: Expr::String(format!("-{}", short.item)),
|
|
|
|
span: named.0.span,
|
|
|
|
ty: Type::String,
|
|
|
|
custom_completion: None,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
extern_call.add_positional(Expression {
|
|
|
|
expr: Expr::String(format!("--{}", named.0.item)),
|
|
|
|
span: named.0.span,
|
|
|
|
ty: Type::String,
|
|
|
|
custom_completion: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if let Some(arg) = &named.2 {
|
|
|
|
extern_call.add_positional(arg.clone());
|
|
|
|
}
|
|
|
|
}
|
2022-12-21 23:33:26 +01:00
|
|
|
Argument::Unknown(unknown) => extern_call.add_unknown(unknown.clone()),
|
2022-04-09 04:55:02 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 23:22:21 +01:00
|
|
|
|
2022-04-09 04:55:02 +02:00
|
|
|
if call.redirect_stdout {
|
|
|
|
extern_call.add_named((
|
|
|
|
Spanned {
|
|
|
|
item: "redirect-stdout".into(),
|
|
|
|
span: call_span,
|
|
|
|
},
|
|
|
|
None,
|
2022-04-09 07:17:48 +02:00
|
|
|
None,
|
2022-04-09 04:55:02 +02:00
|
|
|
))
|
|
|
|
}
|
2022-02-11 19:38:10 +01:00
|
|
|
|
2022-04-09 04:55:02 +02:00
|
|
|
if call.redirect_stderr {
|
|
|
|
extern_call.add_named((
|
|
|
|
Spanned {
|
|
|
|
item: "redirect-stderr".into(),
|
|
|
|
span: call_span,
|
|
|
|
},
|
|
|
|
None,
|
2022-04-09 07:17:48 +02:00
|
|
|
None,
|
2022-04-09 04:55:02 +02:00
|
|
|
))
|
2022-02-11 19:38:10 +01:00
|
|
|
}
|
2022-04-09 04:55:02 +02:00
|
|
|
|
|
|
|
command.run(engine_state, stack, &extern_call, input)
|
2022-02-11 19:38:10 +01:00
|
|
|
}
|
|
|
|
}
|