Exemple #1
0
def copy(ctx: click.Context, src_template_name: str, dest_template_name: str, description: str, version: str):
    local_templates = get_local_templates()
    if not local_templates.get(src_template_name):
        click.echo(Texts.SRC_TEMPLATE_NOT_FOUND.format(src_template_name=src_template_name))
        exit(1)

    if local_templates.get(dest_template_name):
        click.echo(Texts.TEMPLATE_ALREADY_EXISTS.format(dest_template_name=dest_template_name))
        exit(1)

    if not description:
        description = None
        while not description or not description.strip():
            description = click.prompt(Texts.DESCRIPTION_PROMPT.format(max_len=MAX_TEMPLATE_DESCRIPTION_LENGTH))

        description = description[:MAX_TEMPLATE_DESCRIPTION_LENGTH]

    try:
        new_template = Template(name=dest_template_name, description=description, local_version=version)
        new_template.render_from_existing_template(src_template_name=src_template_name)
        click.echo(Texts.COPY_SUCCESS.format(dest_template_name=dest_template_name,
                                             src_template_name=src_template_name))
    except Exception:
        error_msg = Texts.COPY_FAILURE.format(src_template_name=src_template_name,
                                              dest_template_name=dest_template_name)
        logger.exception(error_msg)
        click.echo(error_msg)
        exit(1)
Exemple #2
0
def test_get_local_templates_success(mocker, monkeypatch):
    def mockreturn(path):
        return '/abc'

    monkeypatch.setattr(os.path, 'expanduser', mockreturn)

    mocker.patch("os.walk").return_value = [("template_name", "dirnames", "filenames")]
    mocker.patch("os.path.split").return_value = CHART_NAME
    mocker.patch("os.path.isfile").return_value = True
    patch.object(util.config.Config, "get_config_path", return_value="")

    with patch("builtins.open", mock_open(read_data=CORRECT_CHART_FILE)), \
         patch.object(util.config.Config, "get_config_path", return_value="config_path"): # noqa
        dict = get_local_templates()

        assert len(dict) == 1
        assert dict[CHART_NAME].name == CHART_NAME
Exemple #3
0
def install(state: State, template_name: str):
    chart_file_location = os.path.join(Config.get_config_path(), "packs",
                                       template_name)

    with spinner(text=Texts.GETTING_LIST_OF_TEMPLATES_MSG):
        repository_name, access_token = get_repository_configuration()

        try:
            remote_templates = get_remote_templates(repository_name,
                                                    access_token)
        except ExceptionWithMessage as e:
            click.echo(e.message)
            sys.exit(1)

        remote_template_counterpart = remote_templates.get(template_name)

        if not remote_template_counterpart:
            click.echo(
                Texts.REMOTE_TEMPLATE_NOT_FOUND.format(
                    template_name=template_name))
            sys.exit(1)

    local_templates = get_local_templates()
    local_template_counterpart = local_templates.get(template_name)

    if local_template_counterpart:
        click.confirm(Texts.LOCAL_VERSION_ALREADY_INSTALLED.format(
            local_version=local_template_counterpart.local_version,
            template_name=local_template_counterpart.name,
            remote_version=remote_template_counterpart.remote_version),
                      abort=True)

        # noinspection PyBroadException
        try:
            shutil.rmtree(chart_file_location)
        except Exception:
            logger.exception("failed to remove local copy of template!")

    with spinner(text=Texts.DOWNLOADING_TEMPLATE):
        repository_name, access_token = get_repository_configuration()

        g = Github(repository_name, access_token)

        g.download_whole_directory(template_name, chart_file_location)

    click.echo("successfully installed!")
Exemple #4
0
def install(ctx: click.Context, template_name: str):
    packs_location = os.path.join(Config.get_config_path(), "packs")
    chart_file_location = os.path.join(packs_location, template_name)
    repository_address = get_repository_address()

    with spinner(
            text=Texts.GETTING_LIST_OF_TEMPLATES_MSG) as templates_spinner:
        try:
            remote_template = load_remote_template(
                template_name, repository_address=repository_address)
        except Exception:
            templates_spinner.stop()
            handle_error(logger,
                         user_msg=Texts.FAILED_TO_LOAD_TEMPLATE.format(
                             template_name=template_name),
                         log_msg=Texts.FAILED_TO_LOAD_TEMPLATE.format(
                             template_name=template_name),
                         add_verbosity_msg=ctx.obj.verbosity == 0)
            sys.exit(1)

        if not remote_template:
            templates_spinner.stop()
            handle_error(logger,
                         user_msg=Texts.REMOTE_TEMPLATE_NOT_FOUND.format(
                             template_name=template_name),
                         log_msg=Texts.REMOTE_TEMPLATE_NOT_FOUND.format(
                             template_name=template_name),
                         add_verbosity_msg=ctx.obj.verbosity == 0)
            sys.exit(1)

    local_templates = get_local_templates()
    local_template_counterpart = local_templates.get(template_name)

    if local_template_counterpart:
        if (not click.get_current_context().obj.force) and (not click.confirm(
                Texts.LOCAL_VERSION_ALREADY_INSTALLED.format(
                    local_version=local_template_counterpart.local_version,
                    template_name=local_template_counterpart.name,
                    remote_version=remote_template.remote_version))):
            sys.exit(0)
        # noinspection PyBroadException
        try:
            shutil.rmtree(chart_file_location)
        except Exception:
            logger.exception("failed to remove local copy of template!")

    with spinner(text=Texts.DOWNLOADING_TEMPLATE) as download_spinner:
        try:
            download_remote_template(template=remote_template,
                                     repository_address=repository_address,
                                     output_dir_path=packs_location)
        except Exception:
            download_spinner.stop()
            handle_error(logger,
                         user_msg=Texts.FAILED_TO_INSTALL_TEMPLATE.format(
                             template_name=template_name,
                             repository_name=repository_address),
                         log_msg=Texts.FAILED_TO_INSTALL_TEMPLATE.format(
                             template_name=template_name,
                             repository_name=repository_address),
                         add_verbosity_msg=ctx.obj.verbosity == 0)
            sys.exit(1)

    update_resources_in_packs()

    click.echo("successfully installed!")