Implement command tools
This commit is contained in:
parent
0398988074
commit
13e10cd4a8
@ -1,6 +1,6 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{identifier, AbstractTree, Expression, Identifier, Item, Table, Value};
|
use crate::{AbstractTree, Expression, Identifier, Item, Value};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub struct Select {
|
pub struct Select {
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs::{copy, metadata, read_dir, read_to_string, remove_file, write, File},
|
fs::{copy, metadata, read_dir, read_to_string, remove_file, write, File},
|
||||||
io::Write,
|
io::Write,
|
||||||
path::{Path, PathBuf},
|
path::PathBuf,
|
||||||
|
process::Command,
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::builder::PathBufValueParser;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
use tree_sitter::Node;
|
||||||
|
|
||||||
use crate::{AbstractTree, Error, Expression, Result, Table, Value, VariableMap};
|
use crate::{expression, AbstractTree, Error, Expression, Result, Table, Value, VariableMap};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub enum Tool {
|
pub enum Tool {
|
||||||
@ -34,11 +34,11 @@ pub enum Tool {
|
|||||||
ToString(Expression),
|
ToString(Expression),
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
Bash(Expression),
|
Bash(Vec<Expression>),
|
||||||
Fish(Expression),
|
Fish(Vec<Expression>),
|
||||||
Raw(Expression),
|
Raw(Vec<Expression>),
|
||||||
Sh(Expression),
|
Sh(Vec<Expression>),
|
||||||
Zsh(Expression),
|
Zsh(Vec<Expression>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AbstractTree for Tool {
|
impl AbstractTree for Tool {
|
||||||
@ -157,34 +157,29 @@ impl AbstractTree for Tool {
|
|||||||
Tool::ToString(expression)
|
Tool::ToString(expression)
|
||||||
}
|
}
|
||||||
"bash" => {
|
"bash" => {
|
||||||
let expression_node = node.child(2).unwrap();
|
let expressions = parse_expressions(source, node)?;
|
||||||
let expression = Expression::from_syntax_node(source, expression_node)?;
|
|
||||||
|
|
||||||
Tool::Bash(expression)
|
Tool::Bash(expressions)
|
||||||
}
|
}
|
||||||
"fish" => {
|
"fish" => {
|
||||||
let expression_node = node.child(2).unwrap();
|
let expressions = parse_expressions(source, node)?;
|
||||||
let expression = Expression::from_syntax_node(source, expression_node)?;
|
|
||||||
|
|
||||||
Tool::Fish(expression)
|
Tool::Fish(expressions)
|
||||||
}
|
}
|
||||||
"raw" => {
|
"raw" => {
|
||||||
let expression_node = node.child(2).unwrap();
|
let expressions = parse_expressions(source, node)?;
|
||||||
let expression = Expression::from_syntax_node(source, expression_node)?;
|
|
||||||
|
|
||||||
Tool::Raw(expression)
|
Tool::Raw(expressions)
|
||||||
}
|
}
|
||||||
"sh" => {
|
"sh" => {
|
||||||
let expression_node = node.child(2).unwrap();
|
let expressions = parse_expressions(source, node)?;
|
||||||
let expression = Expression::from_syntax_node(source, expression_node)?;
|
|
||||||
|
|
||||||
Tool::Sh(expression)
|
Tool::Sh(expressions)
|
||||||
}
|
}
|
||||||
"zsh" => {
|
"zsh" => {
|
||||||
let expression_node = node.child(2).unwrap();
|
let expressions = parse_expressions(source, node)?;
|
||||||
let expression = Expression::from_syntax_node(source, expression_node)?;
|
|
||||||
|
|
||||||
Tool::Zsh(expression)
|
Tool::Zsh(expressions)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(Error::UnexpectedSyntaxNode {
|
return Err(Error::UnexpectedSyntaxNode {
|
||||||
@ -370,11 +365,77 @@ impl AbstractTree for Tool {
|
|||||||
Ok(Value::String(json))
|
Ok(Value::String(json))
|
||||||
}
|
}
|
||||||
Tool::ToString(_) => todo!(),
|
Tool::ToString(_) => todo!(),
|
||||||
Tool::Bash(_) => todo!(),
|
Tool::Bash(expressions) => {
|
||||||
Tool::Fish(_) => todo!(),
|
let mut command = Command::new("bash");
|
||||||
Tool::Raw(_) => todo!(),
|
|
||||||
Tool::Sh(_) => todo!(),
|
for expression in expressions {
|
||||||
Tool::Zsh(_) => todo!(),
|
let value = expression.run(source, context)?;
|
||||||
|
let command_input = value.as_string()?;
|
||||||
|
|
||||||
|
command.arg(command_input);
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = command.spawn()?.wait_with_output()?.stdout;
|
||||||
|
|
||||||
|
Ok(Value::String(String::from_utf8(output)?))
|
||||||
|
}
|
||||||
|
Tool::Fish(expressions) => {
|
||||||
|
let mut command = Command::new("fish");
|
||||||
|
|
||||||
|
for expression in expressions {
|
||||||
|
let value = expression.run(source, context)?;
|
||||||
|
let command_input = value.as_string()?;
|
||||||
|
|
||||||
|
command.arg(command_input);
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = command.spawn()?.wait_with_output()?.stdout;
|
||||||
|
|
||||||
|
Ok(Value::String(String::from_utf8(output)?))
|
||||||
|
}
|
||||||
|
Tool::Raw(expressions) => {
|
||||||
|
let raw_command = expressions[0].run(source, context)?;
|
||||||
|
let mut command = Command::new(raw_command.as_string()?);
|
||||||
|
|
||||||
|
for expression in &expressions[1..] {
|
||||||
|
let value = expression.run(source, context)?;
|
||||||
|
let command_input = value.as_string()?;
|
||||||
|
|
||||||
|
command.arg(command_input);
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = command.spawn()?.wait_with_output()?.stdout;
|
||||||
|
|
||||||
|
Ok(Value::String(String::from_utf8(output)?))
|
||||||
|
}
|
||||||
|
Tool::Sh(expressions) => {
|
||||||
|
let mut command = Command::new("sh");
|
||||||
|
|
||||||
|
for expression in expressions {
|
||||||
|
let value = expression.run(source, context)?;
|
||||||
|
let command_input = value.as_string()?;
|
||||||
|
|
||||||
|
command.arg(command_input);
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = command.spawn()?.wait_with_output()?.stdout;
|
||||||
|
|
||||||
|
Ok(Value::String(String::from_utf8(output)?))
|
||||||
|
}
|
||||||
|
Tool::Zsh(expressions) => {
|
||||||
|
let mut command = Command::new("zsh");
|
||||||
|
|
||||||
|
for expression in expressions {
|
||||||
|
let value = expression.run(source, context)?;
|
||||||
|
let command_input = value.as_string()?;
|
||||||
|
|
||||||
|
command.arg(command_input);
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = command.spawn()?.wait_with_output()?.stdout;
|
||||||
|
|
||||||
|
Ok(Value::String(String::from_utf8(output)?))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
use crate::{value::Value, Identifier};
|
use crate::{value::Value, Identifier};
|
||||||
|
|
||||||
use std::{fmt, io, time};
|
use std::{fmt, io, time, string::FromUtf8Error};
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
@ -140,6 +140,12 @@ impl Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<FromUtf8Error> for Error {
|
||||||
|
fn from(value: FromUtf8Error) -> Self {
|
||||||
|
Error::ToolFailure(value.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<csv::Error> for Error {
|
impl From<csv::Error> for Error {
|
||||||
fn from(value: csv::Error) -> Self {
|
fn from(value: csv::Error) -> Self {
|
||||||
Error::ToolFailure(value.to_string())
|
Error::ToolFailure(value.to_string())
|
||||||
|
Loading…
Reference in New Issue
Block a user