Skip to content

Ask question only once #1459

@henningWoehr

Description

@henningWoehr

Actual Situation

We are using copier, to copy kubernetes manifests of app to a repo (set name, namespace and app specific stuff). After deploying that app on kubernetes, changing the name or namespace is pretty difficult when using something like a persistent volume.

Desired Situation

When answering the questions like name or namespace of a resource, it shouldn't be allowed to change these afterwards to force a clean deletion and new creation of the app. Otherwise it could lead to multiple instances of the app and the deletion of each resource would be a hassle.

Proposed solution

Current Solution

My current workaround to this consists of the following:

In the copier.yaml I specify the question with the when key checking if the question already has a value. If so, skip this answer

resource_name:
  when: "{{ not resource_name }}"
  type: str
  help: What is the name for the resources which will be applied in k8s? (Can't be changed afterwards)
  default: container-registry
  validator: "{{resource_name | validate_dns_name}}"

Because questions won't get saved in the answers.yaml, on the third copy/update the questions would be asked again. For that I use the ContextHook and write the answer in into the _copier_answers dict in the context.

from copier_templates_extensions import ContextHook

CONST_QUESTIONS = [
    "resource_name",
    "resource_namespace"
]

class ConstQuestions(ContextHook):
    update = False

    def hook(self, context: dict):
        for question in CONST_QUESTIONS:
            context["_copier_answers"][question] = context.get(question)

Possible Solution

The problem with my solution is that we will never see the answered question again. Nice would be, that the answered question would be grey instead of yellow.

Possible copier.yaml:

resource_name:
  type: str
  help: What is the name for the resources which will be applied in k8s? (Can't be changed afterwards)
  answer_once: true
  default: container-registry
  validator: "{{resource_name | validate_dns_name}}"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions