2023-08-07 20:14:21 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Shared\EnvironmentVariable;
|
2023-08-07 20:14:21 +00:00
|
|
|
|
|
|
|
use App\Models\EnvironmentVariable;
|
|
|
|
use Livewire\Component;
|
|
|
|
use Visus\Cuid2\Cuid2;
|
|
|
|
|
|
|
|
class All extends Component
|
|
|
|
{
|
|
|
|
public $resource;
|
2024-05-17 09:10:57 +00:00
|
|
|
public string $resourceClass;
|
2023-09-08 14:16:59 +00:00
|
|
|
public bool $showPreview = false;
|
2024-03-15 21:02:37 +00:00
|
|
|
public ?string $modalId = null;
|
2023-09-08 14:16:59 +00:00
|
|
|
public ?string $variables = null;
|
|
|
|
public ?string $variablesPreview = null;
|
|
|
|
public string $view = 'normal';
|
2024-04-15 12:23:25 +00:00
|
|
|
protected $listeners = [
|
|
|
|
'refreshEnvs',
|
|
|
|
'saveKey' => 'submit',
|
|
|
|
];
|
2024-05-17 09:10:57 +00:00
|
|
|
protected $rules = [
|
|
|
|
'resource.settings.is_env_sorting_enabled' => 'required|boolean',
|
|
|
|
];
|
|
|
|
|
2023-08-07 20:14:21 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2024-05-17 09:10:57 +00:00
|
|
|
$this->resourceClass = get_class($this->resource);
|
2023-09-08 14:16:59 +00:00
|
|
|
$resourceWithPreviews = ['App\Models\Application'];
|
|
|
|
$simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile'));
|
2024-05-17 09:10:57 +00:00
|
|
|
if (str($this->resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->showPreview = true;
|
|
|
|
}
|
2023-08-07 20:14:21 +00:00
|
|
|
$this->modalId = new Cuid2(7);
|
2024-05-17 09:10:57 +00:00
|
|
|
$this->sortMe();
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->getDevView();
|
|
|
|
}
|
2024-05-17 09:10:57 +00:00
|
|
|
|
|
|
|
public function sortMe()
|
|
|
|
{
|
|
|
|
if ($this->resourceClass === 'App\Models\Application' && $this->resource->settings->is_env_sorting_enabled) {
|
|
|
|
$this->resource->environment_variables = $this->resource->environment_variables->sortBy('key');
|
|
|
|
$this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('key');
|
|
|
|
} else {
|
|
|
|
$this->resource->environment_variables = $this->resource->environment_variables->sortBy('id');
|
|
|
|
$this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('id');
|
|
|
|
}
|
|
|
|
$this->getDevView();
|
|
|
|
}
|
|
|
|
public function instantSave()
|
|
|
|
{
|
|
|
|
if ($this->resourceClass === 'App\Models\Application') {
|
|
|
|
$this->resource->settings->save();
|
|
|
|
$this->dispatch('success', 'Environment variable settings updated.');
|
|
|
|
$this->sortMe();
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
public function getDevView()
|
|
|
|
{
|
|
|
|
$this->variables = $this->resource->environment_variables->map(function ($item) {
|
2023-10-24 13:41:21 +00:00
|
|
|
if ($item->is_shown_once) {
|
|
|
|
return "$item->key=(locked secret)";
|
|
|
|
}
|
2024-03-15 21:02:37 +00:00
|
|
|
if ($item->is_multiline) {
|
|
|
|
return "$item->key=(multiline, edit in normal view)";
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
return "$item->key=$item->value";
|
2024-05-17 09:10:57 +00:00
|
|
|
})->join('
|
2023-09-08 14:16:59 +00:00
|
|
|
');
|
|
|
|
if ($this->showPreview) {
|
|
|
|
$this->variablesPreview = $this->resource->environment_variables_preview->map(function ($item) {
|
2023-10-24 13:41:21 +00:00
|
|
|
if ($item->is_shown_once) {
|
|
|
|
return "$item->key=(locked secret)";
|
|
|
|
}
|
2024-03-15 21:02:37 +00:00
|
|
|
if ($item->is_multiline) {
|
|
|
|
return "$item->key=(multiline, edit in normal view)";
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
return "$item->key=$item->value";
|
2024-05-17 09:10:57 +00:00
|
|
|
})->join('
|
2023-09-08 14:16:59 +00:00
|
|
|
');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function switch()
|
|
|
|
{
|
2024-05-17 09:10:57 +00:00
|
|
|
if ($this->view === 'normal') {
|
|
|
|
$this->view = 'dev';
|
|
|
|
} else {
|
|
|
|
$this->view = 'normal';
|
|
|
|
}
|
|
|
|
$this->sortMe();
|
2023-09-08 14:16:59 +00:00
|
|
|
}
|
|
|
|
public function saveVariables($isPreview)
|
|
|
|
{
|
|
|
|
if ($isPreview) {
|
|
|
|
$variables = parseEnvFormatToArray($this->variablesPreview);
|
2023-11-09 11:40:53 +00:00
|
|
|
$this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete();
|
2023-09-08 14:16:59 +00:00
|
|
|
} else {
|
|
|
|
$variables = parseEnvFormatToArray($this->variables);
|
2024-05-17 09:10:57 +00:00
|
|
|
ray($variables, $this->variables);
|
2023-11-09 11:40:53 +00:00
|
|
|
$this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
|
2023-09-08 14:16:59 +00:00
|
|
|
}
|
|
|
|
foreach ($variables as $key => $variable) {
|
2023-11-09 11:40:53 +00:00
|
|
|
if ($isPreview) {
|
|
|
|
$found = $this->resource->environment_variables_preview()->where('key', $key)->first();
|
|
|
|
} else {
|
|
|
|
$found = $this->resource->environment_variables()->where('key', $key)->first();
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
if ($found) {
|
2024-03-15 21:02:37 +00:00
|
|
|
if ($found->is_shown_once || $found->is_multiline) {
|
2023-10-24 13:41:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
$found->value = $variable;
|
2024-01-31 12:40:15 +00:00
|
|
|
if (str($found->value)->startsWith('{{') && str($found->value)->endsWith('}}')) {
|
|
|
|
$type = str($found->value)->after("{{")->before(".")->value;
|
2024-01-31 12:43:23 +00:00
|
|
|
if (!collect(SHARED_VARIABLE_TYPES)->contains($type)) {
|
2024-01-31 12:40:15 +00:00
|
|
|
$this->dispatch('error', 'Invalid shared variable type.', "Valid types are: team, project, environment.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
$found->save();
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$environment = new EnvironmentVariable();
|
|
|
|
$environment->key = $key;
|
|
|
|
$environment->value = $variable;
|
2024-01-31 12:40:15 +00:00
|
|
|
if (str($environment->value)->startsWith('{{') && str($environment->value)->endsWith('}}')) {
|
|
|
|
$type = str($environment->value)->after("{{")->before(".")->value;
|
2024-01-31 12:43:23 +00:00
|
|
|
if (!collect(SHARED_VARIABLE_TYPES)->contains($type)) {
|
2024-01-31 12:40:15 +00:00
|
|
|
$this->dispatch('error', 'Invalid shared variable type.', "Valid types are: team, project, environment.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
$environment->is_build_time = false;
|
2024-03-26 12:36:06 +00:00
|
|
|
$environment->is_multiline = false;
|
2023-09-08 14:16:59 +00:00
|
|
|
$environment->is_preview = $isPreview ? true : false;
|
2023-09-22 09:23:49 +00:00
|
|
|
switch ($this->resource->type()) {
|
|
|
|
case 'application':
|
|
|
|
$environment->application_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-postgresql':
|
|
|
|
$environment->standalone_postgresql_id = $this->resource->id;
|
|
|
|
break;
|
2023-10-12 15:18:33 +00:00
|
|
|
case 'standalone-redis':
|
|
|
|
$environment->standalone_redis_id = $this->resource->id;
|
|
|
|
break;
|
2023-10-19 11:32:03 +00:00
|
|
|
case 'standalone-mongodb':
|
|
|
|
$environment->standalone_mongodb_id = $this->resource->id;
|
|
|
|
break;
|
2023-10-24 12:31:28 +00:00
|
|
|
case 'standalone-mysql':
|
|
|
|
$environment->standalone_mysql_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-mariadb':
|
|
|
|
$environment->standalone_mariadb_id = $this->resource->id;
|
|
|
|
break;
|
2024-04-10 13:00:46 +00:00
|
|
|
case 'standalone-keydb':
|
|
|
|
$environment->standalone_keydb_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-dragonfly':
|
|
|
|
$environment->standalone_dragonfly_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-clickhouse':
|
|
|
|
$environment->standalone_clickhouse_id = $this->resource->id;
|
|
|
|
break;
|
2023-09-22 09:23:49 +00:00
|
|
|
case 'service':
|
|
|
|
$environment->service_id = $this->resource->id;
|
|
|
|
break;
|
2023-09-08 14:16:59 +00:00
|
|
|
}
|
|
|
|
$environment->save();
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 20:29:47 +00:00
|
|
|
if ($isPreview) {
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Preview environment variables updated.');
|
2023-09-11 20:29:47 +00:00
|
|
|
} else {
|
2024-02-22 13:53:42 +00:00
|
|
|
$this->dispatch('success', 'Environment variables updated.');
|
2023-09-11 20:29:47 +00:00
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->refreshEnvs();
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
|
|
|
public function refreshEnvs()
|
|
|
|
{
|
|
|
|
$this->resource->refresh();
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->getDevView();
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-07 20:14:21 +00:00
|
|
|
public function submit($data)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$found = $this->resource->environment_variables()->where('key', $data['key'])->first();
|
|
|
|
if ($found) {
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('error', 'Environment variable already exists.');
|
2023-08-07 20:14:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
$environment = new EnvironmentVariable();
|
2023-08-07 20:14:21 +00:00
|
|
|
$environment->key = $data['key'];
|
|
|
|
$environment->value = $data['value'];
|
|
|
|
$environment->is_build_time = $data['is_build_time'];
|
2024-03-15 21:02:37 +00:00
|
|
|
$environment->is_multiline = $data['is_multiline'];
|
2024-04-17 08:49:34 +00:00
|
|
|
$environment->is_literal = $data['is_literal'];
|
2023-08-07 20:14:21 +00:00
|
|
|
$environment->is_preview = $data['is_preview'];
|
|
|
|
|
2023-09-26 12:45:52 +00:00
|
|
|
switch ($this->resource->type()) {
|
|
|
|
case 'application':
|
|
|
|
$environment->application_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-postgresql':
|
|
|
|
$environment->standalone_postgresql_id = $this->resource->id;
|
|
|
|
break;
|
2024-01-06 10:38:12 +00:00
|
|
|
case 'standalone-redis':
|
|
|
|
$environment->standalone_redis_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-mongodb':
|
|
|
|
$environment->standalone_mongodb_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-mysql':
|
|
|
|
$environment->standalone_mysql_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-mariadb':
|
|
|
|
$environment->standalone_mariadb_id = $this->resource->id;
|
|
|
|
break;
|
2024-04-10 13:00:46 +00:00
|
|
|
case 'standalone-keydb':
|
|
|
|
$environment->standalone_keydb_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-dragonfly':
|
|
|
|
$environment->standalone_dragonfly_id = $this->resource->id;
|
|
|
|
break;
|
|
|
|
case 'standalone-clickhouse':
|
|
|
|
$environment->standalone_clickhouse_id = $this->resource->id;
|
|
|
|
break;
|
2023-09-26 12:45:52 +00:00
|
|
|
case 'service':
|
|
|
|
$environment->service_id = $this->resource->id;
|
|
|
|
break;
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
|
|
|
$environment->save();
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->refreshEnvs();
|
2024-02-06 14:05:11 +00:00
|
|
|
$this->dispatch('success', 'Environment variable added.');
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|