-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·413 lines (355 loc) · 12.4 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·413 lines (355 loc) · 12.4 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/bin/bash
# ----------------------------------------------------------------------------
# Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
#
# WSO2 LLC. licenses this file to you 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 -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# --- Set Default OS and architecture ---
# Auto-detect GO OS
DEFAULT_OS=$(go env GOOS 2>/dev/null)
if [ -z "$DEFAULT_OS" ]; then
UNAME_OS="$(uname -s)"
case "$UNAME_OS" in
Darwin) DEFAULT_OS="darwin" ;;
Linux) DEFAULT_OS="linux" ;;
MINGW*|MSYS*|CYGWIN*) DEFAULT_OS="windows" ;;
*) echo "Unsupported OS: $UNAME_OS"; exit 1 ;;
esac
fi
# Auto-detect GO ARCH
DEFAULT_ARCH=$(go env GOARCH 2>/dev/null)
if [ -z "$DEFAULT_ARCH" ]; then
UNAME_ARCH="$(uname -m)"
case "$UNAME_ARCH" in
x86_64|amd64) DEFAULT_ARCH="amd64" ;;
arm64|aarch64) DEFAULT_ARCH="arm64" ;;
*) echo "Unsupported architecture: $UNAME_ARCH"; exit 1 ;;
esac
fi
GO_OS=${2:-$DEFAULT_OS}
GO_ARCH=${3:-$DEFAULT_ARCH}
echo "================================================================"
echo "Using GO OS: $GO_OS and ARCH: $GO_ARCH"
echo "================================================================"
# Version management
VERSION_FILE="version.txt"
if [ -f "$VERSION_FILE" ]; then
VERSION=$(cat "$VERSION_FILE")
else
VERSION="1.0.0"
fi
# Configuration
BINARY_NAME="consent-server"
TARGET_DIR="target"
OUTPUT_DIR="$TARGET_DIR/server"
INTEGRATION_OUTPUT_DIR="${INTEGRATION_OUTPUT_DIR:-$TARGET_DIR/server-integration}"
DIST_DIR="$TARGET_DIR/dist"
SOURCE_DIR="consent-server/cmd/server"
CONFIG_SOURCE="consent-server/cmd/server/repository/conf/deployment.yaml"
CONFIG_TARGET="$OUTPUT_DIR/repository/conf/deployment.yaml"
TEST_CONFIG_SOURCE_MYSQL="tests/integration/repository/conf/deployment-mysql.yaml"
TEST_CONFIG_SOURCE_SQLITE="tests/integration/repository/conf/deployment-sqlite.yaml"
TEST_CONFIG_SOURCE_POSTGRES="tests/integration/repository/conf/deployment-postgres.yaml"
# Package naming
PACKAGE_OS=$GO_OS
PACKAGE_ARCH=$GO_ARCH
# Normalize OS name for distribution packaging
if [ "$GO_OS" = "darwin" ]; then
PACKAGE_OS=macos
elif [ "$GO_OS" = "windows" ]; then
PACKAGE_OS="win"
fi
if [ "$GO_ARCH" = "amd64" ]; then
PACKAGE_ARCH=x64
fi
# Strip leading 'v' from version for zip/folder naming (e.g. v0.5.0 -> 0.5.0)
PACKAGE_VERSION="${VERSION#v}"
PRODUCT_FOLDER="${BINARY_NAME}-${PACKAGE_VERSION}-${PACKAGE_OS}-${PACKAGE_ARCH}"
# ============================================================================
# Functions
# ============================================================================
function clean_all() {
echo "================================================================"
echo "Cleaning all build artifacts..."
rm -rf "$TARGET_DIR"
echo "✓ All build artifacts cleaned"
echo "================================================================"
}
function clean() {
clean_all
}
function build_binary() {
echo "================================================================"
echo "Building Consent Management API Server..."
# Set binary name with .exe extension for Windows
local output_binary="$BINARY_NAME"
if [ "$GO_OS" = "windows" ]; then
output_binary="${BINARY_NAME}.exe"
fi
# Clean previous build
if [ -d "$OUTPUT_DIR" ]; then
echo "Cleaning previous build..."
rm -rf "$OUTPUT_DIR"
fi
# Create directory structure
echo "Creating directory structure..."
mkdir -p "$OUTPUT_DIR/repository/conf"
# Build the binary with version and build date
echo "Compiling binary for $GO_OS/$GO_ARCH..."
cd consent-server
GOOS=$GO_OS GOARCH=$GO_ARCH CGO_ENABLED=0 go build \
-ldflags "-X 'main.version=$VERSION' -X 'main.buildDate=$(date -u '+%Y-%m-%d %H:%M:%S UTC')'" \
-o "../$OUTPUT_DIR/$output_binary" "./cmd/server"
cd ..
# Copy configuration
echo "Copying configuration..."
cp "$CONFIG_SOURCE" "$CONFIG_TARGET"
# Copy start script
echo "Copying start script..."
cp start.sh "$OUTPUT_DIR/start.sh"
chmod +x "$OUTPUT_DIR/start.sh"
# Copy database scripts
if [ -d "consent-server/dbscripts" ]; then
echo "Copying database scripts..."
mkdir -p "$OUTPUT_DIR/dbscripts"
cp consent-server/dbscripts/*.sql "$OUTPUT_DIR/dbscripts/" 2>/dev/null || true
fi
# Copy API specifications
if [ -d "api" ]; then
echo "Copying API specifications..."
mkdir -p "$OUTPUT_DIR/api"
cp api/*.yaml "$OUTPUT_DIR/api/" 2>/dev/null || true
fi
# Copy README
if [ -f "README.md" ]; then
echo "Copying README..."
cp "README.md" "$OUTPUT_DIR/"
fi
# Copy version file
if [ -f "$VERSION_FILE" ]; then
echo "Copying version file..."
cp "$VERSION_FILE" "$OUTPUT_DIR/"
fi
# Make binary executable (not needed for Windows)
if [ "$GO_OS" != "windows" ]; then
chmod +x "$OUTPUT_DIR/$output_binary"
fi
echo ""
echo "✓ Build completed successfully!"
echo ""
echo "Build output:"
echo " Binary: $OUTPUT_DIR/$output_binary"
echo " Start Script: $OUTPUT_DIR/start.sh"
echo " Config: $CONFIG_TARGET"
if [ -d "$OUTPUT_DIR/dbscripts" ]; then
echo " DB Scripts: $OUTPUT_DIR/dbscripts/"
fi
if [ -d "$OUTPUT_DIR/api" ]; then
echo " API Specs: $OUTPUT_DIR/api/"
fi
echo ""
echo "To run the server:"
echo " cd $OUTPUT_DIR && ./start.sh"
echo ""
echo "Or with debug mode:"
echo " cd $OUTPUT_DIR && ./start.sh --debug"
echo ""
echo "================================================================"
}
function package() {
echo "================================================================"
echo "Creating distribution package..."
# Build first
build_binary
# Create distribution directory
mkdir -p "$DIST_DIR/$PRODUCT_FOLDER"
# Copy everything from bin to dist
echo "Copying build artifacts to distribution..."
cp -r "$OUTPUT_DIR/"* "$DIST_DIR/$PRODUCT_FOLDER/"
# Copy version file
if [ -f "$VERSION_FILE" ]; then
cp "$VERSION_FILE" "$DIST_DIR/$PRODUCT_FOLDER/"
fi
# Copy README if exists
if [ -f "README.md" ]; then
cp "README.md" "$DIST_DIR/$PRODUCT_FOLDER/"
fi
# Copy LICENSE if exists
if [ -f "LICENSE" ]; then
cp "LICENSE" "$DIST_DIR/$PRODUCT_FOLDER/"
fi
# Create zip file
echo "Creating zip archive..."
(cd "$DIST_DIR" && zip -r "$PRODUCT_FOLDER.zip" "$PRODUCT_FOLDER")
# Clean up unzipped folder
rm -rf "${DIST_DIR:?DIST_DIR not set}/${PRODUCT_FOLDER:?PRODUCT_FOLDER not set}"
echo ""
echo "✓ Distribution package created successfully!"
echo ""
echo "Package: $DIST_DIR/$PRODUCT_FOLDER.zip"
echo ""
echo "================================================================"
}
function run_server() {
echo "================================================================"
echo "Running Consent Management API Server..."
# Build first if binary doesn't exist
if [ ! -f "$OUTPUT_DIR/$BINARY_NAME" ]; then
echo "Binary not found. Building first..."
build_binary
fi
echo "Starting server..."
cd "$OUTPUT_DIR" && "./$BINARY_NAME"
echo "================================================================"
}
function test_unit() {
echo "================================================================"
echo "Running unit tests..."
echo "Cleaning test cache..."
go clean -testcache
cd consent-server || exit 1
go test ./internal/... -v -cover
cd "$SCRIPT_DIR" || exit 1
echo "================================================================"
}
function test_integration() {
echo "================================================================"
echo "Running integration tests..."
OUTPUT_DIR="$INTEGRATION_OUTPUT_DIR"
CONFIG_TARGET="$OUTPUT_DIR/repository/conf/deployment.yaml"
local test_server_dir="../../$OUTPUT_DIR"
echo "Integration server output: $OUTPUT_DIR"
# Select database type: default sqlite, override with DB_TYPE env var
local db_type="${DB_TYPE:-sqlite}"
echo "Database type: $db_type"
# Validate DB_TYPE before doing any build work.
local test_config_source
case "$db_type" in
mysql)
test_config_source="$TEST_CONFIG_SOURCE_MYSQL"
;;
sqlite)
test_config_source="$TEST_CONFIG_SOURCE_SQLITE"
;;
postgres)
test_config_source="$TEST_CONFIG_SOURCE_POSTGRES"
;;
*)
echo "✗ Unsupported DB_TYPE '$db_type'. Supported values: mysql, sqlite, postgres"
exit 1
;;
esac
# Clean test cache to ensure tests run with latest changes
echo "Cleaning test cache..."
go clean -testcache
# Build a dedicated integration-test server so normal build outputs are untouched.
build_binary
# Replace app config with test config for integration tests
echo "Copying test configuration..."
if [ -f "$test_config_source" ]; then
cp "$test_config_source" "$CONFIG_TARGET"
echo "✓ Test configuration copied ($test_config_source)"
else
echo "⚠ Warning: Test configuration not found, using default config"
fi
# Run integration test suite
echo "Starting integration test suite..."
cd tests/integration || exit 1
set +e
TEST_SERVER_DIR="$test_server_dir" DB_TYPE="$db_type" go run main.go
TEST_EXIT_CODE=$?
set -e
cd "$SCRIPT_DIR" || exit 1
if [ $TEST_EXIT_CODE -ne 0 ]; then
echo "✗ Integration tests failed"
exit 1
fi
echo "✓ Integration tests passed"
echo "================================================================"
}
function test_all() {
test_unit
test_integration
}
function show_help() {
echo "Consent Management API Build Script"
echo ""
echo "Usage: ./build.sh {command} [OS] [ARCH]"
echo ""
echo "Commands:"
echo " clean - Clean build artifacts"
echo " clean_all - Clean all artifacts including distributions"
echo " build - Build the binary and prepare output directory"
echo " package - Build and create distribution package (zip)"
echo " run - Build and run the server"
echo " test_unit - Run unit tests"
echo " test_integration - Run integration tests"
echo " test - Run all tests"
echo " help - Show this help message"
echo ""
echo "Optional Arguments:"
echo " OS - Target operating system (darwin, linux, windows)"
echo " Default: auto-detected ($DEFAULT_OS)"
echo " ARCH - Target architecture (amd64, arm64)"
echo " Default: auto-detected ($DEFAULT_ARCH)"
echo ""
echo "Examples:"
echo " ./build.sh build # Build for current platform"
echo " ./build.sh build linux amd64 # Build for Linux AMD64"
echo " ./build.sh build darwin arm64 # Build for macOS ARM64"
echo " ./build.sh package # Create distribution package"
echo " ./build.sh run # Build and run server"
echo ""
}
# ============================================================================
# Main script execution
# ============================================================================
case "$1" in
clean)
clean
;;
clean_all)
clean_all
;;
build)
build_binary
;;
package)
package
;;
run)
run_server
;;
test_unit)
test_unit
;;
test_integration)
test_integration
;;
test)
test_all
;;
help|--help|-h)
show_help
;;
*)
echo "Error: Unknown command '$1'"
echo ""
show_help
exit 1
;;
esac