Skip to content

Commit

Permalink
feat: literal env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Apr 15, 2024
1 parent cbd2580 commit 5b36f07
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 6 deletions.
13 changes: 12 additions & 1 deletion app/Jobs/ApplicationDeploymentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,6 @@ private function generate_compose_file()
$persistent_storages = $this->generate_local_persistent_volumes();
$volume_names = $this->generate_local_persistent_volumes_only_volume_names();
$environment_variables = $this->generate_environment_variables($ports);

if (data_get($this->application, 'custom_labels')) {
$this->application->parseContainerLabels();
$labels = collect(preg_split("/\r\n|\n|\r/", base64_decode($this->application->custom_labels)));
Expand Down Expand Up @@ -1419,6 +1418,9 @@ private function generate_environment_variables($ports)
} else {
$real_value = escapeEnvVariables($env->real_value);
}
if ($env->is_literal) {
$real_value = escapeDollarSign($real_value);
}
$environment_variables->push("$env->key=$real_value");
}
foreach ($this->application->nixpacks_environment_variables as $env) {
Expand All @@ -1427,6 +1429,9 @@ private function generate_environment_variables($ports)
} else {
$real_value = escapeEnvVariables($env->real_value);
}
if ($env->is_literal) {
$real_value = escapeDollarSign($real_value);
}
$environment_variables->push("$env->key=$real_value");
}
} else {
Expand All @@ -1436,6 +1441,9 @@ private function generate_environment_variables($ports)
} else {
$real_value = escapeEnvVariables($env->real_value);
}
if ($env->is_literal) {
$real_value = escapeDollarSign($real_value);
}
$environment_variables->push("$env->key=$real_value");
}
foreach ($this->application->nixpacks_environment_variables_preview as $env) {
Expand All @@ -1444,6 +1452,9 @@ private function generate_environment_variables($ports)
} else {
$real_value = escapeEnvVariables($env->real_value);
}
if ($env->is_literal) {
$real_value = escapeDollarSign($real_value);
}
$environment_variables->push("$env->key=$real_value");
}
}
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Project/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function saveKey($data)
'key' => $data['key'],
'value' => $data['value'],
'is_multiline' => $data['is_multiline'],
'is_literal' => $data['is_literal'],
'type' => 'project',
'team_id' => currentTeam()->id,
]);
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/Project/EnvironmentEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function saveKey($data)
'key' => $data['key'],
'value' => $data['value'],
'is_multiline' => $data['is_multiline'],
'is_literal' => $data['is_literal'],
'type' => 'environment',
'team_id' => currentTeam()->id,
]);
Expand Down
6 changes: 6 additions & 0 deletions app/Livewire/Project/Shared/EnvironmentVariable/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
class Add extends Component
{
public $parameters;
public bool $shared = false;
public bool $is_preview = false;
public string $key;
public ?string $value = null;
public bool $is_build_time = false;
public bool $is_multiline = false;
public bool $is_literal = false;

protected $listeners = ['clearAddEnv' => 'clear'];
protected $rules = [
'key' => 'required|string',
'value' => 'nullable',
'is_build_time' => 'required|boolean',
'is_multiline' => 'required|boolean',
'is_literal' => 'required|boolean',
];
protected $validationAttributes = [
'key' => 'key',
'value' => 'value',
'is_build_time' => 'build',
'is_multiline' => 'multiline',
'is_literal' => 'literal',
];

public function mount()
Expand All @@ -47,6 +51,7 @@ public function submit()
'value' => $this->value,
'is_build_time' => $this->is_build_time,
'is_multiline' => $this->is_multiline,
'is_literal' => $this->is_literal,
'is_preview' => $this->is_preview,
]);
$this->clear();
Expand All @@ -58,5 +63,6 @@ public function clear()
$this->value = '';
$this->is_build_time = false;
$this->is_multiline = false;
$this->is_literal = false;
}
}
3 changes: 3 additions & 0 deletions app/Livewire/Project/Shared/EnvironmentVariable/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Show extends Component
'env.value' => 'nullable',
'env.is_build_time' => 'required|boolean',
'env.is_multiline' => 'required|boolean',
'env.is_literal' => 'required|boolean',
'env.is_shown_once' => 'required|boolean',
'env.real_value' => 'nullable',
];
Expand All @@ -30,6 +31,7 @@ class Show extends Component
'env.value' => 'Value',
'env.is_build_time' => 'Build Time',
'env.is_multiline' => 'Multiline',
'env.is_literal' => 'Literal',
'env.is_shown_once' => 'Shown Once',
];

