# Path: gg/utils.py
# def error_out(msg, raise_abort=True):
#     click.echo(click.style(msg, fg="red"))
#     if raise_abort:
#         raise click.Abort
# 
# def success_out(msg):
#     click.echo(click.style(msg, fg="green"))
# 
# def info_out(msg):
#     click.echo(msg)
# 
# Path: gg/state.py
# def read(configfile):
#     with open(configfile, "r") as f:
#         return json.load(f)
# 
# def update(configfile, data):
#     state = read(configfile)
#     state.update(data)
#     write(configfile, state)
# 
# def remove(configfile, key):
#     state = read(configfile)
#     del state[key]
#     write(configfile, state)
# 
# Path: gg/main.py
# DEFAULT_CONFIGFILE = os.path.expanduser("~/.gg.json")
# class Config:
#     def __init__(self):
# def cli(config, configfile, verbose):


import json
import getpass
import urllib.parse
import click
import requests
from gg.utils import error_out, success_out, info_out
from gg.state import read, update, remove
from gg.main import cli, pass_config
@bugzilla.command()
@click.argument("api_key", default="")
@pass_config
def login(config, api_key=""):
    """Store your Bugzilla API Key"""
    if not api_key:
        info_out(
            "If you don't have an API Key, go to:\n"
            "https://bugzilla.mozilla.org/userprefs.cgi?tab=apikey\n"
        )
        api_key = getpass.getpass("API Key: ")

    # Before we store it, let's test it.
    url = urllib.parse.urljoin(config.bugzilla_url, "/rest/whoami")
    assert url.startswith("https://"), url
    response = requests.get(url, params={"api_key": api_key})
    if response.status_code == 200:
        if response.json().get("error"):
            error_out(f"Failed - {response.json()}")
        else:
            update(
                config.configfile,
                {
                    "BUGZILLA": {
                        "bugzilla_url": config.bugzilla_url,
                        "api_key": api_key,
                        # "login": login,
                    }
                },
            )

# Path: gg/utils.py
# def error_out(msg, raise_abort=True):
#     click.echo(click.style(msg, fg="red"))
#     if raise_abort:
#         raise click.Abort
# 
# def success_out(msg):
#     click.echo(click.style(msg, fg="green"))
# 
# def info_out(msg):
#     click.echo(msg)
# 
# Path: gg/state.py
# def read(configfile):
#     with open(configfile, "r") as f:
#         return json.load(f)
# 
# def update(configfile, data):
#     state = read(configfile)
#     state.update(data)
#     write(configfile, state)
# 
# def remove(configfile, key):
#     state = read(configfile)
#     del state[key]
#     write(configfile, state)
# 
# Path: gg/main.py
# DEFAULT_CONFIGFILE = os.path.expanduser("~/.gg.json")
# class Config:
#     def __init__(self):
# def cli(config, configfile, verbose):


# Path: gg/builtins/bugzilla.py
import json
import getpass
import urllib.parse
import click
import requests
from gg.utils import error_out, success_out, info_out
from gg.state import read, update, remove
from gg.main import cli, pass_config

@bugzilla.command()
@click.argument("api_key", default="")
@pass_config
def login(config, api_key=""):
    """Store your Bugzilla API Key"""
    if not api_key:
        info_out(
            "If you don't have an API Key, go to:\n"
            "https://bugzilla.mozilla.org/userprefs.cgi?tab=apikey\n"
        )
        api_key = getpass.getpass("API Key: ")

    # Before we store it, let's test it.
    url = urllib.parse.urljoin(config.bugzilla_url, "/rest/whoami")
    assert url.startswith("https://"), url
    response = requests.get(url, params={"api_key": api_key})
    if response.status_code == 200:
        if response.json().get("error"):
            error_out(f"Failed - {response.json()}")
        else:
            update(
                config.configfile,
                {
                    "BUGZILLA": {
                        "bugzilla_url": config.bugzilla_url,
                        "api_key": api_key,
                        # "login": login,
                    }
                },
            )

            success_out("Yay! It worked!")