Internal to external (#51)

Add a simple form of internal->external support
This commit is contained in:
Jonathan Turner 2019-06-01 15:19:03 +12:00 committed by GitHub
parent ffe2896aac
commit 5043367d11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 18 deletions

View file

@ -212,14 +212,11 @@ async fn process_line(readline: Result<String, ReadlineError>, ctx: &mut Context
}, },
( (
Some(ClassifiedCommand::Internal(ref i)), Some(ClassifiedCommand::Internal(left)),
Some(ClassifiedCommand::External(ref e)), Some(ClassifiedCommand::External(_)),
) => { ) => match left.run(ctx, input).await {
return LineResult::Error(ShellError::string(&format!( Ok(val) => ClassifiedInputStream::from_input_stream(val),
"Unimplemented Internal({}) -> External({})", Err(err) => return LineResult::Error(err),
i.name(),
e.name()
)))
} }
( (

View file

@ -105,10 +105,6 @@ impl InternalCommand {
Ok(stream.boxed() as InputStream) Ok(stream.boxed() as InputStream)
} }
crate fn name(&self) -> &str {
self.command.name()
}
} }
crate struct ExternalCommand { crate struct ExternalCommand {
@ -129,10 +125,33 @@ impl ExternalCommand {
input: ClassifiedInputStream, input: ClassifiedInputStream,
stream_next: StreamNext, stream_next: StreamNext,
) -> Result<ClassifiedInputStream, ShellError> { ) -> Result<ClassifiedInputStream, ShellError> {
let inputs: Vec<Value> = input.objects.collect().await;
let mut arg_string = format!("{}", self.name);
for arg in &self.args {
arg_string.push_str(" ");
arg_string.push_str(&arg);
}
let mut process = Exec::shell(&self.name); let mut process = Exec::shell(&self.name);
for arg in self.args { if arg_string.contains("$it") {
process = process.arg(arg) let mut first = true;
for i in &inputs {
if !first {
process = process.arg("&&");
process = process.arg(&self.name);
} else {
first = false;
}
for arg in &self.args {
process = process.arg(&arg.replace("$it", &i.as_string().unwrap()));
}
}
} else {
for arg in &self.args {
process = process.arg(arg);
}
} }
process = process.cwd(context.env.lock().unwrap().cwd()); process = process.cwd(context.env.lock().unwrap().cwd());
@ -168,8 +187,4 @@ impl ExternalCommand {
} }
} }
} }
crate fn name(&self) -> &str {
&self.name[..]
}
} }