Skip to content
Snippets Groups Projects
Config.php 2.25 KiB
Newer Older
Thomas Flori's avatar
Thomas Flori committed
<?php

namespace App\Command;

use App\Application;
Thomas Flori's avatar
Thomas Flori committed
use App\Model\Fan\HwmonFan;
use App\Service\Utils;
use App\Subroutines\InitializeConfig;
Thomas Flori's avatar
Thomas Flori committed
use GetOpt\GetOpt;
use Hugga\Console;
use Hugga\Input\Question\Choice;
use Hugga\Input\Question\Confirmation;

class Config extends AbstractCommand
{
    protected $name = 'config';

    protected $description = 'Generate or update fan configuration';

    public function __construct(Application $app, Console $console)
    {
        parent::__construct($app, $console);
    }

    public function handle(GetOpt $getOpt): int
    {
        if (posix_getuid() !== 0) {
            $this->warn('This programm has to run as root');
            return 1;
        }

        if ($this->getOption('quiet')) {
            $this->warn('This command requires user interaction');
            return 1;
        }

        $fanConfig = $this->app->fanConfig;
Thomas Flori's avatar
Thomas Flori committed
        try {
            $fanConfig->readConfig();
        } catch (\Throwable $e) {
            $this->error('Unable to read config: ' . $e->getMessage());
        }
Thomas Flori's avatar
Thomas Flori committed
        if (!$fanConfig->hasFans()) {
            if ($this->app->get(InitializeConfig::class)->main() > 0) {
Thomas Flori's avatar
Thomas Flori committed
                return 1;
            }
        }

        $actions = new Choice([
            'edit a fan',
            'add a sensor',
            'edit a sensor',
            'add a rule',
            'edit a rule',
            'save and exit',
            'exit without saving',
        ], 'What next?', 'save and exit');
        do {
Thomas Flori's avatar
Thomas Flori committed
            $fanConfig->display($this->console);
            $answer = $this->ask($actions);
Thomas Flori's avatar
Thomas Flori committed
            switch ($answer) {
Thomas Flori's avatar
Thomas Flori committed
                case 'edit a fan':
                    $fan = $this->selectFan($fanConfig->getFans());
                    break;
Thomas Flori's avatar
Thomas Flori committed
                case 'exit without saving':
                    break 2;
                case 'save and exit':
Thomas Flori's avatar
Thomas Flori committed
                    $fanConfig->save();
Thomas Flori's avatar
Thomas Flori committed
                    break 2;
                default:
Thomas Flori's avatar
Thomas Flori committed
                    $this->warn('not implemented');
                    break;
Thomas Flori's avatar
Thomas Flori committed
            }
        } while (true);


        // - edit a fan
        // - add a sensor
        // - edit a sensor
        // - add a rule
        // - edit a rule
        // - save and leave
        // - leave without saving

        return 0;
    }
}