summaryrefslogtreecommitdiff
path: root/src/Module
diff options
context:
space:
mode:
Diffstat (limited to 'src/Module')
-rw-r--r--src/Module/AptModule.php48
-rw-r--r--src/Module/ModuleInterface.php6
-rw-r--r--src/Module/Modules/AptModule.php54
3 files changed, 60 insertions, 48 deletions
diff --git a/src/Module/AptModule.php b/src/Module/AptModule.php
deleted file mode 100644
index ff4cb95..0000000
--- a/src/Module/AptModule.php
+++ /dev/null
@@ -1,48 +0,0 @@
-<?php
-
-namespace PHPIAC\Module;
-
-use phpseclib3\Net\SSH2;
-
-class AptModule implements ModuleInterface
-{
- public function __construct(
- private string $package,
- private string $state = State::PRESENT
- ) {}
-
- public function checkState(): bool
- {
- global $ssh;
- /**@var SSH2 $ssh*/
- $ssh->enablePTY();
- $ssh->exec("which $this->package");
-
- if ($this->state === State::PRESENT) {
- $state = ! empty($ssh->read());
- }
- else if ($this->state === State::ABSENT) {
- $state = empty($ssh->read());
- }
-
- $ssh->disablePTY();
-
- return $state;
- }
-
- public function getCommands(): array
- {
- if ($this->state === State::PRESENT) {
- return [
- "sudo apt install -y $this->package",
- ];
- }
- else if ($this->state === State::ABSENT) {
- return [
- "sudo apt remove -y $this->package",
- ];
- }
-
- return [];
- }
-}
diff --git a/src/Module/ModuleInterface.php b/src/Module/ModuleInterface.php
index 2963b5f..649a095 100644
--- a/src/Module/ModuleInterface.php
+++ b/src/Module/ModuleInterface.php
@@ -4,7 +4,13 @@ namespace PHPIAC\Module;
interface ModuleInterface
{
+ /**
+ * @return bool
+ */
public function checkState(): bool;
+ /**
+ * @return array
+ */
public function getCommands(): array;
}
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"],
+ };
+ }
+}