Skip to content

Commit e1a4dae

Browse files
deancsmithicanhazstring
authored andcommitted
Add new information methods (isActiveRaw, isEnabledRaw and show) (#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
1 parent 2930482 commit e1a4dae

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Available unit commands are:
9292
- restart
9393
- isEnabled
9494
- isActive
95+
- show
9596

9697
```php
9798
$systemCtl = new SystemCtl();

src/Unit/AbstractUnit.php

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function __construct(string $name, CommandDispatcherInterface $commandDis
3131
$this->commandDispatcher = $commandDispatcher;
3232
}
3333

34-
3534
/**
3635
* @param string $type
3736
* @param string $name
@@ -154,19 +153,41 @@ public function enable(): bool
154153
*/
155154
public function isEnabled(): bool
156155
{
157-
$output = $this->execute('is-enabled')->getOutput();
156+
return $this->isEnabledRaw() === 'enabled';
157+
}
158+
159+
/**
160+
* Get the raw (text) output of the `is-enabled` command.
161+
*
162+
* @return string
163+
*/
164+
public function isEnabledRaw(): string
165+
{
166+
// We have to trim() the output, as it may end in a newline character that we don't want.
167+
$output = \trim($this->execute('is-enabled')->getOutput());
158168

159-
return trim($output) === 'enabled';
169+
return $output;
160170
}
161171

162172
/**
163173
* @return bool
164174
*/
165175
public function isActive(): bool
166176
{
167-
$output = $this->execute('is-active')->getOutput();
177+
return $this->isActiveRaw() === 'active';
178+
}
179+
180+
/**
181+
* Get the raw (text) output of the `is-active` command.
182+
*
183+
* @return string
184+
*/
185+
public function isActiveRaw(): string
186+
{
187+
// We have to trim() the output, as it may end in a newline character that we don't want.
188+
$output = \trim($this->execute('is-active')->getOutput());
168189

169-
return trim($output) === 'active';
190+
return $output;
170191
}
171192

172193
/**
@@ -176,4 +197,43 @@ public function isRunning(): bool
176197
{
177198
return $this->isActive();
178199
}
200+
201+
/**
202+
* Get an array of debugging unit information from the output of the systemctl `show` command.
203+
*
204+
* The output uses the service information as the returned array key, e.g.
205+
* [
206+
* 'Type' => 'service',
207+
* 'Restart' => 'no',
208+
* ...
209+
* ]
210+
*
211+
* @return array
212+
*/
213+
public function show(): array
214+
{
215+
// Turn the output string into an array, using a newline to separate entries.
216+
$output = \explode(
217+
"\n",
218+
$this->execute('show')->getOutput()
219+
);
220+
221+
// Walk the array to re-key it based on the systemd service information kay/value.
222+
$outputArray = [];
223+
\array_walk(
224+
$output,
225+
function ($line) use (&$outputArray) {
226+
// Skip any empty lines/lines that do not contain '=', as the raw systemd output always
227+
// contains =, e.g. 'Restart=no'. If we do not have this value, then we cannot split it as below.
228+
if (empty($line) || false === \strpos($line, "=")) {
229+
return;
230+
}
231+
$lineSplit = \explode("=", $line, 2);
232+
233+
$outputArray[$lineSplit[0]] = $lineSplit[1];
234+
}
235+
);
236+
237+
return $outputArray;
238+
}
179239
}

src/Unit/UnitInterface.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,38 @@ public function enable(): bool;
9898
*/
9999
public function isActive(): bool;
100100

101+
/**
102+
* Get the raw (text) output of the `is-active` command.
103+
*
104+
* @return string
105+
*/
106+
public function isActiveRaw(): string;
107+
101108
/**
102109
* Check whether unit is enabled
103110
*
104111
* @return bool
105112
*/
106113
public function isEnabled(): bool;
114+
115+
/**
116+
* Get the raw (text) output of the `is-enabled` command.
117+
*
118+
* @return string
119+
*/
120+
public function isEnabledRaw(): string;
121+
122+
/**
123+
* Get an array of debugging unit information from the output of the systemctl `show` command.
124+
*
125+
* The output uses the service information as the returned array key, e.g.
126+
* [
127+
* 'Type' => 'service',
128+
* 'Restart' => 'no',
129+
* ...
130+
* ]
131+
*
132+
* @return array
133+
*/
134+
public function show(): array;
107135
}

0 commit comments

Comments
 (0)