<commit_before><?php

namespace DonePM\ConsoleClient\Commands;

use DonePM\ConsoleClient\Repositories\Config;
use DonePM\ConsoleClient\Repositories\Reader\JsonReader;
use DonePM\ConsoleClient\Repositories\Writer\JsonWriter;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Class InitCommand
 *
 * @package DonePM\ConsoleClient\Commands
 */
class InitCommand extends Command
{
    /**
     * configures the command
     */
    protected function configure()
    {
        $this
            ->setName('dpm:init')
            ->setDescription('Initializes local dpm account');
    }

    /**
     * executes the command
     */
    protected function handle()
    {
        $configFile = $this->getConfigFile();

        $config = $this->readConfigOrUseDefaultConfig($configFile);

        if ( ! $config->has('url')
            || ! $this->confirm('Is your donePM API url ' . $config->get('url') . '?', true)
        ) {
            $url = $this->ask('What is your donePM API url?', 'https://api.done.pm/');
            $config->set('url', $url);
        }

        if ( ! $config->has('email')
            || ! $this->confirm('Is your donePM email ' . $config->get('email') . '?', true)
        ) {
            $email = $this->ask('What is your donePM email?');
            $config->set('email', $email);
        }

        if ( ! $config->has('password')
            || ! $this->confirm('Do you want to keep your password?', true)
        ) {
            $password = $this->secret('What is your donePM password? (will be stored encrypted)');

            $key = $config->get('key', Str::random(16));
            $encrypter = new Encrypter($key);
            $config->set('password', $encrypter->encrypt($password));
            $config->set('key', $key);
        }

        if ( ! $config->has('token')) {
            // fetch token
            // $this->call('dpm:token');
        }

        (new JsonWriter())->write($configFile, $config);
    }

    /**
     * reads existing config or returns default config
     *
     * @param string $configFile
     *
     * @return \DonePM\ConsoleClient\Repositories\Config|\DonePM\ConsoleClient\Repositories\Repository
     */
    private function readConfigOrUseDefaultConfig($configFile)
    {

        if (file_exists($configFile)) {
            return (new JsonReader())->read($configFile);
        }

        return new Config();
    }
}<commit_msg>refactored init command to handle new structure and reduce the config knowledge -> ask application<commit_after><?php

namespace DonePM\ConsoleClient\Commands;

use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Class InitCommand
 *
 * @package DonePM\ConsoleClient\Commands
 */
class InitCommand extends Command
{
    /**
     * configures the command
     */
    protected function configure()
    {
        $this
            ->setName('init')
            ->setDescription('Initializes local dpm account');
    }

    /**
     * executes the command
     */
    protected function handle()
    {
        $config = $this->getApplication()->config();

        if ( ! $config->has('url')
            || ! $this->confirm('Is your donePM API url ' . $config->get('url') . '?', true)
        ) {
            $url = $this->ask('What is your donePM API url?', 'https://api.done.pm/');
            $config->set('url', $url);
        }

        if ( ! $config->has('email')
            || ! $this->confirm('Is your donePM email ' . $config->get('email') . '?', true)
        ) {
            $email = $this->ask('What is your donePM email?');
            $config->set('email', $email);
        }

        if ( ! $config->has('password')
            || ! $this->confirm('Do you want to keep your password?', true)
        ) {
            $password = $this->secret('What is your donePM password? (will be stored encrypted)');

            $key = $config->get('key', Str::random(16));
            $encrypter = new Encrypter($key);
            $config->set('password', $encrypter->encrypt($password));
            $config->set('key', $key);
        }

        if ( ! $config->has('token')) {
            // fetch token
            // $this->call('dpm:token');
        }

        $this->getApplication()->writeConfig($config);
    }
}
{'fraction_non_alphanumeric': 0.16, 'fraction_numerical': 0.000893854748603352, 'mean_word_length': 2.267153284671533, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 2, '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': '1436437', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1282, 'n_words': 266}