An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat: developer view for shared env variables
Aditya Tripathi85a1483356c3
diff --git a/app/Livewire/SharedVariables/Environment/Show.php b/app/Livewire/SharedVariables/Environment/Show.phpindex bee757a64c..6c8342c41b 100644--- a/app/Livewire/SharedVariables/Environment/Show.php+++ b/app/Livewire/SharedVariables/Environment/Show.php@@ -19,7 +19,11 @@ class Show extends Componentpublic array $parameters;- protected $listeners = ['refreshEnvs' => '$refresh', 'saveKey', 'environmentVariableDeleted' => '$refresh'];+ public string $view = 'normal';++ public ?string $variables = null;++ protected $listeners = ['refreshEnvs' => 'refreshEnvs', 'saveKey', 'environmentVariableDeleted' => 'refreshEnvs'];public function saveKey($data){@@ -39,6 +43,7 @@ class Show extends Component'team_id' => currentTeam()->id,]);$this->environment->refresh();+ $this->getDevView();} catch (\Throwable $e) {return handleError($e, $this);}@@ -49,6 +54,116 @@ class Show extends Component$this->parameters = get_route_parameters();$this->project = Project::ownedByCurrentTeam()->where('uuid', request()->route('project_uuid'))->firstOrFail();$this->environment = $this->project->environments()->where('uuid', request()->route('environment_uuid'))->firstOrFail();+ $this->getDevView();+ }++ public function switch()+ {+ $this->view = $this->view === 'normal' ? 'dev' : 'normal';+ $this->getDevView();+ }++ public function getDevView()+ {+ $this->variables = $this->formatEnvironmentVariables($this->environment->environment_variables->sortBy('key'));+ }++ private function formatEnvironmentVariables($variables)+ {+ return $variables->map(function ($item) {+ if ($item->is_shown_once) {+ return "$item->key=(Locked Secret, delete and add again to change)";+ }+ if ($item->is_multiline) {+ return "$item->key=(Multiline environment variable, edit in normal view)";+ }++ return "$item->key=$item->value";+ })->join("\n");+ }++ public function submit()+ {+ try {+ $this->authorize('update', $this->environment);+ $this->handleBulkSubmit();+ $this->getDevView();+ } catch (\Throwable $e) {+ return handleError($e, $this);+ } finally {+ $this->refreshEnvs();+ }+ }++ private function handleBulkSubmit()+ {+ $variables = parseEnvFormatToArray($this->variables);+ $changesMade = false;++ // Delete removed variables+ $deletedCount = $this->deleteRemovedVariables($variables);+ if ($deletedCount > 0) {+ $changesMade = true;+ }++ // Update or create variables+ $updatedCount = $this->updateOrCreateVariables($variables);+ if ($updatedCount > 0) {+ $changesMade = true;+ }++ if ($changesMade) {+ $this->dispatch('success', 'Environment variables updated.');+ }+ }++ private function deleteRemovedVariables($variables)+ {+ $variablesToDelete = $this->environment->environment_variables()->whereNotIn('key', array_keys($variables))->get();++ if ($variablesToDelete->isEmpty()) {+ return 0;+ }++ $this->environment->environment_variables()->whereNotIn('key', array_keys($variables))->delete();++ return $variablesToDelete->count();+ }++ private function updateOrCreateVariables($variables)+ {+ $count = 0;+ foreach ($variables as $key => $value) {+ $found = $this->environment->environment_variables()->where('key', $key)->first();++ if ($found) {+ if (! $found->is_shown_once && ! $found->is_multiline) {+ if ($found->value !== $value) {+ $found->value = $value;+ $found->save();+ $count++;+ }+ }+ } else {+ $this->environment->environment_variables()->create([+ 'key' => $key,+ 'value' => $value,+ 'is_multiline' => false,+ 'is_literal' => false,+ 'type' => 'environment',+ 'team_id' => currentTeam()->id,+ ]);+ $count++;+ }+ }++ return $count;+ }++ public function refreshEnvs()+ {+ $this->environment->refresh();+ $this->getDevView();}public function render()diff --git a/app/Livewire/SharedVariables/Project/Show.php b/app/Livewire/SharedVariables/Project/Show.phpindex 712a9960b3..a3abe5df23 100644--- a/app/Livewire/SharedVariables/Project/Show.php+++ b/app/Livewire/SharedVariables/Project/Show.php@@ -12,7 +12,11 @@ class Show extends Componentpublic Project $project;- protected $listeners = ['refreshEnvs' => '$refresh', 'saveKey' => 'saveKey', 'environmentVariableDeleted' => '$refresh'];+ public string $view = 'normal';++ public ?string $variables = null;++ protected $listeners = ['refreshEnvs' => 'refreshEnvs', 'saveKey' => 'saveKey', 'environmentVariableDeleted' => 'refreshEnvs'];public function saveKey($data){@@ -32,6 +36,7 @@ class Show extends Component'team_id' => currentTeam()->id,]);$this->project->refresh();+ $this->getDevView();} catch (\Throwable $e) {return handleError($e, $this);}@@ -46,6 +51,116 @@ class Show extends Componentreturn redirect()->route('dashboard');}$this->project = $project;+ $this->getDevView();+ }++ public function switch()+ {+ $this->view = $this->view === 'normal' ? 'dev' : 'normal';+ $this->getDevView();+ }++ public function getDevView()+ {+ $this->variables = $this->formatEnvironmentVariables($this->project->environment_variables->sortBy('key'));+ }++ private function formatEnvironmentVariables($variables)+ {+ return $variables->map(function ($item) {+ if ($item->is_shown_once) {+ return "$item->key=(Locked Secret, delete and add again to change)";+ }+ if ($item->is_multiline) {+ return "$item->key=(Multiline environment variable, edit in normal view)";+ }++ return "$item->key=$item->value";+ })->join("\n");+ }++ public function submit()+ {+ try {+ $this->authorize('update', $this->project);+ $this->handleBulkSubmit();+ $this->getDevView();+ } catch (\Throwable $e) {+ return handleError($e, $this);+ } finally {+ $this->refreshEnvs();+ }+ }++ private function handleBulkSubmit()+ {+ $variables = parseEnvFormatToArray($this->variables);+ $changesMade = false;++ // Delete removed variables+ $deletedCount = $this->deleteRemovedVariables($variables);+ if ($deletedCount > 0) {+ $changesMade = true;+ }++ // Update or create variables+ $updatedCount = $this->updateOrCreateVariables($variables);+ if ($updatedCount > 0) {+ $changesMade = true;+ }++ if ($changesMade) {+ $this->dispatch('success', 'Environment variables updated.');+ }+ }++ private function deleteRemovedVariables($variables)+ {+ $variablesToDelete = $this->project->environment_variables()->whereNotIn('key', array_keys($variables))->get();++ if ($variablesToDelete->isEmpty()) {+ return 0;+ }++ $this->project->environment_variables()->whereNotIn('key', array_keys($variables))->delete();++ return $variablesToDelete->count();+ }++ private function updateOrCreateVariables($variables)+ {+ $count = 0;+ foreach ($variables as $key => $value) {+ $found = $this->project->environment_variables()->where('key', $key)->first();++ if ($found) {+ if (! $found->is_shown_once && ! $found->is_multiline) {+ if ($found->value !== $value) {+ $found->value = $value;+ $found->save();+ $count++;+ }+ }+ } else {+ $this->project->environment_variables()->create([+ 'key' => $key,+ 'value' => $value,+ 'is_multiline' => false,+ 'is_literal' => false,+ 'type' => 'project',+ 'team_id' => currentTeam()->id,+ ]);+ $count++;+ }+ }++ return $count;+ }++ public function refreshEnvs()+ {+ $this->project->refresh();+ $this->getDevView();}public function render()diff --git a/app/Livewire/SharedVariables/Team/Index.php b/app/Livewire/SharedVariables/Team/Index.phpindex 82473528cc..6311d9a87f 100644--- a/app/Livewire/SharedVariables/Team/Index.php+++ b/app/Livewire/SharedVariables/Team/Index.php@@ -12,7 +12,11 @@ class Index extends Componentpublic Team $team;- protected $listeners = ['refreshEnvs' => '$refresh', 'saveKey' => 'saveKey', 'environmentVariableDeleted' => '$refresh'];+ public string $view = 'normal';++ public ?string $variables = null;++ protected $listeners = ['refreshEnvs' => 'refreshEnvs', 'saveKey' => 'saveKey', 'environmentVariableDeleted' => 'refreshEnvs'];public function saveKey($data){@@ -32,6 +36,7 @@ class Index extends Component'team_id' => currentTeam()->id,]);$this->team->refresh();+ $this->getDevView();} catch (\Throwable $e) {return handleError($e, $this);}@@ -40,6 +45,116 @@ class Index extends Componentpublic function mount(){$this->team = currentTeam();+ $this->getDevView();+ }++ public function switch()+ {+ $this->view = $this->view === 'normal' ? 'dev' : 'normal';+ $this->getDevView();+ }++ public function getDevView()+ {+ $this->variables = $this->formatEnvironmentVariables($this->team->environment_variables->sortBy('key'));+ }++ private function formatEnvironmentVariables($variables)+ {+ return $variables->map(function ($item) {+ if ($item->is_shown_once) {+ return "$item->key=(Locked Secret, delete and add again to change)";+ }+ if ($item->is_multiline) {+ return "$item->key=(Multiline environment variable, edit in normal view)";+ }++ return "$item->key=$item->value";+ })->join("\n");+ }++ public function submit()+ {+ try {+ $this->authorize('update', $this->team);+ $this->handleBulkSubmit();+ $this->getDevView();+ } catch (\Throwable $e) {+ return handleError($e, $this);+ } finally {+ $this->refreshEnvs();+ }+ }++ private function handleBulkSubmit()+ {+ $variables = parseEnvFormatToArray($this->variables);+ $changesMade = false;++ // Delete removed variables+ $deletedCount = $this->deleteRemovedVariables($variables);+ if ($deletedCount > 0) {+ $changesMade = true;+ }++ // Update or create variables+ $updatedCount = $this->updateOrCreateVariables($variables);+ if ($updatedCount > 0) {+ $changesMade = true;+ }++ if ($changesMade) {+ $this->dispatch('success', 'Environment variables updated.');+ }+ }++ private function deleteRemovedVariables($variables)+ {+ $variablesToDelete = $this->team->environment_variables()->whereNotIn('key', array_keys($variables))->get();++ if ($variablesToDelete->isEmpty()) {+ return 0;+ }++ $this->team->environment_variables()->whereNotIn('key', array_keys($variables))->delete();++ return $variablesToDelete->count();+ }++ private function updateOrCreateVariables($variables)+ {+ $count = 0;+ foreach ($variables as $key => $value) {+ $found = $this->team->environment_variables()->where('key', $key)->first();++ if ($found) {+ if (! $found->is_shown_once && ! $found->is_multiline) {+ if ($found->value !== $value) {+ $found->value = $value;+ $found->save();+ $count++;+ }+ }+ } else {+ $this->team->environment_variables()->create([+ 'key' => $key,+ 'value' => $value,+ 'is_multiline' => false,+ 'is_literal' => false,+ 'type' => 'team',+ 'team_id' => currentTeam()->id,+ ]);+ $count++;+ }+ }++ return $count;+ }++ public function refreshEnvs()+ {+ $this->team->refresh();+ $this->getDevView();}public function render()diff --git a/resources/views/livewire/shared-variables/environment/show.blade.php b/resources/views/livewire/shared-variables/environment/show.blade.phpindex 0799a74222..41c8249045 100644--- a/resources/views/livewire/shared-variables/environment/show.blade.php+++ b/resources/views/livewire/shared-variables/environment/show.blade.php@@ -9,17 +9,33 @@<livewire:project.shared.environment-variable.add :shared="true" /></x-modal-input>@endcan+ @can('update', $environment)+ <x-forms.button wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>+ @endcan</div><div class="flex items-center gap-1 subtitle">You can use these variables anywhere with <spanclass="dark:text-warning text-coollabs">@{{ environment.VARIABLENAME }}</span><x-helperhelper="More info <a class='underline dark:text-white' href='https://coolify.io/docs/knowledge-base/environment-variables#shared-variables' target='_blank'>here</a>."></x-helper></div>- <div class="flex flex-col gap-2">- @forelse ($environment->environment_variables->sort()->sortBy('key') as $env)- <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"- :env="$env" type="environment" />- @empty- <div>No environment variables found.</div>- @endforelse- </div>+ @if ($view === 'normal')+ <div class="flex flex-col gap-2">+ @forelse ($environment->environment_variables->sort()->sortBy('key') as $env)+ <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"+ :env="$env" type="environment" />+ @empty+ <div>No environment variables found.</div>+ @endforelse+ </div>+ @else+ <form wire:submit='submit' class="flex flex-col gap-2">+ @can('update', $environment)+ <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables" wire:model="variables"+ label="Environment Shared Variables"></x-forms.textarea>+ <x-forms.button type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>+ @else+ <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables" wire:model="variables"+ label="Environment Shared Variables" disabled></x-forms.textarea>+ @endcan+ </form>+ @endif</div>diff --git a/resources/views/livewire/shared-variables/project/show.blade.php b/resources/views/livewire/shared-variables/project/show.blade.phpindex 7db3b61a27..cb99824a0c 100644--- a/resources/views/livewire/shared-variables/project/show.blade.php+++ b/resources/views/livewire/shared-variables/project/show.blade.php@@ -9,6 +9,9 @@<livewire:project.shared.environment-variable.add :shared="true" /></x-modal-input>@endcan+ @can('update', $project)+ <x-forms.button wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>+ @endcan</div><div class="flex flex-wrap gap-1 subtitle"><div>You can use these variables anywhere with</div>@@ -16,12 +19,25 @@<x-helperhelper="More info <a class='underline dark:text-white' href='https://coolify.io/docs/knowledge-base/environment-variables#shared-variables' target='_blank'>here</a>."></x-helper></div>- <div class="flex flex-col gap-2">- @forelse ($project->environment_variables->sort()->sortBy('key') as $env)- <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"- :env="$env" type="project" />- @empty- <div>No environment variables found.</div>- @endforelse- </div>+ @if ($view === 'normal')+ <div class="flex flex-col gap-2">+ @forelse ($project->environment_variables->sort()->sortBy('key') as $env)+ <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"+ :env="$env" type="project" />+ @empty+ <div>No environment variables found.</div>+ @endforelse+ </div>+ @else+ <form wire:submit='submit' class="flex flex-col gap-2">+ @can('update', $project)+ <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables" wire:model="variables"+ label="Project Shared Variables"></x-forms.textarea>+ <x-forms.button type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>+ @else+ <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables" wire:model="variables"+ label="Project Shared Variables" disabled></x-forms.textarea>+ @endcan+ </form>+ @endif</div>diff --git a/resources/views/livewire/shared-variables/team/index.blade.php b/resources/views/livewire/shared-variables/team/index.blade.phpindex 1fbdfc2c5a..b043bcc0ee 100644--- a/resources/views/livewire/shared-variables/team/index.blade.php+++ b/resources/views/livewire/shared-variables/team/index.blade.php@@ -9,18 +9,34 @@<livewire:project.shared.environment-variable.add :shared="true" /></x-modal-input>@endcan+ @can('update', $team)+ <x-forms.button wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>+ @endcan</div><div class="flex items-center gap-1 subtitle">You can use these variables anywhere with <spanclass="dark:text-warning text-coollabs">@{{ team.VARIABLENAME }}</span> <x-helperhelper="More info <a class='underline dark:text-white' href='https://coolify.io/docs/knowledge-base/environment-variables#shared-variables' target='_blank'>here</a>."></x-helper></div>- <div class="flex flex-col gap-2">- @forelse ($team->environment_variables->sort()->sortBy('key') as $env)- <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"- :env="$env" type="team" />- @empty- <div>No environment variables found.</div>- @endforelse- </div>+ @if ($view === 'normal')+ <div class="flex flex-col gap-2">+ @forelse ($team->environment_variables->sort()->sortBy('key') as $env)+ <livewire:project.shared.environment-variable.show wire:key="environment-{{ $env->id }}"+ :env="$env" type="team" />+ @empty+ <div>No environment variables found.</div>+ @endforelse+ </div>+ @else+ <form wire:submit='submit' class="flex flex-col gap-2">+ @can('update', $team)+ <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables" wire:model="variables"+ label="Team Shared Variables"></x-forms.textarea>+ <x-forms.button type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>+ @else+ <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables" wire:model="variables"+ label="Team Shared Variables" disabled></x-forms.textarea>+ @endcan+ </form>+ @endif</div>