-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun
executable file
·151 lines (122 loc) · 3.33 KB
/
run
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
#!/usr/bin/env bash
set -euo pipefail
if [[ -z "${APP_DIR:-""}" ]]; then
APP_DIR="$(dirname "${0}")"
fi
. "${APP_DIR}/scripts/run/docker-file.sh"
. "${APP_DIR}/scripts/run/shell.sh"
. "${APP_DIR}/scripts/lib/utils.sh"
# If we're running in CI we need to disable TTY allocation for docker compose
# commands that enable it by default, such as exec and run.
# Idea from: https://github.com/nickjj/docker-flask-example/blob/main/run
TTY=""
if [[ ! -t 1 ]]; then
TTY="-T"
fi
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
function help {
cat <<EOF
$0 [command] [options...]
Main commands:
format Format code of the whole project
lint Lint the whole project
pre-commit Format, lint and test the whole project
test Test the whole project
docker compose commands:
dc:run Run a function from "./run" inside the "app" docker service
Dev commands:
dev:code Prepare docker services for development environment
dev:dc Run docker compose with development configuration
dev:build Build docker service with development configuration
EOF
shell:help
dockerfile:help
}
# ---------------------------------------------- #
# Helper functions #
# ---------------------------------------------- #
# Check if the command is from the docker container or the host.
# INSIDE_DOCKER=1 is set from the Dockerfile
function is_inside_docker {
if [[ ${INSIDE_DOCKER:-0} -eq 1 ]]; then
return 0
else
return 1
fi
}
# ---------------------------------------------- #
# Main #
# ---------------------------------------------- #
function test {
if is_inside_docker; then
pytest
else
dc:run test
fi
}
function lint {
if is_inside_docker; then
local status=0
shell:lint || status=${?}
dockerfile:lint || status=${?}
npx markdownlint . || status=${?}
npx prettier --check . || status=${?}
if [[ ${status} -eq 0 ]]; then
success "✔ Project linted"
else
alert "✖ Project linted: issue(s) found"
exit ${status}
fi
else
dc:run lint
fi
}
function format {
if is_inside_docker; then
shell:format
dockerfile:format
black tests/.
npx prettier --list-different --write .
success "✔ Project formatted"
else
dc:run format
fi
}
function pre-commit {
if is_inside_docker; then
format
lint
test
else
dc:run pre-commit
fi
}
# ---------------------------------------------- #
# docker compose #
# ---------------------------------------------- #
function dc:run {
docker compose run ${TTY} app ./run "${@}"
docker compose rm -f -v
}
# ---------------------------------------------- #
# Dev #
# ---------------------------------------------- #
dev_dc_options=(
-f docker-compose.yml
-f docker-compose.dev.yml
)
function dev:code {
dev:build
dev:dc up -d --remove-orphans
dev:dc exec app bash
}
function dev:dc {
docker compose "${dev_dc_options[@]}" "${@}"
}
function dev:build {
docker buildx bake "${dev_dc_options[@]}"
}
# Idea from: https://github.com/adriancooney/Taskfile
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"