-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathapp.sh
More file actions
executable file
·395 lines (346 loc) · 11.3 KB
/
app.sh
File metadata and controls
executable file
·395 lines (346 loc) · 11.3 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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VERSION=$(cat "$SCRIPT_DIR/VERSION")
BINARY="onwatch"
DARWIN_FULL_TAGS="menubar,desktop,production"
DARWIN_CGO_LDFLAGS="-framework UniformTypeIdentifiers"
# --- Colors ---
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
info() { echo -e "${CYAN}${BOLD}==> $1${NC}"; }
success() { echo -e "${GREEN}${BOLD}==> $1${NC}"; }
error() { echo -e "${RED}${BOLD}==> ERROR: $1${NC}" >&2; }
warn() { echo -e "${YELLOW}${BOLD}==> $1${NC}"; }
# --- Usage ---
usage() {
cat <<EOF
${BOLD}onWatch v${VERSION} -- Project Management Script${NC}
${CYAN}USAGE:${NC}
./app.sh [FLAGS...]
${CYAN}FLAGS:${NC}
--build, -b Build production binary (macOS includes menubar support)
--test, -t Run all tests with race detection and coverage
--smoke, -s Quick validation: vet + build check + short tests
--run, -r Build and run in debug mode (foreground)
--release Run tests, then build release binaries
--clean, -c Remove binary, coverage files, dist/, test cache
--stop Stop a running instance (native or Docker)
--docker Docker mode: --build/--run/--clean/--stop use Docker
--deps, -d, --install,
--dependencies,
--requirements Install dependencies (Go, git) for your OS
--help, -h Show this help message
${CYAN}EXAMPLES:${NC}
./app.sh --build # Build production binary
./app.sh --test # Run full test suite
./app.sh --smoke # Quick pre-commit check
./app.sh --clean --build --run # Clean, rebuild, and run
./app.sh --deps --build --test # Install deps, build, test
./app.sh --release # Full release build (5 platforms)
./app.sh --docker --build # Build Docker image
./app.sh --docker --run # Build image and start container
./app.sh --docker --stop # Stop running container
./app.sh --docker --clean # Remove container and image
${CYAN}NOTES:${NC}
Flags can be combined. Execution order is always:
deps -> clean -> build -> test -> smoke -> release -> run
With --docker, build/run/clean/stop operate on Docker instead of native Go.
EOF
}
# --- Flag parsing ---
DO_DEPS=false
DO_CLEAN=false
DO_BUILD=false
DO_TEST=false
DO_SMOKE=false
DO_RELEASE=false
DO_RUN=false
DO_STOP=false
DO_DOCKER=false
if [[ $# -eq 0 ]]; then
usage
exit 0
fi
for arg in "$@"; do
case "$arg" in
--build|-b)
DO_BUILD=true ;;
--test|-t)
DO_TEST=true ;;
--smoke|-s)
DO_SMOKE=true ;;
--run|-r)
DO_RUN=true ;;
--release)
DO_RELEASE=true ;;
--clean|-c)
DO_CLEAN=true ;;
--stop)
DO_STOP=true ;;
--docker)
DO_DOCKER=true ;;
--deps|-d|--install|--dependencies|--requirements)
DO_DEPS=true ;;
--help|-h)
usage
exit 0 ;;
*)
error "Unknown flag: $arg"
echo ""
usage
exit 1 ;;
esac
done
# --- Step functions ---
do_deps() {
info "Installing dependencies..."
if [[ "$(uname)" == "Darwin" ]]; then
info "Detected macOS -- using Homebrew"
if ! command -v brew &>/dev/null; then
error "Homebrew not found. Install from https://brew.sh"
exit 1
fi
if ! command -v go &>/dev/null; then
info "Installing Go..."
brew install go
else
success "Go already installed: $(go version)"
fi
if ! command -v git &>/dev/null; then
info "Installing git..."
brew install git
else
success "git already installed: $(git --version)"
fi
elif [[ -f /etc/debian_version ]]; then
info "Detected Debian/Ubuntu -- using apt"
sudo apt-get update -qq
if ! command -v go &>/dev/null; then
info "Installing Go..."
sudo apt-get install -y golang
else
success "Go already installed: $(go version)"
fi
if ! command -v git &>/dev/null; then
info "Installing git..."
sudo apt-get install -y git
else
success "git already installed: $(git --version)"
fi
elif [[ -f /etc/redhat-release ]] || [[ -f /etc/fedora-release ]]; then
info "Detected Fedora/RHEL -- using dnf"
if ! command -v go &>/dev/null; then
info "Installing Go..."
sudo dnf install -y golang
else
success "Go already installed: $(go version)"
fi
if ! command -v git &>/dev/null; then
info "Installing git..."
sudo dnf install -y git
else
success "git already installed: $(git --version)"
fi
else
error "Unsupported OS. Please install Go and git manually."
exit 1
fi
success "Dependencies ready."
}
do_clean() {
info "Cleaning build artifacts..."
rm -f "$SCRIPT_DIR/$BINARY"
rm -f "$SCRIPT_DIR/coverage.out" "$SCRIPT_DIR/coverage.html"
rm -rf "$SCRIPT_DIR/dist/"
go clean -testcache
success "Clean complete."
}
do_build() {
info "Building onWatch v${VERSION}..."
build_native_binary "$SCRIPT_DIR/$BINARY"
success "Built ./$BINARY ($(du -h "$BINARY" | cut -f1 | xargs))"
}
build_native_binary() {
local output="$1"
cd "$SCRIPT_DIR"
if [[ "$(uname)" == "Darwin" ]]; then
CGO_ENABLED=1 CGO_LDFLAGS="$DARWIN_CGO_LDFLAGS" go build \
-tags "$DARWIN_FULL_TAGS" \
-ldflags="-s -w -X main.version=$VERSION" \
-o "$output" .
return
fi
go build \
-ldflags="-s -w -X main.version=$VERSION" \
-o "$output" .
}
build_darwin() {
cd "$SCRIPT_DIR"
if [[ "$(uname)" != "Darwin" ]]; then
error "macOS menubar builds require a macOS host"
return 1
fi
mkdir -p "$SCRIPT_DIR/dist"
for arch in arm64 amd64; do
local output="dist/onwatch-darwin-${arch}"
info "Building ${output}..."
CGO_ENABLED=1 CGO_LDFLAGS="$DARWIN_CGO_LDFLAGS" GOOS=darwin GOARCH="$arch" go build \
-tags "$DARWIN_FULL_TAGS" \
-ldflags="-s -w -X main.version=$VERSION" \
-o "$SCRIPT_DIR/$output" .
done
success "Built macOS binaries in dist/"
}
do_test() {
info "Running tests with race detection and coverage..."
cd "$SCRIPT_DIR"
go test -race -cover -count=1 ./...
success "All tests passed."
}
do_smoke() {
info "Running smoke checks..."
cd "$SCRIPT_DIR"
info " go vet ./..."
go vet ./...
info " Build check..."
build_native_binary /dev/null
info " Short tests..."
go test -short -count=1 ./...
success "Smoke checks passed."
}
do_release() {
info "Running tests before release..."
cd "$SCRIPT_DIR"
go test -race -cover -count=1 ./...
success "Tests passed."
info "Building release artifacts for onWatch v${VERSION}..."
mkdir -p "$SCRIPT_DIR/dist"
if [[ "$(uname)" == "Darwin" ]]; then
build_darwin
else
warn "Skipping macOS binaries on non-macOS host. Use macOS CI or a local macOS build host for menubar artifacts."
fi
local targets=(
"linux:amd64:"
"linux:arm64:"
"windows:amd64:.exe"
)
for target in "${targets[@]}"; do
IFS=':' read -r os arch ext <<< "$target"
local output="dist/onwatch-${os}-${arch}${ext}"
info " Building ${output}..."
CGO_ENABLED=0 GOOS="$os" GOARCH="$arch" go build \
-ldflags="-s -w -X main.version=$VERSION" \
-o "$SCRIPT_DIR/$output" .
done
success "Release build complete. Binaries in dist/:"
ls -lh "$SCRIPT_DIR/dist/"
}
do_run() {
info "Building and running onWatch v${VERSION} in debug mode..."
do_build
info "Starting ./onwatch --debug"
exec "$SCRIPT_DIR/$BINARY" --debug
}
do_stop() {
info "Stopping onWatch..."
if [[ -f "$SCRIPT_DIR/$BINARY" ]]; then
"$SCRIPT_DIR/$BINARY" stop && success "Stopped." || warn "No running instance found."
else
warn "Binary not found. Build first with --build."
fi
}
# --- Docker functions ---
docker_ensure() {
if ! command -v docker &>/dev/null; then
error "Docker not found. Install from https://docs.docker.com/get-docker/"
exit 1
fi
if ! docker info &>/dev/null 2>&1; then
error "Docker daemon is not running."
exit 1
fi
}
do_docker_build() {
docker_ensure
info "Building Docker image onwatch:${VERSION}..."
docker build -t onwatch:latest \
--build-arg VERSION="$VERSION" \
--build-arg BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
"$SCRIPT_DIR"
local size
size=$(docker image ls onwatch:latest --format '{{.Size}}')
success "Docker image built: onwatch:latest ($size)"
}
do_docker_run() {
docker_ensure
# Build if image doesn't exist
if ! docker image inspect onwatch:latest &>/dev/null; then
do_docker_build
fi
# Stop existing container if running
docker rm -f onwatch 2>/dev/null || true
# Create data dir
mkdir -p "$SCRIPT_DIR/onwatch-data"
# Build env-file arg
local env_args=()
if [[ -f "$SCRIPT_DIR/.env" ]]; then
env_args=(--env-file "$SCRIPT_DIR/.env")
else
warn "No .env file found. Create one from .env.docker.example"
fi
info "Starting onWatch container..."
docker run -d --name onwatch \
-p "${ONWATCH_PORT:-9211}:9211" \
-v "$SCRIPT_DIR/onwatch-data:/data" \
"${env_args[@]}" \
--restart unless-stopped \
--memory 64m \
onwatch:latest
success "Container started. Dashboard: http://localhost:${ONWATCH_PORT:-9211}"
info " Logs: docker logs -f onwatch"
info " Stop: ./app.sh --docker --stop"
}
do_docker_stop() {
docker_ensure
info "Stopping onWatch container..."
if docker stop onwatch 2>/dev/null; then
docker rm onwatch 2>/dev/null || true
success "Container stopped and removed."
else
warn "No running container named 'onwatch' found."
fi
}
do_docker_clean() {
docker_ensure
info "Cleaning Docker resources..."
docker rm -f onwatch 2>/dev/null && info " Removed container: onwatch" || true
docker rmi onwatch:latest 2>/dev/null && info " Removed image: onwatch:latest" || true
success "Docker clean complete."
}
# --- Execute in order: deps -> clean -> build -> test -> smoke -> release -> run/stop ---
if $DO_DOCKER; then
$DO_DEPS && do_deps
$DO_CLEAN && do_docker_clean
$DO_BUILD && do_docker_build
$DO_TEST && do_test
$DO_SMOKE && do_docker_build
$DO_RELEASE && { warn "Release builds native binaries, not Docker images. Skipping."; }
$DO_STOP && do_docker_stop
$DO_RUN && do_docker_run
else
$DO_DEPS && do_deps
$DO_CLEAN && do_clean
$DO_BUILD && do_build
$DO_TEST && do_test
$DO_SMOKE && do_smoke
$DO_RELEASE && do_release
$DO_STOP && do_stop
$DO_RUN && do_run
fi
exit 0