From aa27851bd7374c65cd9512bb96386b7deeae6569 Mon Sep 17 00:00:00 2001 From: Oskar-Mikael Date: Thu, 4 Sep 2025 17:57:15 +0200 Subject: [PATCH] Add basecontract --- app/Bots/BashScript.php | 27 ++++++++++++------------ app/Bots/GenericApi.php | 1 + app/Interfaces/BaseContract.php | 7 ++++++ app/{Bots => Interfaces}/BotContract.php | 4 ++-- app/Interfaces/ScriptContract.php | 9 ++++++++ app/Livewire/CreateEditBot.php | 7 +++--- app/Services/BotService.php | 6 ++++-- 7 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 app/Interfaces/BaseContract.php rename app/{Bots => Interfaces}/BotContract.php (66%) create mode 100644 app/Interfaces/ScriptContract.php diff --git a/app/Bots/BashScript.php b/app/Bots/BashScript.php index c71fcf6..93fdd8d 100644 --- a/app/Bots/BashScript.php +++ b/app/Bots/BashScript.php @@ -2,9 +2,10 @@ namespace App\Bots; +use App\Interfaces\ScriptContract; use Illuminate\Support\Facades\Log; -class BashScript implements BotContract +class BashScript implements ScriptContract { protected array $config; @@ -13,22 +14,20 @@ class BashScript implements BotContract $this->config = $config; } - public function run(): void + public function run(): string { - if (!empty($this->config['script'])) { - // Execute the bash script - $script = $this->config['script']; - $output = []; - $returnVar = null; - exec($script, $output, $returnVar); + // Execute the bash script + $script = $this->config['script']; + $output = []; + $returnVar = null; + exec($script, $output, $returnVar); - if ($returnVar !== 0) { - // Log error if the script failed - \Illuminate\Support\Facades\Log::error("Bash script execution failed: " . implode("\n", $output)); - } else { - Log::info("Bash script executed successfully: " . implode("\n", $output)); - } + + if ($returnVar !== 0) { + Log::error("Bash script execution failed", ['script' => $script, 'output' => $output, 'returnVar' => $returnVar]); + throw new \RuntimeException("Bash script execution failed with return code {$returnVar}"); } + return implode("\n", $output); } public static function configSchema(): array diff --git a/app/Bots/GenericApi.php b/app/Bots/GenericApi.php index e9285d3..7389fcf 100644 --- a/app/Bots/GenericApi.php +++ b/app/Bots/GenericApi.php @@ -2,6 +2,7 @@ namespace App\Bots; +use App\Interfaces\BotContract; use GuzzleHttp\Client; use Illuminate\Http\JsonResponse; diff --git a/app/Interfaces/BaseContract.php b/app/Interfaces/BaseContract.php new file mode 100644 index 0000000..b1fa324 --- /dev/null +++ b/app/Interfaces/BaseContract.php @@ -0,0 +1,7 @@ +class))) { + if (!in_array(BaseContract::class, class_implements($this->class))) { $this->addError('class', 'The specified class does not exist.'); return; } diff --git a/app/Services/BotService.php b/app/Services/BotService.php index fc32022..fa4c198 100644 --- a/app/Services/BotService.php +++ b/app/Services/BotService.php @@ -2,8 +2,10 @@ namespace App\Services; +use App\Interfaces\BaseContract; use App\Models\Bot; -use App\Bots\BotContract; +use App\Interfaces\BotContract; +use App\Interfaces\ScriptContract; use App\Jobs\RunBot; use Cron\CronExpression; use Illuminate\Support\Facades\Log; @@ -25,7 +27,7 @@ class BotService try { $instance = app($bot->class, ['config' => $bot->config]); - if ($instance instanceof BotContract) { + if ($instance instanceof BaseContract) { RunBot::dispatch(bot_id: $bot->id, log_id: $log->id); $log->update([ 'started_at' => now(),