<commit_before>from pathlib import Path

import click

from vantage import utils


@click.command(name="__env")
@click.option(
    "-b", "--base64", is_flag=True, help="When setting a var convert it to base64 first"
)
@click.option(
    "-s", "--stdin", is_flag=True, help="When setting a var read the value from stdin"
)
@click.argument("key_val", required=False)
@click.pass_obj
def env(env, key_val=None, base64=False, stdin=False):
    """Read and set environment variables and files"""
    utils.loquacious("Running __env")
    if key_val is None:
        utils.loquacious("  Printing ENV")
        for k, v in env.items():
            click.echo(f"{k}={v}")
    elif "=" in key_val:
        utils.loquacious("  Setting ENV value")
        key, val = key_val.split("=", 1)
        write_env_value(env, key, val, base64)
    elif stdin:
        utils.loquacious("  Setting ENV value from stdin")
        val = click.get_text_stream("stdin").read().strip()
        write_env_value(env, key_val, val, base64)
    else:
        utils.loquacious("  Printing single ENV value")
        try:
            value = env[key_val]
            click.echo(value)
        except KeyError:
            raise click.ClickException(f"No value found for '{key_val}'")


def write_env_value(env, key, value, base64=False):
    try:
        env_file = Path(env["VG_ENV_FILE"])
        utils.loquacious(f"  Adding {key}={value} to {env_file}")
        with env_file.open("a") as fp:
            if base64:
                value = utils.to_base64(value)
            fp.write(f"{key}={value}\n")
    except KeyError:
        raise click.ClickException(<commit_msg>Sort written env vars, don’t write VG_ ones<commit_after>from pathlib import Path

import click

from vantage import utils


@click.command(name="__env")
@click.option(
    "-b",
    "--base64",
    is_flag=True,
    help="When setting a var convert it to base64 first",
)
@click.option(
    "-s",
    "--stdin",
    is_flag=True,
    help="When setting a var read the value from stdin",
)
@click.argument("key_val", required=False)
@click.pass_obj
def env(env, key_val=None, base64=False, stdin=False):
    """Read and set environment variables and files"""
    utils.loquacious("Running __env")
    if key_val is None:
        utils.loquacious("  Printing ENV")
        for k, v in env.items():
            click.echo(f"{k}={v}")
    elif "=" in key_val:
        utils.loquacious("  Setting ENV value")
        key, val = key_val.split("=", 1)
        write_env_value(env, key, val, base64)
    elif stdin:
        utils.loquacious("  Setting ENV value from stdin")
        val = click.get_text_stream("stdin").read().strip()
        write_env_value(env, key_val, val, base64)
    else:
        utils.loquacious("  Printing single ENV value")
        try:
            value = env[key_val]
            click.echo(value)
        except KeyError:
            raise click.ClickException(f"No value found for '{key_val}'")


def write_env_value(env, key, value, base64=False):
    try:
        env_file = Path(env["VG_ENV_FILE"])
        utils.loquacious(f"  Adding {key}={value} to {env_file}")
        if base64:
            value = utils.to_base64(value)
        env[key] = value
        with env_file.open("w") as fp:
            for key, value in sorted(env.items()):
                if not key.startswith("VG_"):
                    fp.write(f"{key}={value}\n")
    except KeyError:
        raise click.ClickException(
{'fraction_non_alphanumeric': 0.13163972286374134, 'fraction_numerical': 0.009815242494226328, 'mean_word_length': 2.6358866736621196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1494266', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1139, 'n_words': 296}