This commit is contained in:
Saeed Vaziry
2025-06-04 08:08:20 +02:00
parent efacadba10
commit c3f69f3247
114 changed files with 4032 additions and 765 deletions

View File

@ -7,6 +7,7 @@
use App\Models\CommandExecution;
use App\Models\ServerLog;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
class ExecuteCommand
{
@ -15,19 +16,31 @@ class ExecuteCommand
*/
public function execute(Command $command, User $user, array $input): CommandExecution
{
Validator::make($input, self::rules($command))->validate();
$variables = [];
foreach ($command->getVariables() as $variable) {
if (array_key_exists($variable, $input)) {
$variables[$variable] = $input[$variable] ?? '';
}
}
$execution = new CommandExecution([
'command_id' => $command->id,
'server_id' => $command->site->server_id,
'user_id' => $user->id,
'variables' => $input['variables'] ?? [],
'variables' => $variables,
'status' => CommandExecutionStatus::EXECUTING,
]);
$execution->save();
dispatch(function () use ($execution, $command): void {
$log = ServerLog::newLog($execution->server, 'command-'.$command->id.'-'.strtotime('now'));
$log->save();
$execution->server_log_id = $log->id;
$execution->save();
dispatch(function () use ($execution, $command, $log): void {
$content = $execution->getContent();
$log = ServerLog::newLog($execution->server, 'command-'.$command->id.'-'.strtotime('now'));
$log->save();
$execution->server_log_id = $log->id;
$execution->save();
$execution->server->os()->runScript(
@ -48,18 +61,19 @@ public function execute(Command $command, User $user, array $input): CommandExec
}
/**
* @param array<string, mixed> $input
* @return array<string, string|array<int, mixed>>
*/
public static function rules(array $input): array
public static function rules(Command $command): array
{
return [
'variables' => 'array',
'variables.*' => [
$rules = [];
foreach ($command->getVariables() as $variable) {
$rules[$variable] = [
'required',
'string',
'max:255',
],
];
];
}
return $rules;
}
}