简体   繁体   中英

Pass unknown number of argument to from command line to Makefile

I have a docker image that I want to run locally and to make my life easier I am using make file to pass AWS environment variable.

aws_access_key_id := $(shell aws configure get aws_access_key_id)
aws_secret_access_key   := $(shell aws configure get aws_secret_access_key)
aws_region := $(shell aws configure get region)

docker-run:
    docker run -e AWS_ACCESS_KEY_ID="$(aws_access_key_id)" -e AWS_SECRET_ACCESS_KEY="$(aws_secret_access_key)" -e AWS_DEFAULT_REGION="$(aws_region)" --rm mydocker-image

And I need to find a way to do something like this in my terminal

 make docker-run  -d my_db -s dev -t my_table -u my_user -i URI://redshift

 make docker-run  --pre-actions "delete from dev.my_table where first_name = 'John'" -s dev -t my_table 

 make docker-run -s3 s3://temp-parquet/avro/ -s dev -t my_table -u myuser -i URI://redshift

These are the arguments that my docker (python application with argparse ) will accept.

You can't do that, directly. The command line arguments to make are parsed by make, and must be valid make program command line arguments. Makefiles are not shell scripts and make is not a general interpreter: there's no facility for passing arbitrary options to it.

You can do this by putting them into a variable, like this:

make docker-run DOCKER_ARGS="-d my_db -s dev -t my_table -u my_user -i URI://redshift"

make docker-run DOCKER_ARGS="-d my_db -s dev -t my_table"

then use $(DOCKER_ARGS) in your makefile. But that's the only way.

If you want to do argument parsing yourself, you probably don't want a Makefile. You should probably write a Bash script instead.

Example:

#!/usr/bin/env bash
set -euo pipefail

aws_access_key_id="$(aws configure get aws_access_key_id)"
aws_secret_access_key="$(aws configure get aws_secret_access_key)"
aws_region="$(aws configure get region)"

docker run -e AWS_ACCESS_KEY_ID="$aws_access_key_id" -e AWS_SECRET_ACCESS_KEY="$aws_secret_access_key" -e AWS_DEFAULT_REGION="$aws_region" --rm mydocker-imagedocker "$@"

Note the $@ at the end, which passes the arguments from Bash to the docker command.

You might want to try someting like:

$ cat Makefile 
all:
        @echo make docker-run  -d my_db -s dev -t my_table $${MYUSER+-u "$(MYUSER)"} $${URI+-i "URI://$(URI)"}
$ make
make docker-run -d my_db -s dev -t my_table
$ make MYUSER=myuser URI=redshift
make docker-run -d my_db -s dev -t my_table -u myuser -i URI://redshift

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM