Skip to content

Commit

Permalink
Merge branch 'main' into issue/353
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 committed Aug 20, 2024
2 parents 5237c24 + c2b1a98 commit 36de986
Show file tree
Hide file tree
Showing 30 changed files with 531 additions and 134 deletions.
1 change: 1 addition & 0 deletions app/Filament/Pages/Installer/PanelInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function submit()
Artisan::call('migrate', [
'--force' => true,
'--seed' => true,
'--database' => $variables['DB_CONNECTION'],
]);

if (!$this->hasCompletedMigrations()) {
Expand Down
19 changes: 9 additions & 10 deletions app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,28 +262,27 @@ private function mailSettings(): array
->visible(fn (Get $get) => $get('MAIL_MAILER') === 'smtp')
->schema([
TextInput::make('MAIL_HOST')
->label('SMTP Host')
->label('Host')
->required()
->default(env('MAIL_HOST', config('mail.mailers.smtp.host'))),
TextInput::make('MAIL_PORT')
->label('SMTP Port')
->label('Port')
->required()
->numeric()
->minValue(1)
->maxValue(65535)
->default(env('MAIL_PORT', config('mail.mailers.smtp.port'))),
TextInput::make('MAIL_USERNAME')
->label('SMTP Username')
->label('Username')
->required()
->default(env('MAIL_USERNAME', config('mail.mailers.smtp.username'))),
TextInput::make('MAIL_PASSWORD')
->label('SMTP Password')
->label('Password')
->password()
->revealable()
->default(env('MAIL_PASSWORD')),
ToggleButtons::make('MAIL_ENCRYPTION')
->label('SMTP encryption')
->required()
->label('Encryption')
->inline()
->options(['tls' => 'TLS', 'ssl' => 'SSL', '' => 'None'])
->default(env('MAIL_ENCRYPTION', config('mail.mailers.smtp.encryption', 'tls'))),
Expand All @@ -293,15 +292,15 @@ private function mailSettings(): array
->visible(fn (Get $get) => $get('MAIL_MAILER') === 'mailgun')
->schema([
TextInput::make('MAILGUN_DOMAIN')
->label('Mailgun Domain')
->label('Domain')
->required()
->default(env('MAILGUN_DOMAIN', config('services.mailgun.domain'))),
TextInput::make('MAILGUN_SECRET')
->label('Mailgun Secret')
->label('Secret')
->required()
->default(env('MAIL_USERNAME', config('services.mailgun.secret'))),
->default(env('MAILGUN_SECRET', config('services.mailgun.secret'))),
TextInput::make('MAILGUN_ENDPOINT')
->label('Mailgun Endpoint')
->label('Endpoint')
->required()
->default(env('MAILGUN_ENDPOINT', config('services.mailgun.endpoint'))),
]),
Expand Down
31 changes: 27 additions & 4 deletions app/Filament/Resources/EggResource/Pages/CreateEgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function form(Form $form): Form
->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
$data['default_value'] ??= '';
$data['description'] ??= '';
$data['rules'] ??= '';
$data['rules'] ??= [];
$data['user_viewable'] ??= '';
$data['user_editable'] ??= '';

Expand All @@ -143,7 +143,7 @@ public function form(Form $form): Form
->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
$data['default_value'] ??= '';
$data['description'] ??= '';
$data['rules'] ??= '';
$data['rules'] ??= [];
$data['user_viewable'] ??= '';
$data['user_editable'] ??= '';

Expand Down Expand Up @@ -173,7 +173,30 @@ public function form(Form $form): Form
Checkbox::make('user_viewable')->label('Viewable'),
Checkbox::make('user_editable')->label('Editable'),
]),
Textarea::make('rules')->columnSpanFull(),
TagsInput::make('rules')
->columnSpanFull()
->placeholder('Add Rule')
->reorderable()
->suggestions([
'required',
'nullable',
'string',
'integer',
'numeric',
'boolean',
'alpha',
'alpha_dash',
'alpha_num',
'url',
'email',
'regex:',
'min:',
'max:',
'between:',
'between:1024,65535',
'in:',
'in:true,false',
]),
]),
]),
Tab::make('Install Script')
Expand All @@ -187,7 +210,7 @@ public function form(Form $form): Form
TextInput::make('script_container')
->required()
->maxLength(255)
->default('alpine:3.4'),
->default('ghcr.io/pelican-eggs/installers:debian'),

Select::make('script_entry')
->selectablePlaceholder(false)
Expand Down
35 changes: 28 additions & 7 deletions app/Filament/Resources/EggResource/Pages/EditEgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function form(Form $form): Form
->mutateRelationshipDataBeforeCreateUsing(function (array $data): array {
$data['default_value'] ??= '';
$data['description'] ??= '';
$data['rules'] ??= '';
$data['rules'] ??= [];
$data['user_viewable'] ??= '';
$data['user_editable'] ??= '';

Expand All @@ -153,7 +153,7 @@ public function form(Form $form): Form
->mutateRelationshipDataBeforeSaveUsing(function (array $data): array {
$data['default_value'] ??= '';
$data['description'] ??= '';
$data['rules'] ??= '';
$data['rules'] ??= [];
$data['user_viewable'] ??= '';
$data['user_editable'] ??= '';

Expand Down Expand Up @@ -183,7 +183,30 @@ public function form(Form $form): Form
Checkbox::make('user_viewable')->label('Viewable'),
Checkbox::make('user_editable')->label('Editable'),
]),
TextInput::make('rules')->columnSpanFull(),
TagsInput::make('rules')
->columnSpanFull()
->placeholder('Add Rule')
->reorderable()
->suggestions([
'required',
'nullable',
'string',
'integer',
'numeric',
'boolean',
'alpha',
'alpha_dash',
'alpha_num',
'url',
'email',
'regex:',
'min:',
'max:',
'between:',
'between:1024,65535',
'in:',
'in:true,false',
]),
]),
]),
Tab::make('Install Script')
Expand Down Expand Up @@ -270,16 +293,14 @@ protected function getHeaderActions(): array
Notification::make()
->title('Import Failed')
->body($exception->getMessage())
->danger()
->danger() // Will Robinson
->send();

report($exception);

return;
}
}

if (!empty($data['url'])) {
} elseif (!empty($data['url'])) {
try {
$eggImportService->fromUrl($data['url'], $egg);
} catch (Exception $exception) {
Expand Down
16 changes: 6 additions & 10 deletions app/Filament/Resources/ServerResource/Pages/CreateServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Allocation;
use App\Models\Egg;
use App\Models\Node;
use App\Models\ServerVariable;
use App\Models\User;
use App\Services\Allocations\AssignmentService;
use App\Services\Servers\RandomWordService;
Expand Down Expand Up @@ -443,8 +444,7 @@ public function form(Form $form): Form

$text = Forms\Components\TextInput::make('variable_value')
->hidden($this->shouldHideComponent(...))
->maxLength(255)
->required(fn (Forms\Get $get) => in_array('required', explode('|', $get('rules'))))
->required(fn (ServerVariable $serverVariable) => $serverVariable->variable->getRequiredAttribute())
->rules(
fn (Forms\Get $get): Closure => function (string $attribute, $value, Closure $fail) use ($get) {
$validator = Validator::make(['validatorkey' => $value], [
Expand Down Expand Up @@ -806,11 +806,9 @@ protected function handleRecordCreation(array $data): Model
return $service->handle($data);
}

private function shouldHideComponent(Forms\Get $get, Forms\Components\Component $component): bool
private function shouldHideComponent(ServerVariable $serverVariable, Forms\Components\Component $component): bool
{
$containsRuleIn = str($get('rules'))->explode('|')->reduce(
fn ($result, $value) => $result === true && !str($value)->startsWith('in:'), true
);
$containsRuleIn = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'), false);

if ($component instanceof Forms\Components\Select) {
return $containsRuleIn;
Expand All @@ -823,11 +821,9 @@ private function shouldHideComponent(Forms\Get $get, Forms\Components\Component
throw new \Exception('Component type not supported: ' . $component::class);
}

private function getSelectOptionsFromRules(Forms\Get $get): array
private function getSelectOptionsFromRules(ServerVariable $serverVariable): array
{
$inRule = str($get('rules'))->explode('|')->reduce(
fn ($result, $value) => str($value)->startsWith('in:') ? $value : $result, ''
);
$inRule = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'));

return str($inRule)
->after('in:')
Expand Down
20 changes: 8 additions & 12 deletions app/Filament/Resources/ServerResource/Pages/EditServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public function form(Form $form): Form

$text = Forms\Components\TextInput::make('variable_value')
->hidden($this->shouldHideComponent(...))
->required(fn (ServerVariable $serverVariable) => in_array('required', explode('|', $serverVariable->variable->rules)))
->required(fn (ServerVariable $serverVariable) => $serverVariable->variable->getRequiredAttribute())
->rules([
fn (ServerVariable $serverVariable): Closure => function (string $attribute, $value, Closure $fail) use ($serverVariable) {
$validator = Validator::make(['validatorkey' => $value], [
Expand All @@ -512,7 +512,7 @@ public function form(Form $form): Form
->live(onBlur: true)
->hintIcon('tabler-code')
->label(fn (ServerVariable $serverVariable) => $serverVariable->variable->name)
->hintIconTooltip(fn (ServerVariable $serverVariable) => $serverVariable->variable->rules)
->hintIconTooltip(fn (ServerVariable $serverVariable) => implode('|', $serverVariable->variable->rules))
->prefix(fn (ServerVariable $serverVariable) => '{{' . $serverVariable->variable->env_variable . '}}')
->helperText(fn (ServerVariable $serverVariable) => empty($serverVariable->variable->description) ? '' : $serverVariable->variable->description);
}
Expand Down Expand Up @@ -753,28 +753,24 @@ public function getRelationManagers(): array
];
}

private function shouldHideComponent(Forms\Get $get, Forms\Components\Component $component): bool
private function shouldHideComponent(ServerVariable $serverVariable, Forms\Components\Component $component): bool
{
$containsRuleIn = str($get('rules'))->explode('|')->reduce(
fn ($result, $value) => $result === true && !str($value)->startsWith('in:'), true
);
$containsRuleIn = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'), false);

if ($component instanceof Forms\Components\Select) {
return $containsRuleIn;
return !$containsRuleIn;
}

if ($component instanceof Forms\Components\TextInput) {
return !$containsRuleIn;
return $containsRuleIn;
}

throw new \Exception('Component type not supported: ' . $component::class);
}

private function getSelectOptionsFromRules(Forms\Get $get): array
private function getSelectOptionsFromRules(ServerVariable $serverVariable): array
{
$inRule = str($get('rules'))->explode('|')->reduce(
fn ($result, $value) => str($value)->startsWith('in:') ? $value : $result, ''
);
$inRule = array_first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'));

return str($inRule)
->after('in:')
Expand Down
9 changes: 6 additions & 3 deletions app/Models/EggVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @property string $default_value
* @property bool $user_viewable
* @property bool $user_editable
* @property string $rules
* @property array $rules
* @property \Carbon\CarbonImmutable $created_at
* @property \Carbon\CarbonImmutable $updated_at
* @property bool $required
Expand Down Expand Up @@ -58,12 +58,14 @@ class EggVariable extends Model
'default_value' => 'string',
'user_viewable' => 'boolean',
'user_editable' => 'boolean',
'rules' => 'string',
'rules' => 'array',
'rules.*' => 'string',
];

protected $attributes = [
'user_editable' => 0,
'user_viewable' => 0,
'rules' => '[]',
];

protected function casts(): array
Expand All @@ -72,14 +74,15 @@ protected function casts(): array
'egg_id' => 'integer',
'user_viewable' => 'bool',
'user_editable' => 'bool',
'rules' => 'array',
'created_at' => 'immutable_datetime',
'updated_at' => 'immutable_datetime',
];
}

public function getRequiredAttribute(): bool
{
return in_array('required', explode('|', $this->rules));
return in_array('required', $this->rules);
}

public function egg(): HasOne
Expand Down
2 changes: 2 additions & 0 deletions app/Services/Eggs/Sharing/EggImporterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function fromFile(UploadedFile $file, Egg $egg = null): Egg
// Update existing variables or create new ones.
foreach ($parsed['variables'] ?? [] as $variable) {
EggVariable::unguarded(function () use ($egg, $variable) {
$variable['rules'] = is_array($variable['rules']) ? $variable['rules'] : explode('|', $variable['rules']);

$egg->variables()->updateOrCreate([
'env_variable' => $variable['env_variable'],
], Collection::make($variable)->except(['egg_id', 'env_variable'])->toArray());
Expand Down
4 changes: 2 additions & 2 deletions app/Services/Eggs/Variables/VariableCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle(int $egg, array $data): EggVariable
throw new ReservedVariableNameException(sprintf('Cannot use the protected name %s for this environment variable.', array_get($data, 'env_variable')));
}

if (!empty($data['rules'] ?? '')) {
if (!empty($data['rules'] ?? [])) {
$this->validateRules($data['rules']);
}

Expand All @@ -55,7 +55,7 @@ public function handle(int $egg, array $data): EggVariable
'default_value' => $data['default_value'] ?? '',
'user_viewable' => in_array('user_viewable', $options),
'user_editable' => in_array('user_editable', $options),
'rules' => $data['rules'] ?? '',
'rules' => $data['rules'] ?? [],
]);

return $eggVariable;
Expand Down
11 changes: 3 additions & 8 deletions app/Services/Eggs/Variables/VariableUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Services\Eggs\Variables;

use Illuminate\Support\Str;
use App\Models\EggVariable;
use App\Exceptions\DisplayException;
use App\Traits\Services\ValidatesValidationRules;
Expand Down Expand Up @@ -54,12 +53,8 @@ public function handle(EggVariable $variable, array $data): mixed
}
}

if (!empty($data['rules'] ?? '')) {
$this->validateRules(
(is_string($data['rules']) && Str::contains($data['rules'], ';;'))
? explode(';;', $data['rules'])
: $data['rules']
);
if (!empty($data['rules'] ?? [])) {
$this->validateRules($data['rules']);
}

$options = array_get($data, 'options') ?? [];
Expand All @@ -71,7 +66,7 @@ public function handle(EggVariable $variable, array $data): mixed
'default_value' => $data['default_value'] ?? '',
'user_viewable' => in_array('user_viewable', $options),
'user_editable' => in_array('user_editable', $options),
'rules' => $data['rules'] ?? '',
'rules' => $data['rules'] ?? [],
]);
}
}
Loading

0 comments on commit 36de986

Please sign in to comment.