mirror of
https://codeberg.org/selfsigned-ash/antifed
synced 2026-07-21 18:49:34 +02:00
✨ Adds running an action
This commit is contained in:
@@ -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("/<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>")]
|
||||
pub fn update(
|
||||
id: &str,
|
||||
|
||||
+3
-1
@@ -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
|
||||
],
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user