use std::collections::HashMap;
use async_trait::async_trait;
use crate::plugin::{Plugin, PluginCommand, PluginMetadata};
use crate::system;
type PluginResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub struct SystemPlugin {
metadata: PluginMetadata,
}
impl SystemPlugin {
pub fn new() -> Self {
Self {
metadata: PluginMetadata {
name: "system".to_string(),
version: "1.0.0".to_string(),
description: "System plugin for OpenScript".to_string(),
author: "OpenScript Team".to_string(),
license: "MIT".to_string(),
dependencies: vec![],
api_version: "1.0".to_string(),
},
}
}
}
#[async_trait]
impl Plugin for SystemPlugin {
fn metadata(&self) -> &PluginMetadata {
&self.metadata
}
async fn initialize(&mut self) -> PluginResult<()> {
Ok(())
}
fn register_commands(&self) -> HashMap<String, Box<dyn PluginCommand>> {
let mut commands = HashMap::new();
commands.insert("sys_exec".to_string(), Box::new(ExecCommand) as Box<dyn PluginCommand>);
commands
}
async fn cleanup(&mut self) -> PluginResult<()> {
Ok(())
}
}
struct ExecCommand;
#[async_trait]
impl PluginCommand for ExecCommand {
async fn execute(&self, args: &[String]) -> PluginResult<String> {
if args.is_empty() {
return Err("sys_exec() requires a command".into());
}
let cmd = &args[0];
let cmd_args = &args[1..];
let result = system::exec(cmd, cmd_args).await.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
Ok(result.to_string())
}
fn help(&self) -> &str {
"Executes a system command."
}
fn usage(&self) -> &str {
"sys_exec <command> [args...]"
}
}