Skip to content

Commit

Permalink
feature: add target units support for systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Jan 28, 2025
1 parent b777f6a commit 87f16a7
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 22 deletions.
17 changes: 13 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@ jobs:
composer-flags: '--prefer-lowest'
job-description: 'Ubuntu; PHP 8.3; lowest-deps'

- operating-system: 'ubuntu-latest'
php-version: '8.4'
job-description: 'Ubuntu; PHP 8.4; latest-deps'

- operating-system: 'ubuntu-latest'
php-version: '8.4'
composer-flags: '--prefer-lowest'
job-description: 'Ubuntu; PHP 8.4; lowest-deps'

- operating-system: 'windows-latest'
php-version: '8.3'
job-description: 'Windows; PHP 8.3; latest-deps'
php-version: '8.4'
job-description: 'Windows; PHP 8.4; latest-deps'

#- operating-system: 'macos-latest'
# php-version: '8.3'
# job-description: 'MacOS; PHP 8.3; latest-deps'
# php-version: '8.4'
# job-description: 'MacOS; PHP 8.4; latest-deps'


name: ${{ matrix.job-description }}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"phpstan/phpstan": "^1.10",
"friendsofphp/php-cs-fixer": "^3.46"
"phpstan/phpstan": "^2",
"friendsofphp/php-cs-fixer": "^3.68"
},
"suggest": {
"ext-apcu": "APCU info"
Expand Down
14 changes: 9 additions & 5 deletions src/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,21 @@ public static function parseKeyValueBlock(string $block, string $delimiter = ':'
/**
* @param Service[] $services
*/
public static function searchService(array $services, string $serviceName): ?Service
public static function searchService(array $services, string $serviceName, ?string $type = null): ?Service
{
$item = null;
foreach ($services as $service) {
if ($service->getName() === $serviceName) {
$item = $service;
break;
if ($type) {
if ($service->getType() === $type) {
return $service;
}
} else {
return $service;
}
}
}

return $item;
return null;
}

public static function convertHumanSizeToBytes(string $humanSize): ?float
Expand Down
2 changes: 1 addition & 1 deletion src/Info/Samba.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Samba
{
/** @var File[] */
private array $files = [];
/** @var \Ginfo\Info\Samba\Service[] */
/** @var Samba\Service[] */
private array $services = [];
/** @var Connection[] */
private array $connections = [];
Expand Down
16 changes: 16 additions & 0 deletions src/Info/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@

class Service
{
public const TYPE_SERVICE = 'service';
public const TYPE_TARGET = 'target';

private string $name;
private string $description;
private bool $loaded;
private bool $started;
private string $state;
private ?string $type = null;

public function getType(): ?string
{
return $this->type;
}

public function setType(?string $type): self
{
$this->type = $type;

return $this;
}

public function getName(): string
{
Expand Down
33 changes: 24 additions & 9 deletions src/OS/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,19 +590,34 @@ public function getProcesses(): ?array

public function getServices(): ?array
{
$services = Systemd::work();
if (null === $services) {
$services = Systemd::work(Service::TYPE_SERVICE);
$targets = Systemd::work(Service::TYPE_TARGET);
if (null === $services && null === $targets) {
return null;
}

$out = [];
foreach ($services as $service) {
$out[] = (new Service())
->setName($service['name'])
->setDescription($service['description'])
->setLoaded($service['loaded'])
->setStarted($service['started'])
->setState($service['state']);
if ($services) {
foreach ($services as $service) {
$out[] = (new Service())
->setType(Service::TYPE_SERVICE)
->setName($service['name'])
->setDescription($service['description'])
->setLoaded($service['loaded'])
->setStarted($service['started'])
->setState($service['state']);
}
}
if ($targets) {
foreach ($targets as $service) {
$out[] = (new Service())
->setType(Service::TYPE_TARGET)
->setName($service['name'])
->setDescription($service['description'])
->setLoaded($service['loaded'])
->setStarted($service['started'])
->setState($service['state']);
}
}

return $out;
Expand Down
46 changes: 45 additions & 1 deletion src/Parsers/Systemd.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ginfo\Parsers;

use Ginfo\Info\Service;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\ProcessStartFailedException;
use Symfony\Component\Process\Process;
Expand All @@ -16,7 +17,49 @@ private function __clone()
{
}

public static function work(): ?array
public static function work(string $type = Service::TYPE_SERVICE): ?array
{
return match ($type) {
Service::TYPE_SERVICE => self::services(),
Service::TYPE_TARGET => self::targets(),
default => null,
};
}

private static function targets(): ?array
{
$process = new Process(['systemctl', 'list-units', '--type', 'target', '--all'], null, ['LANG' => 'C']);
try {
$process->mustRun();
} catch (ProcessFailedException|ProcessStartFailedException $e) {
return null;
}

$list = $process->getOutput();

$lines = \explode("\n", \explode("\n\n", $list, 2)[0]);
\array_shift($lines); // remove header

$out = [];
foreach ($lines as $line) {
$line = \ltrim($line, '');
$line = \trim($line);
[$unit, $load, $active, $sub, $description] = \preg_split('/\s+/', $line, 5);

$out[] = [
'type' => 'target',
'name' => $unit,
'loaded' => 'loaded' === $load,
'started' => 'active' === $active,
'state' => $sub,
'description' => $description,
];
}

return $out;
}

private static function services(): ?array
{
$process = new Process(['systemctl', 'list-units', '--type', 'service', '--all'], null, ['LANG' => 'C']);
try {
Expand All @@ -37,6 +80,7 @@ public static function work(): ?array
[$unit, $load, $active, $sub, $description] = \preg_split('/\s+/', $line, 5);

$out[] = [
'type' => 'service',
'name' => $unit,
'loaded' => 'loaded' === $load,
'started' => 'active' === $active,
Expand Down

0 comments on commit 87f16a7

Please sign in to comment.