From 8967315c49bee04249c7e8a8ab4e94ed5819cd21 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 17 Sep 2024 12:29:36 +0200 Subject: [PATCH] refactor: terminal / run command --- app/Livewire/RunCommand.php | 71 ------------------- app/Livewire/Terminal/Index.php | 63 ++++++++++++++++ .../views/livewire/run-command.blade.php | 22 ------ .../views/livewire/terminal/index.blade.php | 24 ++++++- 4 files changed, 86 insertions(+), 94 deletions(-) delete mode 100644 app/Livewire/RunCommand.php delete mode 100644 resources/views/livewire/run-command.blade.php diff --git a/app/Livewire/RunCommand.php b/app/Livewire/RunCommand.php deleted file mode 100644 index 3650f2e41..000000000 --- a/app/Livewire/RunCommand.php +++ /dev/null @@ -1,71 +0,0 @@ -user()->isAdmin()) { - abort(403); - } - $this->servers = Server::isReachable()->get(); - $this->containers = $this->getAllActiveContainers(); - } - - private function getAllActiveContainers() - { - return collect($this->servers)->flatMap(function ($server) { - if (! $server->isFunctional()) { - return []; - } - - return $server->loadAllContainers()->map(function ($container) use ($server) { - $state = data_get_str($container, 'State')->lower(); - if ($state->contains('running')) { - return [ - 'name' => data_get($container, 'Names'), - 'connection_name' => data_get($container, 'Names'), - 'uuid' => data_get($container, 'Names'), - 'status' => data_get_str($container, 'State')->lower(), - 'server' => $server, - 'server_uuid' => $server->uuid, - ]; - } - - return null; - })->filter(); - }); - } - - public function updatedSelectedUuid() - { - $this->connectToContainer(); - } - - #[On('connectToContainer')] - public function connectToContainer() - { - if ($this->selected_uuid === 'default') { - $this->dispatch('error', 'Please select a server or a container.'); - - return; - } - $container = collect($this->containers)->firstWhere('uuid', $this->selected_uuid); - $this->dispatch('send-terminal-command', - isset($container), - $container['connection_name'] ?? $this->selected_uuid, - $container['server_uuid'] ?? $this->selected_uuid - ); - } -} diff --git a/app/Livewire/Terminal/Index.php b/app/Livewire/Terminal/Index.php index 8c59bdcbf..945b25714 100644 --- a/app/Livewire/Terminal/Index.php +++ b/app/Livewire/Terminal/Index.php @@ -2,10 +2,73 @@ namespace App\Livewire\Terminal; +use App\Models\Server; +use Livewire\Attributes\On; use Livewire\Component; class Index extends Component { + public $selected_uuid = 'default'; + + public $servers = []; + + public $containers = []; + + public function mount() + { + if (! auth()->user()->isAdmin()) { + abort(403); + } + $this->servers = Server::isReachable()->get(); + $this->containers = $this->getAllActiveContainers(); + } + + private function getAllActiveContainers() + { + return collect($this->servers)->flatMap(function ($server) { + if (! $server->isFunctional()) { + return []; + } + + return $server->loadAllContainers()->map(function ($container) use ($server) { + $state = data_get_str($container, 'State')->lower(); + if ($state->contains('running')) { + return [ + 'name' => data_get($container, 'Names'), + 'connection_name' => data_get($container, 'Names'), + 'uuid' => data_get($container, 'Names'), + 'status' => data_get_str($container, 'State')->lower(), + 'server' => $server, + 'server_uuid' => $server->uuid, + ]; + } + + return null; + })->filter(); + }); + } + + public function updatedSelectedUuid() + { + $this->connectToContainer(); + } + + #[On('connectToContainer')] + public function connectToContainer() + { + if ($this->selected_uuid === 'default') { + $this->dispatch('error', 'Please select a server or a container.'); + + return; + } + $container = collect($this->containers)->firstWhere('uuid', $this->selected_uuid); + $this->dispatch('send-terminal-command', + isset($container), + $container['connection_name'] ?? $this->selected_uuid, + $container['server_uuid'] ?? $this->selected_uuid + ); + } + public function render() { return view('livewire.terminal.index'); diff --git a/resources/views/livewire/run-command.blade.php b/resources/views/livewire/run-command.blade.php deleted file mode 100644 index 4330e94cf..000000000 --- a/resources/views/livewire/run-command.blade.php +++ /dev/null @@ -1,22 +0,0 @@ -
-
- - @foreach ($servers as $server) - @if ($loop->first) - - @endif - - @foreach ($containers as $container) - @if ($container['server_uuid'] == $server->uuid) - - @endif - @endforeach - @endforeach - - Connect -
- -
diff --git a/resources/views/livewire/terminal/index.blade.php b/resources/views/livewire/terminal/index.blade.php index 34a2d4c21..357295002 100644 --- a/resources/views/livewire/terminal/index.blade.php +++ b/resources/views/livewire/terminal/index.blade.php @@ -8,5 +8,27 @@ - +
+
+ + @foreach ($servers as $server) + @if ($loop->first) + + @endif + + @foreach ($containers as $container) + @if ($container['server_uuid'] == $server->uuid) + + @endif + @endforeach + @endforeach + + Connect +
+ +
+