-
Notifications
You must be signed in to change notification settings - Fork 0
/
essex
executable file
·224 lines (185 loc) · 5.02 KB
/
essex
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env bash
# shellcheck disable=SC2034
# Setup the Essex Path
ESSEX_PATH="$(cd "$(dirname "$0")" ; pwd -P )" || exit 1
# Set Colors
bold=$(tput bold)
reset=$(tput sgr0)
purple=$(tput setaf 171)
red=$(tput setaf 1)
green=$(tput setaf 76)
tan=$(tput setaf 3)
blue=$(tput setaf 38)
underline=$(tput sgr 0 1)
# Use gsed (macOS)
sed="$([[ `command -v gsed` ]] && echo gsed || echo sed)"
# Print usage information
usage() {
cat <<-EOF
${bold}Essex $(version): Boilerplate for Docker Based Projects.${reset}
License: MIT Copyright (c) 2019 Utensils Union
Usage:
essex list
essex new <template> <repo-path>/<image-name> [OPTION]...
essex update
Options:
-u, --username [NAME] Sets the repo username (your dockerhub username)
-v, --vendor [NAME] Sets the vendor label
Examples:
essex new basic utensils/MyApp
essex new basic utensils/MyApp --username jamesbrink
essex new basic jamesbrink/appname
EOF
}
# Print version information
version() {
git --git-dir="$ESSEX_PATH"/.git describe --tags --abbrev=0 2>/dev/null \
|| git --git-dir="$ESSEX_PATH"/.git rev-parse --abbrev-ref HEAD
}
# List available templates
list(){
echo "Available templates:"
for template in "$ESSEX_PATH"/templates/*
do
if [ -f "$template"/.description ]
then
echo " ${bold}$(basename "$template")${reset} - $(cat "$template"/.description)"
fi
done
}
# Update Essex
update(){
echo "Updating Essex"
cd "$ESSEX_PATH" || exit 1
git reset HEAD --hard
git pull origin || git reset HEAD origin/master --hard
exit $?
}
# Collect maintainer name from git
maintainer(){
user_name="$(git config user.name)"
email="$(git config user.email)"
echo "$user_name <$email>"
}
# Update the project's maintainer
project_maintainer(){
local maintainer="$1"
$sed -i 's/org.opencontainers.image.authors=.* \\/org.opencontainers.image.authors="'"$maintainer"'" \\/g' Dockerfile
}
# Update the project's name
project_name(){
local name="$1"
$sed -r -i 's/(^IMAGE_NAME.+\?= ).+$/\1'"${name,,}"'/g' Makefile
$sed -i 's/org.opencontainers.image.title=.* \\/org.opencontainers.image.title="'"$name"'" \\/g' Dockerfile
$sed -r -i 's/^# .*/# '"$name"'/g' README.md
$sed -r -i 's|/template|/'"${name,,}"'|g' README.md
}
# Update the projects repo path
project_repo(){
local repo="$1"
echo "Setting Docker Repo: $repo"
$sed -r -i 's|(^REPO_NAMESPACE.+\?= ).+$|\1'"${repo,,}"'|g' Makefile
# We set username here by default, if it is passed in as an option
# It will be updated by another function.
$sed -r -i 's|(^REPO_USERNAME.+\?= ).+$|\1'"${repo,,}"'|g' Makefile
# We set vendor by default
$sed -i 's|org.opencontainers.image.vendor=.* \\|org.opencontainers.image.vendor="'"$repo"'" \\|g' Dockerfile
$sed -r -i 's|utensils|'"${repo,,}"'|g' README.md
git add .
}
# Update the projects vendor label
project_vendor(){
local vendor="$1"
echo "Setting Vendor: $vendor"
$sed -i 's|org.opencontainers.image.vendor=.* \\|org.opencontainers.image.vendor="'"$vendor"'" \\|g' Dockerfile
git add .
}
# Update the projects repo username
project_username(){
local username="$1"
echo "Setting Repository Username: $username"
$sed -r -i 's/(^REPO_USERNAME.+\?= ).+$/\1'"${username,,}"'/g' Makefile
git add .
}
# Create a new project from a template
new(){
local template="$2"
local repo_path=${3%/*}
local image_name=${3##*/}
# Make sure the user passed in an active template
if [ ! -f "$ESSEX_PATH"/templates/"$template"/.description ]
then
echo "No template named: $template"
list
exit 1
fi
if [ -z "$repo_path" ]
then
echo "No docker repo path was specified"
usage
exit 1
fi
if [ -z "$image_name" ]
then
echo "No image name specified"
usage
exit 1
fi
echo "Creating new project $repo_path/$image_name using template: $template"
mkdir -p "$image_name"
cd "$image_name" || exit 1
# If this is not already a git project lets init one now
if [ ! -d ".git" ]
then
git init
fi
# Copy the template files into the project
cp -r "$ESSEX_PATH"/templates/"$template"/. ./
# Remove description file from project
rm .description
# Set the project maintainer
project_maintainer "$(maintainer)"
# Set the project name
project_name "$image_name"
# Set the repo_path/vendor
project_repo "$repo_path"
# Add all files to new project
git add .
}
# Parse any subcommands
while true; do
case "$1" in
list) list; exit $? ;;
new) new "$@"; shift 3 ; break ;;
update) update; exit $? ;;
* ) usage; exit 0 ;;
esac
done
# Parse short and long options
# options=$(getopt -o uvh: --long username,vendor,help: -n 'parse-options' -- "$@")
# shellcheck disable=SC2181
if [ $? != 0 ]
then
echo "Failed parsing options." >&2
exit 1
fi
# eval set -- "$options"
while true; do
case "$1" in
-u | --username ) REPO_USERNAME="$2"; shift ;;
-v | --vendor ) VENDOR="$2"; shift ;;
-h | --help ) usage;break;;
-* ) echo "Failed parsing options." >&2; exit 1; ;;
* ) break ;;
esac
shift
done
if [ -n "$REPO_USERNAME" ]
then
project_username "$REPO_USERNAME"
fi
if [ -n "$VENDOR" ]
then
project_vendor "$VENDOR"
fi
exit 0