|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2025 Google LLC. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http=//www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Set SCRIPT_DIR to the current directory of this file. |
| 18 | +SCRIPT_DIR=$(cd -P "$(dirname "$0")" >/dev/null 2>&1 && pwd) |
| 19 | +SCRIPT_FILE="${SCRIPT_DIR}/$(basename "$0")" |
| 20 | + |
| 21 | +## |
| 22 | +## Local Development |
| 23 | +## |
| 24 | +## These functions should be used to run the local development process |
| 25 | +## |
| 26 | + |
| 27 | +if [[ ! -d venv ]] ; then |
| 28 | + echo "./venv not found. Setting up venv" |
| 29 | + python3 -m venv "$PWD/venv" |
| 30 | +fi |
| 31 | +source "$PWD/venv/bin/activate" |
| 32 | + |
| 33 | +if which pip3 ; then |
| 34 | + PIP_CMD=pip3 |
| 35 | +elif which pip ; then |
| 36 | + PIP_CMD=pip |
| 37 | +else |
| 38 | + echo "pip not found. Please add pip to your path." |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | +if ! which nox ; then |
| 42 | + $PIP_CMD install nox |
| 43 | +fi |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +## clean - Cleans the build output |
| 48 | +function clean() { |
| 49 | + if [[ -d '.tools' ]] ; then |
| 50 | + rm -rf .tools |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +## build - Builds the project without running tests. |
| 55 | +function build() { |
| 56 | + nox --sessions build |
| 57 | +} |
| 58 | + |
| 59 | +## test - Runs local unit tests. |
| 60 | +function test() { |
| 61 | + nox --sessions unit --python=3.13 |
| 62 | +} |
| 63 | + |
| 64 | +## e2e - Runs end-to-end integration tests. |
| 65 | +function e2e() { |
| 66 | + if [[ ! -f .envrc ]] ; then |
| 67 | + write_e2e_env .envrc |
| 68 | + fi |
| 69 | + source .envrc |
| 70 | + nox --sessions system --python=3.13 |
| 71 | +} |
| 72 | + |
| 73 | +## fix - Fixes code format. |
| 74 | +function fix() { |
| 75 | + nox --sessions format |
| 76 | +} |
| 77 | + |
| 78 | +## lint - runs the linters |
| 79 | +function lint() { |
| 80 | + # Check the commit includes a go.mod that is fully |
| 81 | + # up to date. |
| 82 | + nox --sessions lint |
| 83 | +} |
| 84 | + |
| 85 | +## deps - updates project dependencies to latest |
| 86 | +function deps() { |
| 87 | + echo "Todo: deps" |
| 88 | + exit 1 |
| 89 | +} |
| 90 | + |
| 91 | +# write_e2e_env - Loads secrets from the gcloud project and writes |
| 92 | +# them to target/e2e.env to run e2e tests. |
| 93 | +function write_e2e_env(){ |
| 94 | + # All secrets used by the e2e tests in the form <env_name>=<secret_name> |
| 95 | + secret_vars=( |
| 96 | + MYSQL_CONNECTION_NAME=MYSQL_CONNECTION_NAME |
| 97 | + MYSQL_USER=MYSQL_USER |
| 98 | + MYSQL_USER_IAM=MYSQL_USER_IAM_GO |
| 99 | + MYSQL_PASS=MYSQL_PASS |
| 100 | + MYSQL_DB=MYSQL_DB |
| 101 | + MYSQL_MCP_CONNECTION_NAME=MYSQL_MCP_CONNECTION_NAME |
| 102 | + MYSQL_MCP_PASS=MYSQL_MCP_PASS |
| 103 | + POSTGRES_CONNECTION_NAME=POSTGRES_CONNECTION_NAME |
| 104 | + POSTGRES_USER=POSTGRES_USER |
| 105 | + POSTGRES_USER_IAM=POSTGRES_USER_IAM_GO |
| 106 | + POSTGRES_PASS=POSTGRES_PASS |
| 107 | + POSTGRES_DB=POSTGRES_DB |
| 108 | + POSTGRES_CAS_CONNECTION_NAME=POSTGRES_CAS_CONNECTION_NAME |
| 109 | + POSTGRES_CAS_PASS=POSTGRES_CAS_PASS |
| 110 | + POSTGRES_CUSTOMER_CAS_CONNECTION_NAME=POSTGRES_CUSTOMER_CAS_CONNECTION_NAME |
| 111 | + POSTGRES_CUSTOMER_CAS_PASS=POSTGRES_CUSTOMER_CAS_PASS |
| 112 | + POSTGRES_CUSTOMER_CAS_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_DOMAIN_NAME |
| 113 | + POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME |
| 114 | + POSTGRES_MCP_CONNECTION_NAME=POSTGRES_MCP_CONNECTION_NAME |
| 115 | + POSTGRES_MCP_PASS=POSTGRES_MCP_PASS |
| 116 | + SQLSERVER_CONNECTION_NAME=SQLSERVER_CONNECTION_NAME |
| 117 | + SQLSERVER_USER=SQLSERVER_USER |
| 118 | + SQLSERVER_PASS=SQLSERVER_PASS |
| 119 | + SQLSERVER_DB=SQLSERVER_DB |
| 120 | + QUOTA_PROJECT=QUOTA_PROJECT |
| 121 | + ) |
| 122 | + |
| 123 | + if [[ -z "$TEST_PROJECT" ]] ; then |
| 124 | + echo "Set TEST_PROJECT environment variable to the project containing" |
| 125 | + echo "the e2e test suite secrets." |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | + |
| 129 | + echo "Getting test secrets from $TEST_PROJECT into $1" |
| 130 | + { |
| 131 | + for env_name in "${secret_vars[@]}" ; do |
| 132 | + env_var_name="${env_name%%=*}" |
| 133 | + secret_name="${env_name##*=}" |
| 134 | + set -x |
| 135 | + val=$(gcloud secrets versions access latest --project "$TEST_PROJECT" --secret="$secret_name") |
| 136 | + echo "export $env_var_name='$val'" |
| 137 | + done |
| 138 | + } > "$1" |
| 139 | + |
| 140 | +} |
| 141 | + |
| 142 | +## help - prints the help details |
| 143 | +## |
| 144 | +function help() { |
| 145 | + # This will print the comments beginning with ## above each function |
| 146 | + # in this file. |
| 147 | + |
| 148 | + echo "build.sh <command> <arguments>" |
| 149 | + echo |
| 150 | + echo "Commands to assist with local development and CI builds." |
| 151 | + echo |
| 152 | + echo "Commands:" |
| 153 | + echo |
| 154 | + grep -e '^##' "$SCRIPT_FILE" | sed -e 's/##/ /' |
| 155 | +} |
| 156 | + |
| 157 | +set -euo pipefail |
| 158 | + |
| 159 | +# Check CLI Arguments |
| 160 | +if [[ "$#" -lt 1 ]] ; then |
| 161 | + help |
| 162 | + exit 1 |
| 163 | +fi |
| 164 | + |
| 165 | +cd "$SCRIPT_DIR" |
| 166 | + |
| 167 | +"$@" |
| 168 | + |
0 commit comments