预定义函数值的最佳方法是什么?

预定义函数值的最佳方法是什么?

我编写了一个脚本来帮助我将服务部署到 AWS。在我的项目中,我使用多个 AWS 账户和多个服务,因此我想预定义一些值以使使用更清晰。我是这样做的:

#!/bin/bash

declare -A environments
environments["sandbox"]="aws_sandbox_account_id"
environments["staging"]="aws_staging_account_id"
environments["prod"]="aws_production_account_id"

declare -A services
services["service1"]="api-service-1-ecs-id"
services["service2"]="api-service-2-ecs-id"
services["service3"]="api-service-3-ecs-id"

aws_role=${ASSUMED_ROLE}
service="${services[$1]}"
environment="${environments[$aws_role]}"

echo "deploying ${service} on ${aws_role} with account id ${environment}"
echo "aws ecs update-service --cluster \"arn:aws:ecs:eu-west-2:${environment}:cluster/my-cluster\" --service \"arn:aws:ecs:eu-west-2:${environment}:service/my-cluster/${service}\" --force-new-deployment"
aws ecs update-service --cluster "arn:aws:ecs:eu-west-2:${environment}:cluster/my-cluster" --service "arn:aws:ecs:eu-west-2:${environment}:service/my-cluster/${service}" --force-new-deployment

然后我定义了以下函数

deploy_service () {
  bash /path/to/file/script.sh $1
}

但我想知道是否有更好的方法来做到这一点,或者至少是更惯用的方法

相关内容