New to bash. The script I wrote works, but I don't believe it is written in the best way possible.
The script only assesses how to execute the aws ecr get-login
command depending on the parameters and versions of the aws-cli
and docker
currently installed on the host machine.
#!/usr/bin/env bash
set -e
declare aws_region="${AWS_REGION:?''}"
declare registry_id="${REGISTRY_ID:?''}"
declare docker_version=$(docker version -f {{.Client.Version}} | sed 's/[^0-9.]//g')
if ! [[ -z $aws_region ]]; then
$aws_region = "--region $aws_region"
else
echo "Need to set AWS_REGION (eg. us-west-2)"
fi
if ! [[ -z $registry_id ]]; then
$registry_id = "--registy-id $registry_id"
fi
if [[ $docker_version < '17.06' ]]; then
options = "--no-include_email"
fi
query="aws ecr get-login $aws_region $registry_id $options"
echo $query
$($query)
if ! [[ $? == 0 ]]; then
echo "---ERROR! Please login to AWS with myob-auth or ADFS."
exit 1
fi
Any ideas for improvements? I'm personally getting bad smells in the code from the 3 if statements and the way the $query
variable works.