Search code examples
makefilemultichoiceitems

Multi select menu in makefile


I have a makefile that deploys x project in aws

The goal is to execute the make like this:

make deploy-production

And see output like this

Please select the project:
1) api
2) web
3) somethingelse

You press then the number and make continues, by assigning the choice to a variable named project

I have already "autogenerating" targets for supported envs, just want to build the multichoice for projects. Sample makefile of mine with removed nonrelevant stuff:

ENVIRONMENTS := development staging production
TARGETS := $(foreach X,$(ENVIRONMENTS),deploy-$X)

$(TARGETS):
  $(eval ENV:=$(subst deploy-,,$(@)))
  # here want to ask for project, env i have already as resolved from above line

Solution

  • Well, make can run any shell script. So if you write a shell script that will ask the user for input and accept an answer you can run it from make.

    You could do something like this:

    deploy-production:
            @echo Please select the project:; \
            echo '1) api'; \
            echo '2) web'; \
            echo '3) somethingelse'; \
            read -p 'Enter value: ' result && $(MAKE) CHOICE=$$result got-choice
    

    There is a LOT left out here: handling invalid values, converting the CHOICE value into a target that would then be a prerequisite of the got-choice target, etc. etc. I don't really think this is a good way forward but you could make it work.