Skip to content

Commit 7694959

Browse files
committed
Add user/system scope to systemctl command dispatching
1 parent 66b6bfa commit 7694959

23 files changed

+313
-135
lines changed

src/Command/CommandDispatcherInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public function setTimeout(int $timeout): CommandDispatcherInterface;
3131
*/
3232
public function setBinary(string $binary): CommandDispatcherInterface;
3333

34+
/**
35+
* Set additional arguments to be passed to dispatch
36+
*
37+
* @param string[] $arguments
38+
*
39+
* @return CommandDispatcherInterface
40+
*/
41+
public function setArguments(array $arguments): CommandDispatcherInterface;
42+
3443
/**
3544
* Dispatch given commands against implementers logic and creating a new command
3645
* to read results

src/Command/SymfonyCommandDispatcher.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
*/
1414
class SymfonyCommandDispatcher implements CommandDispatcherInterface
1515
{
16+
/** @var string */
1617
private $binary;
17-
private $timetout;
18+
/** @var float */
19+
private $timeout;
20+
/** @var string[] */
21+
private $arguments = [];
1822

1923
/**
2024
* @inheritdoc
@@ -31,20 +35,54 @@ public function setBinary(string $binary): CommandDispatcherInterface
3135
*/
3236
public function setTimeout(int $timeout): CommandDispatcherInterface
3337
{
34-
$this->timetout = $timeout;
38+
$this->timeout = $timeout;
3539

3640
return $this;
3741
}
3842

43+
/**
44+
* @inheritdoc
45+
*/
46+
public function setArguments(array $arguments): CommandDispatcherInterface
47+
{
48+
$this->arguments = $arguments;
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getBinary(): string
57+
{
58+
return $this->binary;
59+
}
60+
61+
/**
62+
* @return float
63+
*/
64+
public function getTimeout(): float
65+
{
66+
return $this->timeout;
67+
}
68+
69+
/**
70+
* @return string[]
71+
*/
72+
public function getArguments(): array
73+
{
74+
return $this->arguments;
75+
}
76+
3977
/**
4078
* @inheritDoc
4179
*/
4280
public function dispatch(string ...$commands): CommandInterface
4381
{
4482
$processBuilder = new ProcessBuilder();
4583
$processBuilder->setPrefix($this->binary);
46-
$processBuilder->setTimeout($this->timetout);
47-
$processBuilder->setArguments($commands);
84+
$processBuilder->setTimeout($this->timeout);
85+
$processBuilder->setArguments(array_merge($this->arguments, $commands));
4886

4987
$process = new SymfonyCommand($processBuilder->getProcess());
5088

src/Scope/ScopeInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SystemCtl\Scope;
5+
6+
/**
7+
* ScopeInterface
8+
*
9+
* @package SystemCtl\Scope
10+
* @author icanhazstring <[email protected]>
11+
*/
12+
interface ScopeInterface
13+
{
14+
/**
15+
* Return scope argument value for dispatching commands
16+
*
17+
* @return string
18+
*/
19+
public function __toString(): string;
20+
}

src/Scope/SystemScope.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SystemCtl\Scope;
5+
6+
/**
7+
* SystemScope
8+
*
9+
* @package SystemCtl\Scope
10+
* @author icanhazstring <[email protected]>
11+
*/
12+
class SystemScope implements ScopeInterface
13+
{
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function __toString(): string
18+
{
19+
return '--system';
20+
}
21+
}

src/Scope/UserScope.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SystemCtl\Scope;
5+
6+
/**
7+
* UserScope
8+
*
9+
* @package SystemCtl\Scope
10+
* @author icanhazstring <[email protected]>
11+
*/
12+
class UserScope implements ScopeInterface
13+
{
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function __toString(): string
18+
{
19+
return '--user';
20+
}
21+
}

src/SystemCtl.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use SystemCtl\Command\SymfonyCommandDispatcher;
88
use SystemCtl\Exception\UnitNotFoundException;
99
use SystemCtl\Exception\UnitTypeNotSupportedException;
10+
use SystemCtl\Scope\ScopeInterface;
11+
use SystemCtl\Scope\SystemScope;
12+
use SystemCtl\Scope\UserScope;
1013
use SystemCtl\Template\AbstractUnitTemplate;
1114
use SystemCtl\Template\Installer\UnitInstaller;
1215
use SystemCtl\Template\Installer\UnitInstallerInterface;
@@ -41,6 +44,9 @@ class SystemCtl
4144
/** @var UnitInstallerInterface */
4245
private $unitInstaller;
4346

47+
/** @var ScopeInterface */
48+
private $scope;
49+
4450
public const AVAILABLE_UNITS = [
4551
Service::UNIT,
4652
'socket',
@@ -132,6 +138,42 @@ public function listUnits(?string $unitPrefix = null, array $unitTypes = self::S
132138
}, []);
133139
}
134140

141+
/**
142+
* Current scope this system ctl instance is running
143+
*
144+
* @return ScopeInterface
145+
*/
146+
public function getScope(): ScopeInterface
147+
{
148+
if ($this->scope === null) {
149+
$this->scope = new SystemScope;
150+
}
151+
152+
return $this->scope;
153+
}
154+
155+
/**
156+
* Switch to user scope
157+
*
158+
* @return SystemCtl
159+
*/
160+
public function user(): self
161+
{
162+
$this->scope = new UserScope;
163+
164+
return $this;
165+
}
166+
167+
/**
168+
* Switch to system scope
169+
*/
170+
public function system(): self
171+
{
172+
$this->scope = new SystemScope;
173+
174+
return $this;
175+
}
176+
135177
/**
136178
* @param string $name
137179
*
@@ -213,6 +255,8 @@ public function getCommandDispatcher(): CommandDispatcherInterface
213255
->setBinary(self::$binary);
214256
}
215257

258+
$this->commandDispatcher->setArguments([(string)$this->getScope()]);
259+
216260
return $this->commandDispatcher;
217261
}
218262

src/Template/Installer/UnitInstaller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class UnitInstaller implements UnitInstallerInterface
2727
public function setPath(string $path): UnitInstallerInterface
2828
{
2929
$this->path = rtrim($path, '/');
30+
3031
return $this;
3132
}
3233

tests/Functional/Command/SymfonyCommandDispatcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function itShouldDispatchACorrectCommand()
2323
$dispatcher->setBinary('echo');
2424

2525
$output = $dispatcher->dispatch('a')->getOutput();
26-
$this->assertEquals('a', trim($output));
26+
self::assertEquals('a', trim($output));
2727
}
2828
}

tests/Integration/SystemCtlTest.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ private function buildCommandDispatcherStub(): ObjectProphecy
6161
$commandDispatcherStub = $this->prophesize(CommandDispatcherInterface::class);
6262
$commandDispatcherStub->setTimeout(Argument::type('int'))->willReturn($commandDispatcherStub);
6363
$commandDispatcherStub->setBinary(Argument::type('string'))->willReturn($commandDispatcherStub);
64+
$commandDispatcherStub->setArguments(Argument::type('array'))->willReturn($commandDispatcherStub);
6465

6566
return $commandDispatcherStub;
6667
}
@@ -105,7 +106,7 @@ public function testListUnitsWithAvailableUnits()
105106
$systemctl->setCommandDispatcher($dispatcherStub->reveal());
106107

107108
$units = $systemctl->listUnits(null, SystemCtl::AVAILABLE_UNITS);
108-
$this->assertCount(11, $units);
109+
self::assertCount(11, $units);
109110
}
110111

111112
public function testListUnitsWithSupportedUnits()
@@ -134,7 +135,7 @@ public function testListUnitsWithSupportedUnits()
134135
$systemctl->setCommandDispatcher($dispatcherStub->reveal());
135136

136137
$units = $systemctl->listUnits();
137-
$this->assertCount(5, $units);
138+
self::assertCount(5, $units);
138139
}
139140

140141
public function testGetServices()
@@ -159,7 +160,7 @@ public function testGetServices()
159160

160161
$services = $systemctl->getServices();
161162

162-
$this->assertCount(2, $services);
163+
self::assertCount(2, $services);
163164
}
164165

165166
public function testGetTimers()
@@ -183,7 +184,7 @@ public function testGetTimers()
183184
$systemctl->setCommandDispatcher($dispatcherStub->reveal());
184185
$timers = $systemctl->getTimers();
185186

186-
$this->assertCount(2, $timers);
187+
self::assertCount(2, $timers);
187188
}
188189

189190
/**
@@ -195,12 +196,12 @@ public function itShouldReturnTrueOnSuccessfulDaemonReload()
195196
$command->isSuccessful()->willReturn(true);
196197

197198
$dispatcher = $this->buildCommandDispatcherStub();
198-
$dispatcher->dispatch(Argument::exact('daemon-reload'))->willReturn($command);
199+
$dispatcher->dispatch('daemon-reload')->willReturn($command);
199200

200201
$systemCtl = new SystemCtl();
201202
$systemCtl->setCommandDispatcher($dispatcher->reveal());
202203

203-
$this->assertTrue($systemCtl->daemonReload());
204+
self::assertTrue($systemCtl->daemonReload());
204205
}
205206

206207
/**
@@ -239,7 +240,7 @@ public function itShouldReturnUnitAfterInstall()
239240

240241
$unit = $systemctl->install($unitTemplate);
241242

242-
$this->assertEquals($unitName, $unit->getName());
243+
self::assertEquals($unitName, $unit->getName());
243244
}
244245

245246
/**
@@ -250,6 +251,28 @@ public function itShouldReturnDefaultInstallerIfReceived()
250251
SystemCtl::setAssetPath('vfs://');
251252

252253
$systemCtl = new SystemCtl;
253-
$this->assertInstanceOf(UnitInstaller::class, $systemCtl->getUnitInstaller());
254+
self::assertInstanceOf(UnitInstaller::class, $systemCtl->getUnitInstaller());
255+
}
256+
257+
/**
258+
* @test
259+
*/
260+
public function itShouldAddScopeArgumentToDispatcher()
261+
{
262+
$output = 'testService.service Active';
263+
264+
$dispatcher = $this->buildCommandDispatcherStub();
265+
$dispatcher->setArguments(['--system'])->shouldBeCalled()->willReturn($dispatcher->reveal());
266+
$dispatcher->dispatch('list-units', 'testService')
267+
->shouldBeCalled()
268+
->willReturn($this->buildCommandStub($output));
269+
270+
$systemCtl = new SystemCtl;
271+
$systemCtl->setCommandDispatcher($dispatcher->reveal());
272+
273+
$systemCtl->getService('testService');
274+
275+
$dispatcher->setArguments(['--user'])->shouldBeCalled()->willReturn($dispatcher->reveal());
276+
$systemCtl->user()->getService('testService');
254277
}
255278
}

tests/Integration/Template/Installer/UnitInstallerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function itShouldCreateTargetFile()
7777

7878
$installer = (new UnitInstaller)->setPath('vfs://units/')->setRenderer($renderer);
7979

80-
$this->assertTrue($installer->install($template));
80+
self::assertTrue($installer->install($template));
8181

8282
/** @var File $file */
8383
$file = self::$fileSystem->get('/units/awesomeService.service');
@@ -89,6 +89,6 @@ public function itShouldCreateTargetFile()
8989
EOF;
9090

9191

92-
$this->assertEquals($expected, $file->getContent());
92+
self::assertEquals($expected, $file->getContent());
9393
}
9494
}

0 commit comments

Comments
 (0)