<commit_before><?php

namespace Kelunik\AcmeClient\Commands;

use Amp\CoroutineResult;
use Amp\File\FilesystemException;
use Amp\Process;
use Kelunik\Acme\AcmeException;
use Kelunik\AcmeClient\ConfigException;
use League\CLImate\Argument\Manager;
use League\CLImate\CLImate;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

class Auto implements Command {
    const EXIT_CONFIG_ERROR = 1;
    const EXIT_SETUP_ERROR = 2;
    const EXIT_ISSUANCE_ERROR = 3;
    const EXIT_ISSUANCE_PARTIAL = 4;
    const EXIT_ISSUANCE_OK = 5;

    const STATUS_NO_CHANGE = 0;
    const STATUS_RENEWED = 1;

    private $climate;

    public function __construct(CLImate $climate) {
        $this->climate = $climate;
    }

    public function execute(Manager $args) {
        return \Amp\resolve($this->doExecute($args));
    }

    /**
     * @param Manager $args
     * @return \Generator
     */
    private function doExecute(Manager $args) {
        $configPath = $args->get("config");

        try {
            $config = Yaml::parse(
                yield \Amp\File\get($configPath)
            );
        } catch (FilesystemException $e) {
            $this->climate->error("Config file ({$configPath}) not found.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        } catch (ParseException $e) {
            $this->climate->error("Config file ({$configPath}) had an invalid format and couldn't be parsed.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        }

        if ($args->defined("server")) {
            $config["server"] = $args->get("server");
        }

        if ($args->defined("storage")) {
            $config["storage"] = $args->get("storage");
        }

        if (!isset($config["server"])) {
            $this->climate->error("Config file ({$configPath}) didn't have a 'server' set nor was it passed as command line argument.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        }

        if (!isset($config["storage"])) {
            $this->climate->error("Config file ({$configPath}) didn't have a 'storage' set nor was it passed as command line argument.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        }

        if (!isset($config["email"])) {
            $this->climate->error("Config file ({$configPath}) didn't have a 'email' set.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        }

        if (!isset($config["certificates"]) || !is_array($config["certificates"])) {
            $this->climate->error("Config file ({$configPath}) didn't have a 'certificates' section that's an array.");<commit_msg>Fall back to global config for server and storage if possible<commit_after><?php

namespace Kelunik\AcmeClient\Commands;

use Amp\CoroutineResult;
use Amp\File\FilesystemException;
use Amp\Process;
use Kelunik\Acme\AcmeException;
use Kelunik\AcmeClient\ConfigException;
use League\CLImate\Argument\Manager;
use League\CLImate\CLImate;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

class Auto implements Command {
    const EXIT_CONFIG_ERROR = 1;
    const EXIT_SETUP_ERROR = 2;
    const EXIT_ISSUANCE_ERROR = 3;
    const EXIT_ISSUANCE_PARTIAL = 4;
    const EXIT_ISSUANCE_OK = 5;

    const STATUS_NO_CHANGE = 0;
    const STATUS_RENEWED = 1;

    private $climate;

    public function __construct(CLImate $climate) {
        $this->climate = $climate;
    }

    public function execute(Manager $args) {
        return \Amp\resolve($this->doExecute($args));
    }

    /**
     * @param Manager $args
     * @return \Generator
     */
    private function doExecute(Manager $args) {
        $configPath = $args->get("config");

        try {
            $config = Yaml::parse(
                yield \Amp\File\get($configPath)
            );
        } catch (FilesystemException $e) {
            $this->climate->error("Config file ({$configPath}) not found.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        } catch (ParseException $e) {
            $this->climate->error("Config file ({$configPath}) had an invalid format and couldn't be parsed.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        }

        if ($args->defined("server")) {
            $config["server"] = $args->get("server");
        } else if (!isset($config["server"]) && $args->exists("server")) {
            $config["server"] = $args->get("server");
        }

        if ($args->defined("storage")) {
            $config["storage"] = $args->get("storage");
        } else if (!isset($config["storage"]) && $args->exists("storage")) {
            $config["storage"] = $args->get("storage");
        }

        if (!isset($config["server"])) {
            $this->climate->error("Config file ({$configPath}) didn't have a 'server' set nor was it passed as command line argument.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        }

        if (!isset($config["storage"])) {
            $this->climate->error("Config file ({$configPath}) didn't have a 'storage' set nor was it passed as command line argument.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        }

        if (!isset($config["email"])) {
            $this->climate->error("Config file ({$configPath}) didn't have a 'email' set.");
            yield new CoroutineResult(self::EXIT_CONFIG_ERROR);
            return;
        }

        if (!isset($config["certificates"]) || !is_array($config["certificates"])) {
            $this->climate->error("Config file ({$configPath}) didn't have a 'certificates' section that's an array.");
{'fraction_non_alphanumeric': 0.14071550255536627, 'fraction_numerical': 0.002385008517887564, 'mean_word_length': 2.648850217526414, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4188376', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1704, 'n_words': 393}