From 7f9702fed77133b0acb5520700fb5b53b3bc63d1 Mon Sep 17 00:00:00 2001 From: Ash Svitan Date: Sun, 19 Jul 2026 14:18:39 +0200 Subject: [PATCH] :sparkles: Adds running an action --- backend/src/api/action.rs | 20 ++++++++++++++++++++ backend/src/main.rs | 4 +++- backend/src/sh.rs | 11 +++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 backend/src/sh.rs diff --git a/backend/src/api/action.rs b/backend/src/api/action.rs index 050f8c4..31efa95 100644 --- a/backend/src/api/action.rs +++ b/backend/src/api/action.rs @@ -3,6 +3,7 @@ use crate::dtos::action::{ActionDTO, NewActionDTO, UpdateActionDTO}; use crate::models::action::Action; use crate::models::{AppState, DEFAULT_QUERY_LIMIT}; use crate::schema::actions::dsl::actions; +use crate::sh; use diesel::{QueryDsl, RunQueryDsl, SelectableHelper}; use rocket::State; use rocket::http::Status; @@ -84,6 +85,25 @@ pub fn get( } } +#[post("/")] +pub fn run(id: &str, _auth: Authenticated, state: &State) -> Result { + let mut db = state.db.get().unwrap(); + + let id = match Uuid::parse_str(id).ok() { + Some(id) => id, + None => return Err(Status::BadRequest), + }; + + let action = actions.find(id).first(&mut db).ok(); + if let None = action { + return Err(Status::NotFound); + } + let action = action.unwrap(); + + let result = sh::sh(format!("{}", ActionDTO::from(&action).source)); + Ok(result) +} + #[patch("/", format = "json", data = "")] pub fn update( id: &str, diff --git a/backend/src/main.rs b/backend/src/main.rs index 4ccfece..8b5500d 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -7,12 +7,13 @@ mod auth; mod dtos; mod models; mod schema; +mod sh; use crate::api::action; use auth::CORS; use chrono::Local; -use diesel::PgConnection; use diesel::r2d2::{ConnectionManager, Pool}; +use diesel::PgConnection; use models::AppState; use rocket::State; use std::env; @@ -73,6 +74,7 @@ fn rocket() -> _ { action::index, action::create, action::get, + action::run, action::update, action::delete ], diff --git a/backend/src/sh.rs b/backend/src/sh.rs new file mode 100644 index 0000000..cfd51db --- /dev/null +++ b/backend/src/sh.rs @@ -0,0 +1,11 @@ +use std::process::Command; + +pub fn sh(command: impl Into) -> String { + let output = Command::new("sh") + .arg("-c") + .arg(command.into()) + .output() + .expect("Failed to execute command"); + + String::from_utf8(output.stdout).expect("Failed to parse output") +}