forked from MisterMX/crossbuilder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·257 lines (215 loc) · 7.64 KB
/
setup.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env bash
BASE_PATH=""
SUBMODULE_URL="https://github.com/mproffitt/crossbuilder.git"
RED=$(echo $'\033[31m')
YELLOW=$(echo $'\033[33m')
CYAN=$(echo $'\033[36m')
WHITE=$(echo $'\033[37m')
RESET=$(echo $'\033[00m')
CROSSPLANE_VERSION="1.17.0"
VERSION="v0.0.1"
function inform() {
if [ "$1" = '-n' ]; then
shift
echo -n "$WHITE[INFO]$RESET $@" 1>&2
else
echo "$WHITE[INFO]$RESET $@" 1>&2
fi
}
function error() {
echo "$RED[ERROR]$RESET $@" 1>&2
}
function warn() {
echo "$YELLOW[WARN]$RESET $@" 1>&2
}
function question() {
local message="$1"
shift
local original_options=$@
local options=$@
if [ -n "${options}" ]; then
message="$message [${options// /|}]"
options="${options// /\\|}"
else
options=".*"
fi
message="$CYAN$message > $RESET"
# use </dev/tty to ensure read has a tty when
# this script is run in a pipe
read -erp "${message}" answer </dev/tty
if [ -z "$answer" ] || ! grep -qi "${options}" <<<"${answer}"; then
answer=$(question "${message}" ${original_options})
fi
echo "$answer"
}
moduleroot() {
local moduleName
local wd
wd="$(pwd)"
while [ ! -d ".git" ] && [ "$(pwd)" != "/" ]; do
cd ..
done
moduleName=$(basename $(pwd))
if [ "$moduleName" = "/" ]; then
warn "Cannot find root directory for current module." 1>&2
cd "$wd"
return 1
fi
return 0
}
moduleroot || (
ans=$(question "Create new git repository?" "yes" "no")
ans="${ans,,}"
if [ "${ans:0:1}" = "n" ]; then
exit 0
fi
git init
url=$(question "Enter the git remote URL (e.g. [email protected]:example/repo.git)")
git remote add origin $url
)
cb_path() {
git submodule foreach --quiet 'echo $(git config remote.origin.url) $path' |
grep 'crossbuilder.git' | awk '{print $2}'
}
# Check if crossbuilder has already been added and if not, add it
crossbuilder_path=$(cb_path)
if [ -z "${crossbuilder_path}" ]; then
inform "Adding crossbuilder as a submodule"
git submodule add ${SUBMODULE_URL}
git submodule init
git add .gitmodules
crossbuilder_path=$(cb_path)
fi
# If we don't have a .gitignore, create a new one
if [ ! -f .gitignore ]; then
cp crossbuilder/template/files/gitignore .gitignore
git add .gitignore
fi
# If we don't have a README, create a new one
if [ ! -f README.md ]; then
echo '# `'$(basename $(pwd))'`' >README.md
git add README.md
fi
# If we have changes, commit them
if ! git diff --cached --quiet --exit-code; then
git commit -m "Initial commit of crossbuilder submodule"
fi
if [ ! -d template ]; then
echo "Setting up the template directory for the first time"
cp -r ${crossbuilder_path}/template .
cp ${crossbuilder_path}/setup.sh template/create.sh
base_path=$(question "please enter the api extension (e.g. crossplane.example.com)")
sed -i "s|^BASE_PATH=.*|BASE_PATH='${base_path}'|" template/create.sh
fi
if [ ! -f Makefile ]; then
inform "copying Makefile"
cp ${crossbuilder_path}/template/files/Makefile Makefile
fi
if [ ! -f Dockerfile ]; then
inform "copying Dockerfile"
cp ${crossbuilder_path}/template/files/Dockerfile Dockerfile
fi
if grep -q 'setup.sh\|bash' <<<$0; then
make create
exit $?
fi
if [ -z "${BASE_PATH}" ]; then
echo "Base path is empty - please edit this script to set BASE_PATH"
echo "to the location of your APIs folder"
exit 1
fi
REPO_NAME="$(git config remote.origin.url | sed 's#\(https://\|git@\)##;s#:#/#g;s#.git##')"
OWNER=$(echo $REPO_NAME | cut -d'/' -f2)
GROUP_NAME=$(question "Enter the group name" | tr '[:upper:]' '[:lower:]')
COMPOSITION=$(question "Enter the composition name (lowercase, hyphenated)")
GROUP_CLASS=$(question "Enter the group class (camel-cased struct name)")
REPO=$(echo $REPO_NAME | cut -d'/' -f3)
# Make sure at least the first letter is uppercase so go can export it
GROUP_CLASS="${GROUP_CLASS^}"
group_class_lower=${GROUP_CLASS,,}
inform "creating directories"
mkdir -p {apis/${GROUP_NAME},apidocs,hack,${BASE_PATH}/${GROUP_NAME}/{compositions/${COMPOSITION}/templates,v1alpha1,docs,examples}}
inform "templating generate.go"
sed -e "s|<GROUP_NAME>|${GROUP_NAME}|g" \
-e "s|<BASE_PATH>|${BASE_PATH}|g" \
-e "s|<REPO>|${REPO}|g" \
-e "s|<YEAR>|$(date +%Y)|g" \
template/files/generate.go.tpl >${BASE_PATH}/${GROUP_NAME}/generate.go
inform "templating main.go"
sed -e "s|<GROUP_NAME>|${GROUP_NAME}|g" \
-e "s|<GROUP_CLASS>|${GROUP_CLASS}|g" \
-e "s|<COMPOSITION>|${COMPOSITION}|g" \
-e "s|<BASE_PATH>|${BASE_PATH}|g" \
-e "s|<REPO_NAME>|${REPO_NAME}|g" \
-e "s|<REPO>|${REPO}|g" \
-e "s|<YEAR>|$(date +%Y)|g" \
template/files/main.go.tpl >${BASE_PATH}/${GROUP_NAME}/compositions/${COMPOSITION}/main.go
if [ ! -f ${BASE_PATH}/${GROUP_NAME}/v1alpha1/doc.go ]; then
inform "templating doc.go"
sed -e "s|<GROUP_NAME>|${GROUP_NAME}|g" \
-e "s|<BASE_PATH>|${BASE_PATH}|g" \
-e "s|<REPO_NAME>|${REPO_NAME}|g" \
-e "s|<REPO>|${REPO}|g" \
-e "s|<YEAR>|$(date +%Y)|g" \
template/files/doc.go.tpl >${BASE_PATH}/${GROUP_NAME}/v1alpha1/doc.go
fi
if [ ! -f ${BASE_PATH}/${GROUP_NAME}/v1alpha1/groupversion.go ]; then
inform "templating groupversion.go"
sed -e "s|<GROUP_NAME>|${GROUP_NAME}|g" \
-e "s|<GROUP_CLASS>|${GROUP_CLASS}|g" \
-e "s|<GROUP_CLASS_LOWER>|${group_class_lower}|g" \
-e "s|<BASE_PATH>|${BASE_PATH}|g" \
-e "s|<REPO_NAME>|${REPO_NAME}|g" \
-e "s|<REPO>|${REPO}|g" \
-e "s|<YEAR>|$(date +%Y)|g" \
template/files/groupversion.go.tpl >"${BASE_PATH}/${GROUP_NAME}/v1alpha1/groupversion.go"
else
if ! grep -q "${GROUP_CLASS}List" "${BASE_PATH}/${GROUP_NAME}/v1alpha1/groupversion.go"; then
inform "updating groupversion.go"
schema="SchemeBuilder.Register(\&${GROUP_CLASS}\{\}, \&${GROUP_CLASS}List\{\})"
sed -i "s|func init() {|func init() {\n\t$schema|" ${BASE_PATH}/${GROUP_NAME}/v1alpha1/groupversion.go
fi
fi
if [ ! -f "${BASE_PATH}/${GROUP_NAME}/v1alpha1/${group_class_lower}_types.go" ]; then
inform "templating ${group_class_lower}_types.go"
SHORTNAME=$(question "Enter a shortname for the XRD type")
ENFORCE_COMPOSITION=$(question "Enforce composition?" "yes" "no")
ENFORCE_COMPOSITION="${ENFORCE_COMPOSITION,,}"
sed -e "s|<GROUP_NAME>|${GROUP_NAME}|g" \
-e "s|<GROUP_CLASS>|${GROUP_CLASS}|g" \
-e "s|<GROUP_CLASS_LOWER>|${GROUP_CLASS,,}|g" \
-e "s|<SHORTNAME>|${SHORTNAME,,}|g" \
-e "s|<COMPOSITION>|${COMPOSITION}|g" \
-e "s|<BASE_PATH>|${BASE_PATH}|g" \
-e "s|<REPO_NAME>|${REPO_NAME}|g" \
-e "s|<YEAR>|$(date +%Y)|g" \
-e "s|<REPO>|${REPO}|g" \
template/files/xrd.go.tpl >${BASE_PATH}/${GROUP_NAME}/v1alpha1/${group_class_lower}_types.go
if [ "${ENFORCE_COMPOSITION:0:1}" = "n" ]; then
sed -i '/.*enforcedCompositionRef.*/d' ${BASE_PATH}/${GROUP_NAME}/v1alpha1/${group_class_lower}_types.go
fi
fi
if [ ! -f "apis/${GROUP_NAME}/crossplane.yaml" ]; then
inform "templating crossplane.yaml"
xp_version=$(question "please enter the minimum Crossplane version")
if [ "${xp_version:0:1}" = "v" ]; then
xp_version="${xp_version:1}"
fi
CROSSPLANE_VERSION=">=v${xp_version}"
query="(.metadata.name = \"${GROUP_NAME}\") \
| (.metadata.labels.\"pkg.crossplane.io/owner\" = \"${OWNER}\") \
| (.metadata.labels.\"pkg.crossplane.io/version\" = \"${VERSION}\") \
| (.spec.crossplane.version = \"${CROSSPLANE_VERSION}\")"
yq "$query" template/files/crossplane.yaml >apis/${GROUP_NAME}/crossplane.yaml
fi
if [ ! -f hack/boilerplate.go.txt ]; then
inform "copying boilerplate.go.txt to hack directory for autogen headers"
sed -e "s|<REPO>|${REPO}|g" template/files/boilerplate.go.txt >hack/boilerplate.go.txt
fi
if [ ! -f go.mod ]; then
inform "setting up go.mod with ${REPO_NAME}"
go mod init ${REPO_NAME}
go mod tidy
fi
inform "Running make"
make build