-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgo.sh
More file actions
248 lines (206 loc) · 9.35 KB
/
go.sh
File metadata and controls
248 lines (206 loc) · 9.35 KB
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
#!/usr/bin/env bash
# This script clones the SDK repo and updates it with the generated API modules
# Pre-requisites: Java, goimports, Go
set -eo pipefail
ROOT_DIR=$(git rev-parse --show-toplevel)
SDK_REPO_LOCAL_PATH="${ROOT_DIR}/sdk-repo-updated"
OAS_REPO=https://github.com/stackitcloud/stackit-api-specifications
SDK_GO_VERSION="1.21"
SERVICES_FOLDER="${SDK_REPO_LOCAL_PATH}/services"
EXAMPLES_FOLDER="${SDK_REPO_LOCAL_PATH}/examples"
SCRIPTS_FOLDER="${SDK_REPO_LOCAL_PATH}/scripts"
GENERATOR_LOG_LEVEL="error" # Must be a Java log level (error, warn, info...)
SDK_GO_VERSION="1.21"
generate_go_sdk() {
# Required parameters
local GENERATOR_JAR_PATH=$1
local GIT_HOST=$2
local GIT_USER_ID=$3
# Optional parameters
local GIT_REPO_ID=$4
local SDK_REPO_URL=$5
local SDK_BRANCH=$6
# Check required parameters
if [[ -z ${GIT_HOST} ]]; then
echo "GIT_HOST not specified."
exit 1
fi
if [[ -z ${GIT_USER_ID} ]]; then
echo "GIT_USER_ID id not specified."
exit 1
fi
# Check optional parameters and set defaults if not provided
if [[ -z ${GIT_REPO_ID} ]]; then
echo "GIT_REPO_ID not specified, default will be used."
GIT_REPO_ID="stackit-sdk-go"
fi
if [[ -z ${SDK_REPO_URL} ]]; then
echo "SDK_REPO_URL not specified, default will be used."
SDK_REPO_URL="https://github.com/stackitcloud/stackit-sdk-go.git"
fi
# Check dependencies
if type -p goimports >/dev/null; then
:
else
echo "Goimports not installed, unable to proceed. For getting the dependencies, run "
echo "make project-tools"
exit 1
fi
if type -p go >/dev/null; then
:
else
echo "! Go not installed, unable to proceed."
exit 1
fi
# Clone SDK repo
if [ -d "${SDK_REPO_LOCAL_PATH}" ]; then
echo "Old SDK repo clone was found, it will be removed."
rm -rf "${SDK_REPO_LOCAL_PATH}"
fi
git clone --quiet "${SDK_REPO_URL}" "${SDK_REPO_LOCAL_PATH}"
# Install SDK project tools
cd "${SDK_REPO_LOCAL_PATH}"
git checkout "${SDK_BRANCH}"
make project-tools
# Backup of the current state of the SDK services dir (services/)
sdk_services_backup_dir=$(mktemp -d)
if [[ ! ${sdk_services_backup_dir} || -d {sdk_services_backup_dir} ]]; then
echo "! Unable to create temporary directory"
exit 1
fi
cleanup() {
rm -rf "${sdk_services_backup_dir}"
}
cp -a "${SERVICES_FOLDER}/." "${sdk_services_backup_dir}"
# Cleanup after we are done
trap cleanup EXIT
# Remove old contents of services dir (services/)
rm -rf "${SERVICES_FOLDER}"
rm "${SDK_REPO_LOCAL_PATH}/go.work"
if [ -f "${SDK_REPO_LOCAL_PATH}/go.work.sum" ]; then
rm "${SDK_REPO_LOCAL_PATH}/go.work.sum"
fi
echo "go ${SDK_GO_VERSION}" >${SDK_REPO_LOCAL_PATH}/go.work
if [ -d ${SDK_REPO_LOCAL_PATH}/core ]; then
cd "${SDK_REPO_LOCAL_PATH}/core"
go work use .
fi
warning=""
# Generate SDK for each service
for service_json in ${ROOT_DIR}/oas/*.json; do
service="${service_json##*/}"
service="${service%.json}"
# Remove invalid characters to ensure a valid Go pkg name
service="${service//-/}" # remove dashes
service="${service// /}" # remove empty spaces
service="${service//_/}" # remove underscores
service=$(echo "${service}" | tr '[:upper:]' '[:lower:]') # convert upper case letters to lower case
service=$(echo "${service}" | tr -d -c '[:alnum:]') # remove non-alphanumeric characters
go_pkg_name_format="^[a-z0-9]+$"
if [[ ! ${service} =~ ${go_pkg_name_format} ]]; then # check that it is a single lower case word
echo "Service ${service} has an invalid Go package name even after removing invalid characters. The generate-sdk.sh script might need to be updated to catch corner case, contact the repo maintainers."
exit 1
fi
contains_empty_space_pattern=" |'"
if [[ ${service_json} =~ ${contains_empty_space_pattern} ]]; then
echo "OAS filename ${service_json} has empty spaces, the generation will fail. If the OAS was downloaded using the make download-oas command, it should be fixed in the api-specifications repo, please contact the repo maintainers at ${OAS_REPO}."
exit 1
fi
if grep -E "^$service$" "${ROOT_DIR}/languages/golang/blacklist.txt"; then
echo "Skipping blacklisted service ${service}"
warning+="Skipping blacklisted service ${service}\n"
continue
fi
echo -e "\n>> Generating \"${service}\" service..."
cd "${ROOT_DIR}"
GO_POST_PROCESS_FILE="gofmt -w" \
mkdir -p "${SERVICES_FOLDER}/${service}/"
cp "${ROOT_DIR}/languages/golang/.openapi-generator-ignore" "${SERVICES_FOLDER}/${service}/.openapi-generator-ignore"
regional_api=
if grep -E "^$service$" ${ROOT_DIR}/regional-whitelist.txt; then
echo "Generating new regional api"
regional_api="regional_api"
fi
# Run the generator for Go
java -Dlog.level=${GENERATOR_LOG_LEVEL} -jar ${jar_path} generate \
--generator-name go \
--input-spec "${service_json}" \
--output "${SERVICES_FOLDER}/${service}" \
--package-name "${service}" \
--enable-post-process-file \
--git-host "${GIT_HOST}" \
--git-user-id "${GIT_USER_ID}" \
--git-repo-id "${GIT_REPO_ID}" \
--global-property apis,models,modelTests=true,modelDocs=false,apiDocs=false,supportingFiles \
--additional-properties=isGoSubmodule=true,enumClassPrefix=true,generateInterfaces=true,$regional_api \
--http-user-agent "stackit-sdk-go/${service}" \
--reserved-words-mappings type=types \
--config "${ROOT_DIR}/languages/golang/openapi-generator-config.yml"
# Remove unnecessary files
rm "${SERVICES_FOLDER}/${service}/.openapi-generator-ignore"
rm "${SERVICES_FOLDER}/${service}/.openapi-generator/FILES"
# If there's a comment at the start of go.mod, copy it
go_mod_backup_path="${sdk_services_backup_dir}/${service}/go.mod"
if [ -f ${go_mod_backup_path} ]; then
go_mod_backup_first_line="$(head -n 1 ${go_mod_backup_path})"
is_comment_pattern="^\/\/"
if [[ ${go_mod_backup_first_line} =~ ${is_comment_pattern} ]]; then
echo "Found comment at the top of ${service}/go.mod"
go_mod_path="${SERVICES_FOLDER}/${service}/go.mod"
echo -e "${go_mod_backup_first_line}\n$(cat ${go_mod_path})" >${go_mod_path}
fi
fi
# Move tests to the service folder
cp "${SERVICES_FOLDER}"/"${service}"/test/* "${SERVICES_FOLDER}/${service}"
rm -r "${SERVICES_FOLDER}/${service}/test/"
# If the service has a wait package files, move them inside the service folder
if [ -d "${sdk_services_backup_dir}/${service}/wait" ]; then
echo "Found ${service} \"wait\" package"
cp -r "${sdk_services_backup_dir}/${service}/wait" "${SERVICES_FOLDER}/${service}/wait"
fi
# If the service has a CHANGELOG file, move it inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/CHANGELOG.md" ]; then
echo "Found ${service} \"CHANGELOG\" file"
cp -r "${sdk_services_backup_dir}/${service}/CHANGELOG.md" "${SERVICES_FOLDER}/${service}/CHANGELOG.md"
fi
# If the service has a LICENSE file, move it inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/LICENSE.md" ]; then
echo "Found ${service} \"LICENSE\" file"
cp -r "${sdk_services_backup_dir}/${service}/LICENSE.md" "${SERVICES_FOLDER}/${service}/LICENSE.md"
fi
# If the service has a NOTICE file, move it inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/NOTICE.txt" ]; then
echo "Found ${service} \"NOTICE\" file"
cp -r "${sdk_services_backup_dir}/${service}/NOTICE.txt" "${SERVICES_FOLDER}/${service}/NOTICE.txt"
fi
# If the service has a VERSION file, move it inside the service folder
if [ -f "${sdk_services_backup_dir}/${service}/VERSION" ]; then
echo "Found ${service} \"VERSION\" file"
cp -r "${sdk_services_backup_dir}/${service}/VERSION" "${SERVICES_FOLDER}/${service}/VERSION"
fi
cd "${SERVICES_FOLDER}/${service}"
go work use .
# Make sure that dependencies are uptodate
go get -u ./...
go mod tidy
done
# Add examples to workspace
if [ -d "${EXAMPLES_FOLDER}" ]; then
for example_dir in ${EXAMPLES_FOLDER}/*; do
cd "${example_dir}"
go work use .
done
fi
# Add scripts to workspace
if [ -d "${SCRIPTS_FOLDER}" ]; then
cd "${SCRIPTS_FOLDER}"
go work use .
fi
# Cleanup after SDK generation
cd "${SDK_REPO_LOCAL_PATH}"
goimports -w "${SERVICES_FOLDER}/"
make sync-tidy
if [[ -n "$warning" ]]; then
echo -e "\nSome of the services were skipped during creation!\n$warning"
fi
}