use crate::{
DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info, warn};
#[derive(Debug)]
pub struct ApplicationControlInstallDriver;
#[async_trait::async_trait]
impl Driver for ApplicationControlInstallDriver {
fn name(&self) -> &str {
"application_control_install"
}
fn description(&self) -> &str {
"Install an application using the system package manager"
}
fn usage_hint(&self) -> &str {
"Use this skill to install software packages. On Windows, uses winget. On Linux, uses apt/yum. On macOS, uses brew."
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![DriverParameter {
name: "package".to_string(),
param_type: "string".to_string(),
description: "Package name to install".to_string(),
required: true,
default: None,
example: Some(Value::String("firefox".to_string())),
enum_values: None,
}];
}
fn example_call(&self) -> DriverResult<Value> {
Ok(json!({
"action": "application_control_install",
"parameters": {
"package": "firefox"
}
}))
}
fn example_output(&self) -> String {
"Package firefox installed successfully".to_string()
}
fn category(&self) -> DriverCategory {
DriverCategory::Application
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing application_control_install driver");
let package = parameters.get("package").and_then(|v| v.as_str()).ok_or_else(|| {
debug!("Missing 'package' parameter");
DriverError::missing_parameter("package")
})?;
debug!("Installing package: {}", package);
#[cfg(target_os = "windows")]
{
use crate::hidden_cmd;
info!("Installing package via winget: {}", package);
let output =
hidden_cmd("winget").args(["install", package, "--accept-package-agreements", "--silent"]).output().map_err(|e| {
let msg = format!("Failed to execute winget: {}", e);
warn!("{}", msg);
DriverError::execution(msg)
})?;
if output.status.success() {
info!("Package installed successfully: {}", package);
Ok(format!("Package {} installed successfully", package))
} else {
let error = String::from_utf8_lossy(&output.stderr);
warn!("Package installation failed: {}", error);
Err(DriverError::execution(format!("Failed to install package: {}", error)))
}
}
#[cfg(target_os = "linux")]
{
info!("Installing package via apt-get: {}", package);
let output = hidden_cmd("sudo").args(["apt-get", "install", "-y", package]).output().map_err(|e| {
let msg = format!("Failed to execute apt-get: {}", e);
warn!("{}", msg);
DriverError::execution(msg)
})?;
if output.status.success() {
info!("Package installed successfully: {}", package);
Ok(format!("Package {} installed successfully", package))
} else {
let error = String::from_utf8_lossy(&output.stderr);
warn!("Package installation failed: {}", error);
Err(DriverError::execution(format!("Failed to install package: {}", error)))
}
}
#[cfg(target_os = "macos")]
{
info!("Installing package via brew: {}", package);
let output = hidden_cmd("brew").args(["install", package]).output().map_err(|e| {
let msg = format!("Failed to execute brew: {}", e);
warn!("{}", msg);
DriverError::execution(msg)
})?;
if output.status.success() {
info!("Package installed successfully: {}", package);
Ok(format!("Package {} installed successfully", package))
} else {
let error = String::from_utf8_lossy(&output.stderr);
warn!("Package installation failed: {}", error);
Err(DriverError::execution(format!("Failed to install package: {}", error)))
}
}
#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
{
let msg = "Install not implemented on this platform";
warn!("{}", msg);
Err(DriverError::execution(msg))
}
}
}