cookiecutter conditional prompt and capture output to variable

307 views Asked by At

I am very much new in cookiecutter programming and trying to create an interactive program similar to below example to generate config! The requirement is to prompt for sub options conditionally if getting yes on one prompt and capture value into variable for later use. My desired menu is as below

$ python -m cookiecutter .
  [1/5] Select your project name: (Short project name or application code): Example1
  [2/5] Select your project slug: (Example1): 
  [3/5] Setting up DEV environment?
    1 - yes
    2 - no
    Choose from [1/2] (1): 1
        DEV Account Id (What is your AWS Dev account's id?): 1234567890
  [4/5] Setting up TST environment?
    1 - no
    2 - yes
    Choose from [1/2] (1):
  [5/5] Setting up PRD environment?
    1 - no
    2 - yes
    Choose from [1/2] (1):

Problem1# In actual, the line DEV Account Id (What is your AWS Dev account's id?) getting printed only after all prompt completes at cookiecutter.json and not after answering [3/5] Setting up DEV environment?

Problem2# Python script is capturing the value however I am unable to import that into cookiecutter variable for later use. Error message: '_aws_dev_account_info' is undefined

cookiecutter.json

{
    "project_name": "Short project name or application code",
    "project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '-') }}",    
    "dev_account": ["yes", "no"],
    "tst_account": ["no", "yes"],
    "prd_account": ["no", "yes"],

    "__prompts__": {
        "project_name": "Select your project name:",
        "project_slug": "Select your project slug:",
        "dev_account": "Setting up DEV environment?",
        "tst_account": "Setting up TST environment?",
        "prd_account": "Setting up PRD environment?"
    },
    "aws_dev_account_id": "{{ cookiecutter._aws_dev_account_id.strip() }}"
}

In hooks/pre_gen_project.py I have written sample code to get a conditional prompt if answer of dev_account is yes only. this should return the value to cookiecutter in return, but that is not working for me or I am doing mistake here.

if "{{ cookiecutter.dev_account }}" == "yes":
    aws_dev_account_info = read_user_variable("\t_aws_dev_account_id","What is your AWS Dev account's id?")
    print("My value is {0}".format(aws_dev_account_info))
    
    """{{ cookiecutter.update({"_aws_dev_account_id": aws_dev_account_info})}}"""
    
else:
    """{{ cookiecutter.update()}}"""

sys.exit(0)
0

There are 0 answers