-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecr-repo-uri.sh
executable file
·126 lines (104 loc) · 2.68 KB
/
ecr-repo-uri.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/zsh
# -*- vim:fenc=utf-8:et:sw=2:ts=2:sts=2
#
setopt local_options
setopt local_traps
unsetopt glob_subst
#set -o errexit
set -o nounset
typeset -a create_flag help_flag version_flag
PROGNAME=${0:t}
PROGDIR=${0:h}
PACKAGE_VERSION=1.0.0-develop
AWS_OUTPUT="--output json"
REQUIRED_PROGS=( aws jq )
check_progs()
{
for p in ${REQUIRED_PROGS}
do
command -v ${p} > /dev/null 2>&1 ||
{
>&2 print -- Cannot find required program: ${p}
exit 1
}
done
}
print_version()
{
print -- "${PROGNAME} ${PACKAGE_VERSION}"
print -- "Copyright (C) 2017 Enrico M. Crisostomo"
print -- "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>."
print -- "This is free software: you are free to change and redistribute it."
print -- "There is NO WARRANTY, to the extent permitted by law."
print
print -- "Written by Enrico M. Crisostomo"
}
print_usage()
{
print -- "${PROGNAME}"
print
print -- "Usage:"
print -- "${PROGNAME} name"
print
print -- "Checks whether name is an existing ECR repository and prints its URI."
print -- "Non existing repositories can optionally be created."
print
print -- "Options:"
print -- " -c, --create Create repository if it does not exist."
print -- " -h, --help Print this message."
print -- " --version Print the program version."
}
# Main
zparseopts -D \
c=create_flag -create=create_flag \
h=help_flag -help=help_flag \
-version=version_flag
if (( ${+help_flag[1]} > 0 ))
then
print_usage
exit 0
fi
if (( ${+version_flag[1]} > 0 ))
then
print_version
exit 0
fi
(( $# == 1 )) || {
print -- Invalid number of arguments.
return 1
}
check_progs
SERVICE_NAME=$1
ECR_DESCRIBE_REPO_OUTPUT=$(aws ecr describe-repositories ${=AWS_OUTPUT} --repository-names ${SERVICE_NAME} 2> /dev/null)
if (( $? != 0 ))
then
if (( ${+create_flag[1]} == 0 ))
then
print "Cannot find ECR repository ${SERVICE_NAME}"
return 1
fi
aws ecr create-repository ${=AWS_OUTPUT} --repository-name ${SERVICE_NAME} > /dev/null
ECR_DESCRIBE_REPO_OUTPUT=$(aws ecr describe-repositories ${=AWS_OUTPUT} --repository-names ${SERVICE_NAME})
if (( $? != 0 ))
then
echo "Cannot find or create ECR repository ${SERVICE_NAME}"
return 1
fi
fi
ECR_REPO_URI=$(printf "%s\n" ${ECR_DESCRIBE_REPO_OUTPUT} | jq -r ".repositories[] | .repositoryUri")
if [[ -z ${ECR_REPO_URI} ]]
then
echo "Cannot find ECR repository ${SERVICE_NAME}"
return 1
fi
print ${ECR_REPO_URI}
# Local variables:
# coding: utf-8
# mode: sh
# eval: (sh-set-shell "zsh")
# tab-width: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# sh-indentation: 2
# End: