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
55 changes: 55 additions & 0 deletions app/Commands/System/BatteryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Commands\System;

use App\Commands\Concerns\InteractsWithLocale;
use App\Contracts\SystemDriverInterface;
use LaravelZero\Framework\Commands\Command;

use function Laravel\Prompts\info;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\warning;

class BatteryCommand extends Command
{
use InteractsWithLocale;

/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'system:battery {--lang= : Interface language (en or pt_BR)}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Inspect laptop battery charge level, design vs full capacity, wear percentage, and cycles';

/**
* Execute the console command.
*/
public function handle(SystemDriverInterface $driver): int
{
$this->resolveLocale();

info((string) __('messages.system.battery_section'));

$result = spin(
callback: fn () => $driver->getBatteryHealth(),
message: (string) __('messages.spinners.system_battery'),
);

if (! $result->success) {
warning((string) __($result->message));

return self::SUCCESS;
}

return $this->renderActionResult($result);
}
}
93 changes: 93 additions & 0 deletions app/Commands/System/EnvInspectorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace App\Commands\System;

use App\Commands\Concerns\InteractsWithLocale;
use App\Contracts\SystemDriverInterface;
use LaravelZero\Framework\Commands\Command;

use function Laravel\Prompts\info;
use function Laravel\Prompts\note;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\table;

class EnvInspectorCommand extends Command
{
use InteractsWithLocale;

/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'system:env {--lang= : Interface language (en or pt_BR)}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Inspect system environment variables and audit PATH directories for missing or duplicate entries';

/**
* Execute the console command.
*/
public function handle(SystemDriverInterface $driver): int
{
$this->resolveLocale();

$audit = spin(
callback: fn (): array => $driver->auditEnvironmentPath(),
message: (string) __('messages.spinners.system_env'),
);

info((string) __('messages.system.env_section'));

$varRows = [];
foreach ($audit['variables'] as $key => $val) {
$varRows[] = [$key, $val];
}

table(
headers: [
(string) __('messages.common.metric'),
(string) __('messages.common.value'),
],
rows: $varRows,
);

info((string) __('messages.system.path_audit_section'));

$statusLabels = [
'VALID' => (string) __('messages.system.path_states.valid'),
'BROKEN' => (string) __('messages.system.path_states.broken'),
'DUPLICATE' => (string) __('messages.system.path_states.duplicate'),
];

table(
headers: [
'#',
(string) __('messages.system.path_headers.directory'),
(string) __('messages.system.path_headers.status'),
],
rows: array_map(
fn (array $entry): array => [
(string) $entry['index'],
$entry['path'],
sprintf('%s %s', $entry['icon'], $statusLabels[$entry['status']] ?? $entry['status']),
],
$audit['path_entries']
),
);

note((string) __('messages.system.path_summary', [
'valid' => $audit['summary']['valid'],
'broken' => $audit['summary']['broken'],
'duplicate' => $audit['summary']['duplicate'],
]));

return self::SUCCESS;
}
}
181 changes: 181 additions & 0 deletions app/Commands/System/KillProcessCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

declare(strict_types=1);

namespace App\Commands\System;

use App\Commands\Concerns\InteractsWithLocale;
use App\Commands\Concerns\InteractsWithTerminal;
use App\Contracts\SystemDriverInterface;
use LaravelZero\Framework\Commands\Command;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\info;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\table;
use function Laravel\Prompts\warning;

class KillProcessCommand extends Command
{
use InteractsWithLocale;
use InteractsWithTerminal;

/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'system:kill
{target? : Process PID or process name to terminate}
{--force : Skip confirmation prompt}
{--lang= : Interface language (en or pt_BR)}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Terminate a frozen or resource-heavy process by PID, name, or interactive top-10 selection';

/**
* Execute the console command.
*/
public function handle(SystemDriverInterface $driver): int
{
$this->resolveLocale();

info((string) __('messages.system.kill_section'));

$targetArg = $this->argument('target');
if (is_string($targetArg) && trim($targetArg) !== '') {
return $this->terminateDirectTarget($driver, trim($targetArg));
}

/** @var array<int, array{pid: int, name: string, cpu: string, memory: string}> $processes */
$processes = spin(
callback: fn (): array => $driver->getTopProcesses(10),
message: (string) __('messages.spinners.system_top'),
);

if ($processes === []) {
warning((string) __('messages.system.no_processes'));

return self::SUCCESS;
}

$rows = [];
$indexedProcesses = [];
$menuOptions = [];

foreach (array_values($processes) as $idx => $proc) {
$num = (string) ($idx + 1);
$indexedProcesses[$num] = $proc;
$menuOptions[$num] = sprintf('%s (PID %d | RAM: %s)', $proc['name'], $proc['pid'], $proc['memory']);

$rows[] = [
sprintf('[%s]', $num),
(string) $proc['pid'],
$proc['name'],
$proc['cpu'],
$proc['memory'],
];
}

table(
headers: [
'#',
(string) __('messages.system.process_headers.pid'),
(string) __('messages.system.process_headers.name'),
(string) __('messages.system.process_headers.cpu'),
(string) __('messages.system.process_headers.memory'),
],
rows: $rows,
);

if (! $this->input->isInteractive()) {
return self::SUCCESS;
}

$menuOptions['0'] = (string) __('messages.common.cancelled');

$choice = $this->promptNumericMenu(
promptLabel: (string) __('messages.system.select_process_prompt'),
options: $menuOptions,
defaultKey: '0',
);

if ($choice === '0' || ! isset($indexedProcesses[$choice])) {
warning((string) __('messages.common.cancelled'));

return self::SUCCESS;
}

$selected = $indexedProcesses[$choice];
$pid = (int) $selected['pid'];
$name = (string) $selected['name'];

if (! $this->option('force') && ! confirm(
label: (string) __('messages.system.confirm_kill', ['pid' => $pid, 'name' => $name]),
default: false,
yes: (string) __('messages.common.yes'),
no: (string) __('messages.common.no'),
)) {
warning((string) __('messages.common.cancelled'));

return self::SUCCESS;
}

$result = spin(
callback: fn () => $driver->killProcess($pid),
message: (string) __('messages.spinners.killing_process', ['pid' => $pid]),
);

return $this->renderActionResult($result);
}

/**
* Terminate a process directly by numeric PID or process name.
*/
protected function terminateDirectTarget(SystemDriverInterface $driver, string $target): int
{
if (ctype_digit($target)) {
$pid = (int) $target;

if (! $this->option('force') && ! confirm(
label: (string) __('messages.system.confirm_kill', ['pid' => $pid, 'name' => 'PID ' . $pid]),
default: false,
yes: (string) __('messages.common.yes'),
no: (string) __('messages.common.no'),
)) {
warning((string) __('messages.common.cancelled'));

return self::SUCCESS;
}

$result = spin(
callback: fn () => $driver->killProcess($pid),
message: (string) __('messages.spinners.killing_process', ['pid' => $pid]),
);

return $this->renderActionResult($result);
}

if (! $this->option('force') && ! confirm(
label: (string) __('messages.system.confirm_kill_name', ['name' => $target]),
default: false,
yes: (string) __('messages.common.yes'),
no: (string) __('messages.common.no'),
)) {
warning((string) __('messages.common.cancelled'));

return self::SUCCESS;
}

$result = spin(
callback: fn () => $driver->killProcessByName($target),
message: (string) __('messages.spinners.killing_process', ['pid' => $target]),
);

return $this->renderActionResult($result);
}
}
Loading
Loading