From 11dc8c730dda2c5bd38cb386f96331c5ce3cac9c Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 20 Apr 2021 12:44:20 +0200 Subject: Adds a bunch of new Modules --- src/AptModule.php | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) (limited to 'src/AptModule.php') diff --git a/src/AptModule.php b/src/AptModule.php index 9b882e8..c133521 100644 --- a/src/AptModule.php +++ b/src/AptModule.php @@ -2,41 +2,33 @@ namespace PHPIAC\Modules; +use PHPIAC\Connection; use PHPIAC\Module\Module; use PHPIAC\Module\State; -use phpseclib3\Net\SSH2; class AptModule extends Module { - /** - * AptModule constructor. - * - * @param string $package - * @param string $state - */ - public function __construct( - private string $package, - private string $state = State::PRESENT - ) {} + protected string $package; + + protected string $state = State::PRESENT; + protected bool $updateCache = false; /** * @inheritDoc */ public function checkState(): bool { - global $ssh; - /**@var SSH2 $ssh*/ - $ssh->enablePTY(); + Connection::enablePty(); - $ssh->exec("dpkg -l $this->package | grep 'ii'"); - $dpkg = $ssh->read(); + Connection::exec("dpkg -l $this->package | grep 'ii'"); + $dpkg = Connection::read(); $state = match ($this->state) { State::PRESENT => str_starts_with($dpkg, 'ii'), State::ABSENT => str_contains($dpkg, 'no packages found'), }; - $ssh->disablePTY(); + Connection::disablePty(); return $state; } @@ -46,9 +38,13 @@ class AptModule extends Module */ public function getCommands(): array { - return match ($this->state) { - State::PRESENT => ["sudo apt install -y $this->package"], - State::ABSENT => ["sudo apt remove -y $this->package"], - }; + if ($this->state === State::PRESENT) { + Connection::exec("sudo apt install -y $this->package"); + } + else if ($this->state === State::ABSENT) { + Connection::exec("sudo apt remove -y $this->package"); + } + + return []; } } -- cgit v1.2.3