Adds running an action

This commit is contained in:
2026-07-19 14:18:39 +02:00
parent c93e35b216
commit 7f9702fed7
3 changed files with 34 additions and 1 deletions
+20
View File
@@ -3,6 +3,7 @@ use crate::dtos::action::{ActionDTO, NewActionDTO, UpdateActionDTO};
use crate::models::action::Action; use crate::models::action::Action;
use crate::models::{AppState, DEFAULT_QUERY_LIMIT}; use crate::models::{AppState, DEFAULT_QUERY_LIMIT};
use crate::schema::actions::dsl::actions; use crate::schema::actions::dsl::actions;
use crate::sh;
use diesel::{QueryDsl, RunQueryDsl, SelectableHelper}; use diesel::{QueryDsl, RunQueryDsl, SelectableHelper};
use rocket::State; use rocket::State;
use rocket::http::Status; use rocket::http::Status;
@@ -84,6 +85,25 @@ pub fn get(
} }
} }
#[post("/<id>")]
pub fn run(id: &str, _auth: Authenticated, state: &State<AppState>) -> Result<String, Status> {
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("/<id>", format = "json", data = "<data>")] #[patch("/<id>", format = "json", data = "<data>")]
pub fn update( pub fn update(
id: &str, id: &str,
+3 -1
View File
@@ -7,12 +7,13 @@ mod auth;
mod dtos; mod dtos;
mod models; mod models;
mod schema; mod schema;
mod sh;
use crate::api::action; use crate::api::action;
use auth::CORS; use auth::CORS;
use chrono::Local; use chrono::Local;
use diesel::PgConnection;
use diesel::r2d2::{ConnectionManager, Pool}; use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;
use models::AppState; use models::AppState;
use rocket::State; use rocket::State;
use std::env; use std::env;
@@ -73,6 +74,7 @@ fn rocket() -> _ {
action::index, action::index,
action::create, action::create,
action::get, action::get,
action::run,
action::update, action::update,
action::delete action::delete
], ],
+11
View File
@@ -0,0 +1,11 @@
use std::process::Command;
pub fn sh(command: impl Into<String>) -> 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")
}