Skip to content

Commit

Permalink
Add new information methods (isActiveRaw, isEnabledRaw and show) (
Browse files Browse the repository at this point in the history
#26)

* Added raw variants of isActive/isEnabled

* Add `show` command for additional unit info

* Style fixes and updated README

* Reuse raw methods within boolean methods
  • Loading branch information
deancsmith authored and icanhazstring committed Apr 7, 2019
1 parent 2930482 commit e1a4dae
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Available unit commands are:
- restart
- isEnabled
- isActive
- show

```php
$systemCtl = new SystemCtl();
Expand Down
70 changes: 65 additions & 5 deletions src/Unit/AbstractUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function __construct(string $name, CommandDispatcherInterface $commandDis
$this->commandDispatcher = $commandDispatcher;
}


/**
* @param string $type
* @param string $name
Expand Down Expand Up @@ -154,19 +153,41 @@ public function enable(): bool
*/
public function isEnabled(): bool
{
$output = $this->execute('is-enabled')->getOutput();
return $this->isEnabledRaw() === 'enabled';
}

/**
* Get the raw (text) output of the `is-enabled` command.
*
* @return string
*/
public function isEnabledRaw(): string
{
// We have to trim() the output, as it may end in a newline character that we don't want.
$output = \trim($this->execute('is-enabled')->getOutput());

return trim($output) === 'enabled';
return $output;
}

/**
* @return bool
*/
public function isActive(): bool
{
$output = $this->execute('is-active')->getOutput();
return $this->isActiveRaw() === 'active';
}

/**
* Get the raw (text) output of the `is-active` command.
*
* @return string
*/
public function isActiveRaw(): string
{
// We have to trim() the output, as it may end in a newline character that we don't want.
$output = \trim($this->execute('is-active')->getOutput());

return trim($output) === 'active';
return $output;
}

/**
Expand All @@ -176,4 +197,43 @@ public function isRunning(): bool
{
return $this->isActive();
}

/**
* Get an array of debugging unit information from the output of the systemctl `show` command.
*
* The output uses the service information as the returned array key, e.g.
* [
* 'Type' => 'service',
* 'Restart' => 'no',
* ...
* ]
*
* @return array
*/
public function show(): array
{
// Turn the output string into an array, using a newline to separate entries.
$output = \explode(
"\n",
$this->execute('show')->getOutput()
);

// Walk the array to re-key it based on the systemd service information kay/value.
$outputArray = [];
\array_walk(
$output,
function ($line) use (&$outputArray) {
// Skip any empty lines/lines that do not contain '=', as the raw systemd output always
// contains =, e.g. 'Restart=no'. If we do not have this value, then we cannot split it as below.
if (empty($line) || false === \strpos($line, "=")) {
return;
}
$lineSplit = \explode("=", $line, 2);

$outputArray[$lineSplit[0]] = $lineSplit[1];
}
);

return $outputArray;
}
}
28 changes: 28 additions & 0 deletions src/Unit/UnitInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,38 @@ public function enable(): bool;
*/
public function isActive(): bool;

/**
* Get the raw (text) output of the `is-active` command.
*
* @return string
*/
public function isActiveRaw(): string;

/**
* Check whether unit is enabled
*
* @return bool
*/
public function isEnabled(): bool;

/**
* Get the raw (text) output of the `is-enabled` command.
*
* @return string
*/
public function isEnabledRaw(): string;

/**
* Get an array of debugging unit information from the output of the systemctl `show` command.
*
* The output uses the service information as the returned array key, e.g.
* [
* 'Type' => 'service',
* 'Restart' => 'no',
* ...
* ]
*
* @return array
*/
public function show(): array;
}

0 comments on commit e1a4dae

Please sign in to comment.