Example #1
0
def config(ctx, view: bool, section: str) -> None:
    """
    Configure your general settings and Github credentials for reuse.
    Available options (sections) are:

    \b
    - general: set your fullname, email and Github username
    - pat: set your Github personal access token for Github repository creation
    - all: calls general and pat
    """
    if view:
        ConfigCommand.view_current_config()
        sys.exit(0)
    if section == 'general':
        # set the full_name and email for reuse in the creation process
        ConfigCommand.config_general_settings()
    elif section == 'pat':
        # set github username and encrypted personal access token
        ConfigCommand.config_pat()
    elif section == 'all':
        # set everything
        ConfigCommand.all_settings()
        # empty section argument causes a customized error
    elif not section:
        HelpErrorHandling.args_not_provided(ctx, 'config')
        # check if a similar section handle can be used/suggested
    else:
        ConfigCommand.similar_handle(section)
Example #2
0
def handle_pat_authentification() -> str:
    """
    Try to read the encrypted Personal Access Token for GitHub.
    If this fails (maybe there was no generated key before) notify user to config its credentials for cookietemple.

    :return: The decrypted PAT
    """
    # check if the key and encrypted PAT already exist
    log.debug(
        f"Attempting to read the personal access token from {ConfigCommand.CONF_FILE_PATH}"
    )
    if os.path.exists(ConfigCommand.CONF_FILE_PATH):
        path = Path(ConfigCommand.CONF_FILE_PATH)
        yaml = YAML(typ="safe")
        settings = yaml.load(path)
        if os.path.exists(ConfigCommand.KEY_PAT_FILE) and "pat" in settings:
            pat = decrypt_pat()
            return pat
        else:
            log.debug(
                f"Unable to read the personal access token from {ConfigCommand.CONF_FILE_PATH}"
            )
            console.print(
                "[bold red]Could not find encrypted personal access token!\n")
            console.print(
                "[bold blue]Please navigate to Github -> Your profile -> Settings -> Developer Settings -> Personal access token -> "
                "Generate a new Token")
            console.print(
                "[bold blue]Only tick 'repo'. The token is a hidden input to cookietemple and stored encrypted locally on your machine."
            )
            console.print(
                "[bold blue]For more information please read" +
                "https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line\n\n"
            )
            console.print(
                "[bold blue]Lets move on to set your personal access token for your cookietemple project!"
            )
            # set the PAT
            ConfigCommand.config_pat()
            # if the user wants to create a GitHub repo but accidentally presses no on PAT config prompt
            if not os.path.exists(ConfigCommand.KEY_PAT_FILE):
                console.print(
                    "[bold red]No Github personal access token found. Please set it using [green]cookietemple config github"
                )
                sys.exit(1)
            else:
                pat = decrypt_pat()
            return pat
    else:
        console.print(
            "[bold red]Cannot find a cookietemple config file! Did you delete it?"
        )

    return ""