summaryrefslogtreecommitdiff
path: root/src/AptModule.php
blob: 8820a13fcb76432f5fa6abcdcc60d27ae94314a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php

namespace PHPIAC\Modules;

use PHPIAC\Connection;
use PHPIAC\Module\Module;
use PHPIAC\Module\State;

class AptModule extends Module
{
    protected string $package;

    protected string $state = State::PRESENT;
    protected bool $updateCache = false;

    /**
     * @inheritDoc
     */
    public function checkState(): bool
    {
        Connection::enablePty();

        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'),
        };

        Connection::disablePty();

        return $state;
    }

    /**
     * @inheritDoc
     */
    public function execute(): void
    {
        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");
        }
    }
}