Exemplo n.º 1
0
def list_profiles(quiet: bool):
    profiles = Config().get_profiles()

    if not profiles:
        return click.echo("No profiles exist.")

    for p in profiles:
        if quiet:
            print(p.name)
        else:
            print(p)
Exemplo n.º 2
0
def do_delete(name: str):
    click.echo(f"Deleting profile '{name}'...")
    config = Config()

    if config.get_profile(name):
        config.delete_profile(profile_name=name)
        config.save()
    else:
        print("Profile does not exist.")
        sys.exit(0)

    if not config.get_profile(name):
        print(f"Successfully deleted profile.")
    else:
        print("Error: failed to delete profile.")
Exemplo n.º 3
0
def clone(repo: str, profile: str):
    profile: Profile = command_utils.choose_profile_interactive(
        profile, title="Choose a profile to clone with")

    click.echo(f"Cloning '{repo}' with profile: {profile}")
    profile: Profile = Config().get_profile(name=profile)

    if not profile:
        click.echo(
            f"Profile '{profile}' appears to be missing. Have you created it?",
            err=True,
        )
        exit(1)

    ssh_key = profile.ssh_key
    if not ssh_key:
        click.echo(f"Can't find SSH key for profile '{profile}'")
        sys.exit(0)

    if not (profile.git_name and profile.git_email):
        click.echo(
            f"Warning: your Git name and/or email address are missing for this profile. You need to set them.",
            err=True,
        )
        exit(1)

    ssh_key = ssh.fully_normalise_path(ssh_key)
    if not os.path.exists(ssh_key):
        print(f"Error: can't find your SSH key at '{ssh_key}'.")
        sys.exit(1)

    dest = re.findall(r"^.*/(.*?)(?:\.git)?$", repo)[0]
    ssh_command = ssh.get_ssh_command(ssh_key)

    do_clone(ssh_command, repo, dest)
    cwd = os.getcwd()

    try:
        os.chdir(dest)
    except FileNotFoundError:
        print(
            f"Could not find your cloned repository. Please 'cd' into it and then use 'gitprof profile apply'."
        )

    command_utils.set_git_configs(profile)

    print(f"Finished setting up your Git repository.")
    os.chdir(cwd)
Exemplo n.º 4
0
def edit_profile(name: str, git_name: str, git_email: str):
    config = Config()
    profile: Profile = config.get_profile(name)

    if not profile:
        print(f"Profile '{name}' does not exist.")
        sys.exit(0)

    if git_name:
        profile.git_name = git_name
    if git_email:
        profile.git_email = git_email

    for field, value in profile.__dict__.items():
        value = ux.get_simple_input(question=f"Enter new value for '{field}'",
                                    default=value)
        setattr(profile, field, value)

    config.set_profile(profile)
    config.save()
Exemplo n.º 5
0
def choose_profile_interactive(profile: str, title: str) -> str:
    config = Config()

    while not profile:
        config.reload()

        if not profile:
            options = config.get_profiles()
            for index, profile in enumerate(options):
                options[index] = profile.name

                if profile.service:
                    whitespace = " " * (35 - len(profile.name))
                    options[
                        index] = f"{options[index]}{whitespace}({profile.service})"

            chosen = ux.get_input_from_list(
                title=title,
                options=options,
                fallback="create new profile",
                fallback_index=-1,
            )

            matches = re.findall(r"^(.*?)\s*\(.*\)\s*$", chosen.value)
            if matches:
                chosen.value = matches[0]

            if not config.get_profiles() or chosen.is_fallback():
                name = ux.get_simple_input(
                    question=
                    "Enter a name for your new profile (e.g. 'github')",
                    validator=lambda n: len(n.split(" ")) == 1,
                )
                profile = name
                os.system(f"gitprof profile create {name}")
                continue

            profile = chosen.value

    return profile
Exemplo n.º 6
0
def create_profile(name: str, username: str = None):
    name = name or ux.get_simple_input(
        question="Enter a name for your profile (e.g. 'github')",
        validator=lambda i: i,
    )

    ux.print_header(f"Creating profile: {name}")
    config = Config()

    if config.get_profile(name):
        click.echo(
            f"Profile '{name}' already exists. Please edit it instead of creating a new one.",
            err=True,
        )
        sys.exit(1)

    keys: List[str] = get_key_options()
    chosen = ux.get_input_from_list(
        title="Choose an SSH key",
        options=keys,
        fallback="CREATE NEW KEY",
    )

    new_ssh_key = False
    if chosen.is_fallback():
        ssh_key = ux.get_simple_input(
            question="Enter the name for your SSH key",
            validator=lambda p: len(p.split(" ")) == 1,
            default=name,
        )
        ssh_key = create_ssh_key(ssh_key)
        new_ssh_key = True
    else:
        ssh_key = chosen.value

    key_path = get_ssh_key_path(ssh_key)

    service = ux.get_input_from_list(
        title="Select the service this profile is for",
        options=services.get_services(),
        fallback_enter_manually=True,
    ).value

    profile: Profile = Profile(name, key_path, service=service)

    if not username and service.lower() == "github":
        username = ux.get_simple_input(
            f"What's your username for the service '{service}'?\n"
            f"This is used to automatically find your committer name and email",
            optional=True,
        )

    if username:
        print(f"Trying to find your Git committer name and email...")
        result = github_utils.get_name_and_email(username)

        if result and len(result) == 2:
            name, email = result

            profile.git_name = name
            profile.git_email = email

            click.echo(
                f"\nYour name and email were found and are set as the defaults for the next questions."
            )
        else:
            click.echo(
                f"Failed to find name and email. You'll need to enter them manually.\n"
            )

    profile.git_name = ux.get_simple_input("Enter your Git committer name",
                                           default=profile.git_name)
    profile.git_email = ux.get_simple_input("Enter your Git committer email",
                                            default=profile.git_email)

    if new_ssh_key:
        print(f"Your new SSH public key is:\n{ssh.get_public_key(ssh_key)}")
        url = services.get_ssh_url_for(profile.service)

        if (url and ux.get_simple_input(
                question=
                f"Would you like to open the settings on '{profile.service}' to add your SSH key [Y/n]",
                default="Y",
                show_default=False,
        ).lower() == "y"):
            webbrowser.open_new_tab(url)

    config.add_profile(profile)
    config.save()

    click.echo(f"Saved profile: {profile.name}")
Exemplo n.º 7
0
def apply_profile(profile: str):
    profile = command_utils.choose_profile_interactive(
        profile, title="Choose a profile to apply")

    profile: Profile = Config().get_profile(name=profile)
    command_utils.set_git_configs(profile)