-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·278 lines (241 loc) · 8.47 KB
/
build.sh
File metadata and controls
executable file
·278 lines (241 loc) · 8.47 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env bash
# Copyright 2025 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http=//www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Set SCRIPT_DIR to the current directory of this file.
SCRIPT_DIR=$(cd -P "$(dirname "$0")" >/dev/null 2>&1 && pwd)
SCRIPT_FILE="${SCRIPT_DIR}/$(basename "$0")"
##
## Local Development
##
## These functions should be used to run the local development process
##
## clean - Cleans the build output
function clean() {
if [[ -d '.tools' ]] ; then
rm -rf .tools
fi
}
## generate - Generates all required files, before building
function generate() {
get_protoc
pbFile="internal/mdx/metadata_exchange.pb.go"
# Delete the old MDX pb file
rm "$pbFile"
PATH="${SCRIPT_DIR}/.tools/protoc/bin:$PATH" "${SCRIPT_DIR}/.tools/protoc/bin/protoc" \
--proto_path=. \
--go_out=. \
--go_opt=default_api_level=API_OPAQUE \
internal/mdx/metadata_exchange.proto \
--go_opt=paths=source_relative
# Add the copyright header to the generated protobuf file
mv "${pbFile}" "${pbFile}.tmp"
cat > "${pbFile}" <<EOF
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
EOF
cat "${pbFile}.tmp" >> "${pbFile}"
rm "${pbFile}.tmp"
}
# Download the protoc tool if it's not already installed.
function get_protoc() {
# Find the latest version of protoc
protoc_version=$(curl -s "https://api.github.com/repos/protocolbuffers/protobuf/releases/latest" | jq -r '.tag_name' | sed 's/v//')
proto_go_version=$(curl -s "https://api.github.com/repos/protocolbuffers/protobuf-go/releases/latest" | jq -r '.tag_name' | sed 's/v//')
mkdir -p "$SCRIPT_DIR/.tools"
versioned_cmd="$SCRIPT_DIR/.tools/protoc-$protoc_version"
if [[ -d "$versioned_cmd" ]] ; then
return
fi
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
protoc_go_arch=$arch
if [[ "$protoc_go_arch" == "x86_64" ]] ; then
protoc_go_arch="amd64"
fi
if [[ "$os" == "darwin" && "$arch" == "arm64" ]] ; then
protoc_url="https://github.com/protocolbuffers/protobuf/releases/download/v${protoc_version}/protoc-${protoc_version}-osx-aarch_64.zip"
protoc_go_url="https://github.com/protocolbuffers/protobuf-go/releases/download/v${proto_go_version}/protoc-gen-go.v${proto_go_version}.${os}.${protoc_go_arch}.tar.gz"
elif [[ "$os" == "linux" ]] ; then
protoc_url="https://github.com/protocolbuffers/protobuf/releases/download/v${protoc_version}/protoc-${protoc_version}-${os}-${arch}.zip"
protoc_go_url="https://github.com/protocolbuffers/protobuf-go/releases/download/v${proto_go_version}/protoc-gen-go.v${proto_go_version}.${os}.${protoc_go_arch}.tar.gz"
else
echo "Unsupported protoc platform : $os $arch"
exit 1
fi
echo "Downloading protoc v$protoc_version..."
curl -v -sSL "$protoc_url" -o protoc.zip
mkdir -p "$versioned_cmd"
unzip -o protoc.zip -d "$versioned_cmd"
rm -rf protoc.zip
echo "Downloading protoc-go v$proto_go_version..."
curl -v -sSL "$protoc_go_url" -o proto-go.tar.gz
mkdir -p "$versioned_cmd"
tar -zxf proto-go.tar.gz -C "$versioned_cmd/bin"
rm -rf proto-go.tar.gz
ln -sf "$versioned_cmd" ".tools/protoc"
}
## build - Builds the project without running tests.
function build() {
generate
go build ./...
}
## test - Runs local unit tests.
function test() {
go test -v -race -cover -short
}
## e2e - Runs end-to-end integration tests.
function e2e() {
if [[ ! -f .envrc ]] ; then
write_e2e_env .envrc
fi
source .envrc
e2e_ci
}
# e2e_ci - Run end-to-end integration tests in the CI system.
# This assumes that the secrets in the env vars are already set.
function e2e_ci() {
go test -v -race -cover ./e2e_mysql_test.go ./e2e_postgres_test.go ./e2e_sqlserver_test.go | tee test_results.txt
}
# Download a tool using `go install`
function get_golang_tool() {
name="$1"
github_repo="$2"
package="$3"
# Download goimports tool
version=$(curl -s "https://api.github.com/repos/$github_repo/tags" | jq -r '.[].name' | head -n 1)
mkdir -p "$SCRIPT_DIR/.tools"
cmd="$SCRIPT_DIR/.tools/$name"
versioned_cmd="$SCRIPT_DIR/.tools/$name-$version"
if [[ ! -f "$versioned_cmd" ]] ; then
GOBIN="$SCRIPT_DIR/.tools" go install "$package@$version"
mv "$cmd" "$versioned_cmd"
if [[ -f "$cmd" ]] ; then
unlink "$cmd"
fi
ln -s "$versioned_cmd" "$cmd"
fi
}
## fix - Fixes code format.
function fix() {
# run code formatting
get_golang_tool 'goimports' 'golang/tools' 'golang.org/x/tools/cmd/goimports'
".tools/goimports" -w .
go mod tidy
go fmt ./...
}
## lint - runs the linters
function lint() {
# run lint checks
get_golang_tool 'golangci-lint' 'golangci/golangci-lint' 'github.com/golangci/golangci-lint/v2/cmd/golangci-lint'
".tools/golangci-lint" run --timeout 3m
# Check the commit includes a go.mod that is fully
# up to date.
fix
if [[ -d "$SCRIPT_DIR/.git" ]] ; then
git diff --exit-code
fi
}
# lint_ci - runs lint in the CI build job, exiting with an error code if lint fails.
function lint_ci() {
fix # run code format cleanup
git diff --exit-code # fail if anything changed
lint # run lint
}
## deps - updates project dependencies to latest
function deps() {
go get -u ./...
go get -t -u ./...
go mod tidy
}
# write_e2e_env - Loads secrets from the gcloud project and writes
# them to target/e2e.env to run e2e tests.
function write_e2e_env(){
# All secrets used by the e2e tests in the form <env_name>=<secret_name>
secret_vars=(
MYSQL_CONNECTION_NAME=MYSQL_CONNECTION_NAME
MYSQL_USER=MYSQL_USER
MYSQL_USER_IAM=MYSQL_USER_IAM_GO
MYSQL_PASS=MYSQL_PASS
MYSQL_DB=MYSQL_DB
MYSQL_MCP_CONNECTION_NAME=MYSQL_MCP_CONNECTION_NAME
MYSQL_MCP_PASS=MYSQL_MCP_PASS
POSTGRES_CONNECTION_NAME=POSTGRES_CONNECTION_NAME
POSTGRES_USER=POSTGRES_USER
POSTGRES_USER_IAM=POSTGRES_USER_IAM_GO
POSTGRES_PASS=POSTGRES_PASS
POSTGRES_DB=POSTGRES_DB
POSTGRES_CAS_CONNECTION_NAME=POSTGRES_CAS_CONNECTION_NAME
POSTGRES_CAS_PASS=POSTGRES_CAS_PASS
POSTGRES_CUSTOMER_CAS_CONNECTION_NAME=POSTGRES_CUSTOMER_CAS_CONNECTION_NAME
POSTGRES_CUSTOMER_CAS_PASS=POSTGRES_CUSTOMER_CAS_PASS
POSTGRES_CUSTOMER_CAS_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_DOMAIN_NAME
POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME
POSTGRES_MCP_CONNECTION_NAME=POSTGRES_MCP_CONNECTION_NAME
POSTGRES_MCP_PASS=POSTGRES_MCP_PASS
SQLSERVER_CONNECTION_NAME=SQLSERVER_CONNECTION_NAME
SQLSERVER_USER=SQLSERVER_USER
SQLSERVER_PASS=SQLSERVER_PASS
SQLSERVER_DB=SQLSERVER_DB
QUOTA_PROJECT=QUOTA_PROJECT
)
if [[ -z "$TEST_PROJECT" ]] ; then
echo "Set TEST_PROJECT environment variable to the project containing"
echo "the e2e test suite secrets."
exit 1
fi
echo "Getting test secrets from $TEST_PROJECT into $1"
{
for env_name in "${secret_vars[@]}" ; do
env_var_name="${env_name%%=*}"
secret_name="${env_name##*=}"
set -x
val=$(gcloud secrets versions access latest --project "$TEST_PROJECT" --secret="$secret_name")
echo "export $env_var_name='$val'"
done
echo "export MYSQL_USER_IAM='$(whoami)'"
echo "export POSTGRES_USER_IAM='$(whoami)@google.com'"
} > "$1"
}
## help - prints the help details
##
function help() {
# This will print the comments beginning with ## above each function
# in this file.
echo "build.sh <command> <arguments>"
echo
echo "Commands to assist with local development and CI builds."
echo
echo "Commands:"
echo
grep -e '^##' "$SCRIPT_FILE" | sed -e 's/##/ /'
}
set -euo pipefail
# Check CLI Arguments
if [[ "$#" -lt 1 ]] ; then
help
exit 1
fi
cd "$SCRIPT_DIR"
"$@"