我需要在Makefile中创建一个查找表/字典/映射来查找键值信息.
我一直在尝试使用ifeq语句来做同样的事情,但我的陈述似乎失败了:
# this gets the account id from the current user's ARN, you must have the AWS CLI and jq installed
AWS_ACCOUNT_ID:=$(shell aws iam get-user | jq -r '.User.Arn' | awk -F ':' '{print $$5;}')
# define a friendly account name for output
ifeq ($(AWS_ACCOUNT_ID), 123456)
AWS_ACCOUNT_FRIENDLY:=staging
endif
ifeq ($(AWS_ACCOUNT_ID), 789012)
AWS_ACCOUNT_FRIENDLY:=preprod
endif
ifeq ($(AWS_ACCOUNT_ID), 345678)
AWS_ACCOUNT_FRIENDLY:=production
endif
Run Code Online (Sandbox Code Playgroud)
它似乎只适用于第一个值123456而不适用于其他值.
有没有办法在Make中定义字典/地图,只需通过帐户ID的密钥查找帐户友好名称?
Mad*_*ist 10
我无法解释为什么你没有看到你期望的行为:我会验证你期望的价值AWS_ACCOUNT_ID:也许你的shell脚本没有做你想要的.尝试添加以下内容:
AWS_ACCOUNT_ID := $(shell ...)
$(info AWS_ACCOUNT_ID = '$(AWS_ACCOUNT_ID)')
Run Code Online (Sandbox Code Playgroud)
看看你得到了什么.
但是与你的更一般的问题有关,我更喜欢在处理这样的情况时使用构造的宏名称,而不是很多ifeq值:
AWS_123456_FRIENDLY := staging
AWS_789012_FRIENDLY := preprod
AWS_345678_FRIENDLY := production
AWS_ACCOUNT_ID := $(shell ...)
AWS_ACCOUNT_FRIENDLY := $(AWS_$(AWS_ACCOUNT_ID)_FRIENDLY)
Run Code Online (Sandbox Code Playgroud)