Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,48 @@ $panel->plugin(CommandRunnerPlugin::make());

## Customizations

1. Command Validation
### 1. Authorization

You can restrict access to this plugin using the `authorize` method. This accepts a boolean or a closure that returns a boolean.

```php
CommandRunnerPlugin::make()->authorize(fn () => auth()->user()->can('view-command-runner'))
```

### 2. Navigation Group

You can change the navigation group using the `navigationGroup` method. This accepts a string, `UnitEnum`.

```php
CommandRunnerPlugin::make()->navigationGroup('System')
```

### 3. Navigation Label

You can customize the navigation label using the `navigationLabel` method.

```php
CommandRunnerPlugin::make()->navigationLabel('Command Runner')
```

### 4. Navigation Icon

You can change the navigation icon using the `navigationIcon` method. This accepts a Heroicon string or a `Heroicon` enum value.

```php
CommandRunnerPlugin::make()->navigationIcon('heroicon-o-exclamation-triangle')
```

### 5. Navigation Sort Order

You can change the navigation sort order using the `navigationSort` method.

```php
CommandRunnerPlugin::make()->navigationSort(10)
```


### 6. Command Validation

You can define custom validation logic using the validateCommand() method. This is useful for restricting which commands can be run:

Expand All @@ -81,7 +122,7 @@ $panel->plugin(
);
```

2. Delete Command History
### 7. Delete Command History

Control who can delete command history entries using a boolean or closure with `canDeleteCommandHistory()`:

Expand All @@ -91,7 +132,7 @@ $panel->plugin(
);
```

3. Purge Old Command History
### 8. Purge Old Command History

Schedule the following artisan command to purge command history entries daily:

Expand Down
78 changes: 78 additions & 0 deletions src/CommandRunnerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,106 @@

namespace BinaryBuilds\CommandRunner;

use BackedEnum;
use BinaryBuilds\CommandRunner\Resources\CommandRuns\CommandRunResource;
use Closure;
use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Concerns\EvaluatesClosures;
use Filament\Support\Icons\Heroicon;
use UnitEnum;

class CommandRunnerPlugin implements Plugin
{
use EvaluatesClosures;

private $validationRule = null;

private $deleteHistory = true;

private bool | Closure $authorized = true;

private string | BackedEnum | null $navigationIcon = HeroIcon::OutlinedCommandLine;

private string | UnitEnum | null $navigationGroup = 'Settings';

private string $navigationLabel = 'Command Runner';

private int | Closure $navigationSort = 9999;

public function getId(): string
{
return 'command-runner';
}

public function authorize(bool | Closure $callback): static
{
$this->authorized = $callback;

return $this;
}

public function isAuthorized(): bool
{
return (bool) $this->evaluate($this->authorized);
}

public function validateCommand(callable $commandRule): CommandRunnerPlugin
{
$this->validationRule = $commandRule;

return $this;
}

public function navigationLabel(string $navigationLabel): static
{
$this->navigationLabel = $navigationLabel;

return $this;
}

public function getNavigationLabel(): string
{
return $this->navigationLabel;
}

public function navigationGroup(string | UnitEnum | null $navigationGroup): static
{
$this->navigationGroup = $navigationGroup;

return $this;
}

public function getNavigationGroup(): string
{
return $this->navigationGroup;
}

public function navigationSort(int | Closure $sort): static
{
$this->navigationSort = $sort;

return $this;
}

public function getNavigationSort(): int
{
/** @var int */
return $this->evaluate($this->navigationSort);
}

public function navigationIcon(string | BackedEnum | null $navigationIcon): static
{
$this->navigationIcon = $navigationIcon;

return $this;
}

public function getNavigationIcon(): string | Heroicon | null
{
return $this->navigationIcon;
}

public function canDeleteCommandHistory(callable | bool $deleteHistory): CommandRunnerPlugin
{
$this->deleteHistory = $deleteHistory;
Expand Down
32 changes: 29 additions & 3 deletions src/Resources/CommandRuns/CommandRunResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace BinaryBuilds\CommandRunner\Resources\CommandRuns;

use BackedEnum;
use BinaryBuilds\CommandRunner\CommandRunnerPlugin;
use BinaryBuilds\CommandRunner\Models\CommandRun;
use BinaryBuilds\CommandRunner\Resources\CommandRuns\Pages\ListCommandRuns;
use BinaryBuilds\CommandRunner\Resources\CommandRuns\Pages\RunCommand;
Expand All @@ -13,14 +15,40 @@
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use UnitEnum;

class CommandRunResource extends Resource
{
protected static ?string $slug = 'command-runner';

protected static function getPlugin(): CommandRunnerPlugin
{
return CommandRunnerPlugin::get();
}

public static function getNavigationLabel(): string
{
return __('Command Runner');
return static::getPlugin()->getNavigationLabel();
}

public static function canAccess(): bool
{
return static::getPlugin()->isAuthorized();
}

public static function getNavigationGroup(): string | UnitEnum | null
{
return static::getPlugin()->getNavigationGroup();
}

public static function getNavigationSort(): ?int
{
return static::getPlugin()->getNavigationSort();
}

public static function getNavigationIcon(): string | BackedEnum | Heroicon | null
{
return static::getPlugin()->getNavigationIcon();
}

public static function getBreadcrumb(): string
Expand All @@ -30,8 +58,6 @@ public static function getBreadcrumb(): string

protected static ?string $model = CommandRun::class;

protected static string | null | \BackedEnum $navigationIcon = Heroicon::OutlinedCommandLine;

public static function infolist(Schema $schema): Schema
{
return CommandRunInfolist::configure($schema);
Expand Down