<commit_before><?php namespace WP_CLI_Dotenv_Command;

use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Command;

/**
 * Manage a .env file
 * @package WP_CLI_Dotenv_Command
 */
class Dotenv_Command extends WP_CLI_Command
{

    /**
     * Initialize the environment file
     *
     * [--file=<path-to-dotenv>]
     * : Path to the environment file.  Default: '.env'
     *
     * [--with-salts]
     * : Additionally, generate and define keys for salts
     *
     * [--template=<template-name>]
     * : Path to a template to use to interactively set values
     *
     * [--interactive]
     * : Set new values from the template interactively. Leave blank for no change.
     *
     * @synopsis [--file=<path-to-dotenv>] [--with-salts] [--template=<template-name>] [--interactive]
     *
     * @when before_wp_load
     *
     * @param $_
     * @param $assoc_args
     */
    public function init( $_, $assoc_args )
    {
        $filepath = get_filepath($assoc_args);

        if ( file_exists( $filepath ) ) {
            WP_CLI::error("Environment file already exists at: $filepath");
            return;
        }

        $dotenv = Dotenv_File::create( $filepath );

        if ( ! $dotenv->exists() ) {
            WP_CLI::error('Failed to create environment file at: ' . $dotenv->get_filepath());
            return;
        }

        if ( $template = \WP_CLI\Utils\get_flag_value( $assoc_args, 'template' ) ) {
            $this->init_from_template($dotenv, $template, $assoc_args);
        }

        if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'with-salts' ) ) {
            WP_CLI::run_command( [ 'dotenv', 'salts', 'generate' ], [ 'file' => $dotenv ] );
        }

        WP_CLI::success("$filepath created.");
    }

    /**
     * @param $dotenv
     * @param $template
     * @param $assoc_args
     */
    protected function init_from_template( Dotenv_File &$dotenv, $template, $assoc_args )
    {
        $template_path = get_filepath(['file' => $template]);

        if ( ! file_exists($template_path) ) {
            WP_CLI::error("Template file does not exist at: $template_path");
            return;
        }
        if ( ! is_readable($template_path) ) {
            WP_CLI::error( "Template file is not readable at: $template_path" );
            return;
        }
        if ( ! $dotenv->is_writable() ) {
            WP_CLI::error('Environment file is not readable at: ' . $dotenv->get_filepath());
            return;
        }

        WP_CLI::line("Initializing from template: $template_path");

        copy( $template_path, $dotenv->get_filepath() );

        // we can't use WP-CLI --prompt because we're working off the template, not the synopsis
        if ( ! $interactive = \WP_CLI\Utils\get_flag_value( $assoc_args, 'interactive' ) )
            return;

        WP_CLI::line('Interactive init');
        WP_CLI::line('Specify a new value for each key, or leave blank for no change.');

        // iterate over each line and prompt for a new value
        $dotenv->map(function( $line ) use ( $dotenv )
        {
            $pair = $dotenv->get_pair_for_line($line);

            if ( ! $pair['key'] ) return $line;

            $user_value = \cli\prompt( $pair[ 'key' ], $pair[ 'value' ] );

            if ( ! strlen($user_value) ) return $line;

            return format_line( $pair[ 'key' ], $user_value );
        });
<commit_msg>fix interactive init from template<commit_after><?php namespace WP_CLI_Dotenv_Command;

use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Command;

/**
 * Manage a .env file
 * @package WP_CLI_Dotenv_Command
 */
class Dotenv_Command extends WP_CLI_Command
{

    /**
     * Initialize the environment file
     *
     * [--file=<path-to-dotenv>]
     * : Path to the environment file.  Default: '.env'
     *
     * [--with-salts]
     * : Additionally, generate and define keys for salts
     *
     * [--template=<template-name>]
     * : Path to a template to use to interactively set values
     *
     * [--interactive]
     * : Set new values from the template interactively. Leave blank for no change.
     *
     * @synopsis [--file=<path-to-dotenv>] [--with-salts] [--template=<template-name>] [--interactive]
     *
     * @when before_wp_load
     *
     * @param $_
     * @param $assoc_args
     */
    public function init( $_, $assoc_args )
    {
        $filepath = get_filepath($assoc_args);

        if ( file_exists( $filepath ) ) {
            WP_CLI::error("Environment file already exists at: $filepath");
            return;
        }

        $dotenv = Dotenv_File::create( $filepath );

        if ( ! $dotenv->exists() ) {
            WP_CLI::error('Failed to create environment file at: ' . $dotenv->get_filepath());
            return;
        }

        if ( $template = \WP_CLI\Utils\get_flag_value( $assoc_args, 'template' ) ) {
            $this->init_from_template($dotenv, $template, $assoc_args);
        }

        if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'with-salts' ) ) {
            WP_CLI::run_command( [ 'dotenv', 'salts', 'generate' ], [ 'file' => $dotenv ] );
        }

        WP_CLI::success("$filepath created.");
    }

    /**
     * @param $dotenv
     * @param $template
     * @param $assoc_args
     */
    protected function init_from_template( Dotenv_File &$dotenv, $template, $assoc_args )
    {
        $template_path = get_filepath(['file' => $template]);

        if ( ! file_exists($template_path) ) {
            WP_CLI::error("Template file does not exist at: $template_path");
            return;
        }
        if ( ! is_readable($template_path) ) {
            WP_CLI::error( "Template file is not readable at: $template_path" );
            return;
        }
        if ( ! $dotenv->is_writable() ) {
            WP_CLI::error('Environment file is not readable at: ' . $dotenv->get_filepath());
            return;
        }

        WP_CLI::line("Initializing from template: $template_path");

        copy( $template_path, $dotenv->get_filepath() );

        // we can't use WP-CLI --prompt because we're working off the template, not the synopsis
        if ( ! $interactive = \WP_CLI\Utils\get_flag_value( $assoc_args, 'interactive' ) )
            return;

        $dotenv->load(); // reload the new copied data from template

        WP_CLI::line('Interactive init');
        WP_CLI::line('Specify a new value for each key, or leave blank for no change.');

        // iterate over each line and prompt for a new value
        $dotenv->map(function( $line ) use ( $dotenv )
        {
            $pair = $dotenv->get_pair_for_line($line);

            if ( ! $pair['key'] ) return $line;

            $user_value = \cli\prompt( $pair[ 'key' ], $pair[ 'value' ] );

            if ( ! strlen($user_value) ) return $line;

            return format_line( $pair[ 'key' ], $user_value );
        });

{'fraction_non_alphanumeric': 0.13971124398424967, 'fraction_numerical': 0.0, 'mean_word_length': 2.2227443609022557, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 32, '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': '774857', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2160, 'n_words': 504}