Expand All @@ -41,6 +43,7 @@ public function mount()
$this->modalId = new Cuid2(7);
$this->parameters = get_route_parameters();
$this->checkEnvs();
ray($this->env);
}
public function checkEnvs()
{
Expand Down
1 change: 1 addition & 0 deletions app/Livewire/TeamSharedVariablesIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function saveKey($data)
'key' => $data['key'],
'value' => $data['value'],
'is_multiline' => $data['is_multiline'],
'is_literal' => $data['is_literal'],
'type' => 'team',
'team_id' => currentTeam()->id,
]);
Expand Down
1 change: 1 addition & 0 deletions app/Models/SharedEnvironmentVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

class SharedEnvironmentVariable extends Model
Expand Down
6 changes: 6 additions & 0 deletions bootstrap/helpers/docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,9 @@ function escapeEnvVariables($value)
$replace = array("\\\\", "\\r", "\\t", "\\0", '\"', "\'");
return str_replace($search, $replace, $value);
}
function escapeDollarSign($value)
{
$search = array('$');
$replace = array('$$');
return str_replace($search, $replace, $value);
}
34 changes: 34 additions & 0 deletions database/migrations/2024_04_15_094703_add_literal_variables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('environment_variables', function (Blueprint $table) {
$table->boolean('is_literal')->default(false);
});
Schema::table('shared_environment_variables', function (Blueprint $table) {
$table->boolean('is_literal')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('environment_variables', function (Blueprint $table) {
$table->dropColumn('is_literal');
});
Schema::table('shared_environment_variables', function (Blueprint $table) {
$table->dropColumn('is_literal');
});
}
};
2 changes: 1 addition & 1 deletion resources/views/livewire/project/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div class="flex gap-2">
<h2>Shared Variables</h2>
<x-modal-input buttonTitle="+ Add" title="New Shared Variable">
<livewire:project.shared.environment-variable.add />
<livewire:project.shared.environment-variable.add :shared="true" />
</x-modal-input>
</div>
<div class="pb-4 lg:flex lg:gap-1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<div class="flex gap-2 pt-10">
<h2>Shared Variables</h2>
<x-modal-input buttonTitle="+ Add" title="New Shared Variable">
<livewire:project.shared.environment-variable.add />
<livewire:project.shared.environment-variable.add :shared="true" />
</x-modal-input>
</div>
<div class="flex items-center gap-2 pb-4">You can use these variables anywhere with <span class="dark:text-warning text-coollabs">@{{environment.VARIABLENAME}}</span><x-helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<x-forms.checkbox id="is_build_time" label="Build Variable?" />
@endif
<x-forms.checkbox id="is_multiline" label="Is Multiline?" />
@if (!$shared)
<x-forms.checkbox id="is_literal"
helper="This means that when you use $VARIABLES, it should be interpreted as the actual characters '$VARIABLES' and not as the value of a variable named VARIABLE.<br><br>Useful if you have $ sign in your value and there are some characters after it, but you would not like to interpolate it form another value. In this case, you should set this to true."
label="Is Literal?" />
@endif
<x-forms.button type="submit" @click="slideOverOpen=false">
Save
</x-forms.button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ class="font-bold dark:text-warning text-coollabs">{{ $env->key }}</span>.
@else
@if ($env->is_shared)
<x-forms.checkbox instantSave id="env.is_build_time" label="Build Variable?" />
<x-forms.checkbox instantSave id="env.is_literal"
helper="This means that when you use $VARIABLES, it should be interpreted as the actual characters '$VARIABLES' and not as the value of a variable named VARIABLE.<br><br>Useful if you have $ sign in your value and there are some characters after it, but you would not like to interpolate it form another value. In this case, you should set this to true."
label="Is Literal?" />
@else
<x-forms.checkbox instantSave id="env.is_multiline" label="Is Multiline?" />
<x-forms.checkbox instantSave id="env.is_build_time" label="Build Variable?" />
@if ($type === 'team' || $type === 'environment' || $type === 'project')
<x-forms.checkbox instantSave id="env.is_multiline" label="Is Multiline?" />
@else
<x-forms.checkbox instantSave id="env.is_build_time" label="Build Variable?" />
<x-forms.checkbox instantSave id="env.is_multiline" label="Is Multiline?" />
<x-forms.checkbox instantSave id="env.is_literal"
helper="This means that when you use $VARIABLES, it should be interpreted as the actual characters '$VARIABLES' and not as the value of a variable named VARIABLE.<br><br>Useful if you have $ sign in your value and there are some characters after it, but you would not like to interpolate it form another value. In this case, you should set this to true."
label="Is Literal?" />
@endif
@endif
@endif
<div class="flex-1"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="flex gap-2">
<h2>Shared Variables</h2>
<x-modal-input buttonTitle="+ Add" title="New Shared Variable">
<livewire:project.shared.environment-variable.add />
<livewire:project.shared.environment-variable.add :shared="true" />
</x-modal-input>
</div>
<div class="flex items-center gap-2 pb-4">You can use these variables anywhere with <span
Expand Down

0 comments on commit 5b36f07

Please sign in to comment.