Skip to content

Commit 8967315

Browse files
committed
refactor: terminal / run command
1 parent 162fb7b commit 8967315

File tree

4 files changed

+86
-94
lines changed

4 files changed

+86
-94
lines changed

app/Livewire/RunCommand.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

app/Livewire/Terminal/Index.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,73 @@
22

33
namespace App\Livewire\Terminal;
44

5+
use App\Models\Server;
6+
use Livewire\Attributes\On;
57
use Livewire\Component;
68

79
class Index extends Component
810
{
11+
public $selected_uuid = 'default';
12+
13+
public $servers = [];
14+
15+
public $containers = [];
16+
17+
public function mount()
18+
{
19+
if (! auth()->user()->isAdmin()) {
20+
abort(403);
21+
}
22+
$this->servers = Server::isReachable()->get();
23+
$this->containers = $this->getAllActiveContainers();
24+
}
25+
26+
private function getAllActiveContainers()
27+
{
28+
return collect($this->servers)->flatMap(function ($server) {
29+
if (! $server->isFunctional()) {
30+
return [];
31+
}
32+
33+
return $server->loadAllContainers()->map(function ($container) use ($server) {
34+
$state = data_get_str($container, 'State')->lower();
35+
if ($state->contains('running')) {
36+
return [
37+
'name' => data_get($container, 'Names'),
38+
'connection_name' => data_get($container, 'Names'),
39+
'uuid' => data_get($container, 'Names'),
40+
'status' => data_get_str($container, 'State')->lower(),
41+
'server' => $server,
42+
'server_uuid' => $server->uuid,
43+
];
44+
}
45+
46+
return null;
47+
})->filter();
48+
});
49+
}
50+
51+
public function updatedSelectedUuid()
52+
{
53+
$this->connectToContainer();
54+
}
55+
56+
#[On('connectToContainer')]
57+
public function connectToContainer()
58+
{
59+
if ($this->selected_uuid === 'default') {
60+
$this->dispatch('error', 'Please select a server or a container.');
61+
62+
return;
63+
}
64+
$container = collect($this->containers)->firstWhere('uuid', $this->selected_uuid);
65+
$this->dispatch('send-terminal-command',
66+
isset($container),
67+
$container['connection_name'] ?? $this->selected_uuid,
68+
$container['server_uuid'] ?? $this->selected_uuid
69+
);
70+
}
71+
972
public function render()
1073
{
1174
return view('livewire.terminal.index');

resources/views/livewire/run-command.blade.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

resources/views/livewire/terminal/index.blade.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,27 @@
88
<x-helper
99
helper="If you're having trouble connecting to your server, make sure that the port is open.<br><br><a class='underline' href='https://coolify.io/docs/knowledge-base/server/firewall/#terminal' target='_blank'>Documentation</a>"></x-helper>
1010
</div>
11-
<livewire:run-command />
11+
<div>
12+
<form class="flex flex-col gap-2 justify-center xl:items-end xl:flex-row"
13+
wire:submit="$dispatchSelf('connectToContainer')">
14+
<x-forms.select id="server" required wire:model.live="selected_uuid">
15+
@foreach ($servers as $server)
16+
@if ($loop->first)
17+
<option disabled value="default">Select a server or container</option>
18+
@endif
19+
<option value="{{ $server->uuid }}">{{ $server->name }}</option>
20+
@foreach ($containers as $container)
21+
@if ($container['server_uuid'] == $server->uuid)
22+
<option value="{{ $container['uuid'] }}">
23+
{{ $server->name }} -> {{ $container['name'] }}
24+
</option>
25+
@endif
26+
@endforeach
27+
@endforeach
28+
</x-forms.select>
29+
<x-forms.button type="submit">Connect</x-forms.button>
30+
</form>
31+
<livewire:project.shared.terminal />
32+
</div>
33+
1234
</div>

0 commit comments

Comments
 (0)