diff options
author | Daniel Weipert <code@drogueronin.de> | 2021-04-18 21:46:12 +0200 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2021-04-18 21:46:12 +0200 |
commit | 82e4d1ca348748e8cdde86f65e62221968cabed5 (patch) | |
tree | 778e3be9a9b3fa8cfb6cda1a34230fdf402da997 /src/Module/Modules/AptModule.php | |
parent | c0ebe78089121c0ad23efb0af32c435bee543a3c (diff) |
Slight refactoring
Diffstat (limited to 'src/Module/Modules/AptModule.php')
-rw-r--r-- | src/Module/Modules/AptModule.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/Module/Modules/AptModule.php b/src/Module/Modules/AptModule.php new file mode 100644 index 0000000..ccf4892 --- /dev/null +++ b/src/Module/Modules/AptModule.php @@ -0,0 +1,54 @@ +<?php + +namespace PHPIAC\Module\Modules; + +use PHPIAC\Module\ModuleInterface; +use PHPIAC\Module\State; +use phpseclib3\Net\SSH2; + +class AptModule implements ModuleInterface +{ + /** + * AptModule constructor. + * + * @param string $package + * @param string $state + */ + public function __construct( + private string $package, + private string $state = State::PRESENT + ) {} + + /** + * @inheritDoc + */ + public function checkState(): bool + { + global $ssh; + /**@var SSH2 $ssh*/ + $ssh->enablePTY(); + + $ssh->exec("dpkg -l $this->package | grep 'ii'"); + $dpkg = $ssh->read(); + + $state = match ($this->state) { + State::PRESENT => str_starts_with($dpkg, 'ii'), + State::ABSENT => str_contains($dpkg, 'no packages found'), + }; + + $ssh->disablePTY(); + + return $state; + } + + /** + * @inheritDoc + */ + public function getCommands(): array + { + return match ($this->state) { + State::PRESENT => ["sudo apt install -y $this->package"], + State::ABSENT => ["sudo apt remove -y $this->package"], + }; + } +} |